it can provide HB100
target azimuth data to the
FPGA, and also periodically
align with the axis of the
wheeled platform to
provide a magnetic vehicle
heading. An optical
encoder will indicate the
moment when the rotating
platform aligns with the
vehicle so that the FPGA
To start using the HMC5883L, I
modified the I2C code. I had to
include initialization values (see
sidebar for explanation) and to
read back the 16-bit X, Y, and Z
field strengths. Before computing
the arctangent on the X and Y
values, I needed to account for
distortions in the magnetic field.
The HMC5883L User Guide (p. 25)
provided the method and formulas
40 SERVO 12.2017
Figure 5. Calibration method to determine offset and scale factor values (HMC5883L).
Figure 4. Rotating GY-271 modules
(HMC5883L) provides pointing
angle and vehicle heading.
Initializing the Honeywell
HMC5883L Three-Axis Digital
The HMC5883L magnetic sensing IC used on the GY-
271 module gives the navigation logic a way to measure
magnetic field strengths and determine magnetic
headings with 1-2 degrees of accuracy. To use the sensor,
the FPGA logic needs to initialize/control the device
through its registers. The device is configured by writes
to two registers (configuration A/B), and data is read back
via six registers (X/Y/Z MSB and LSB registers). The
device’s register list is shown in Figure A.
The meaning of each bit field in the two
configuration registers is shown on pages 11-12 of
Reference 1. For the purpose of navigation and sensor
support (where the magnetic azimuth will be provided to
the microwave sensor), the best choice of operation is a
continuous measurement mode with a fast update rate
The FPGA logic uses a small I2C core to communicate
with the device. The particular register addresses, sub
addresses, and data that will be communicated to the
sensor are defined in the i2c_rom.v file. The first line in
i2c_rom.v is a dummy write to clear the read/write
pointer. The next three lines (shown in Figure B) do the
actual initialization of the A, B, and Mode registers
The highlighted eight bits (read from left to right) are
bits that will be written into the configuration A register.
They are CRA7 (default, clear to 0), MA[1:0], DO[2:0], and
MS[1:0]. The MA bits select the number of magnetic field
samples that are averaged to provide a measurement (set
to “ 11” = 8 samples per output). The DO bits select the
data output rate (set to “110” = 75 Hz output rate). The MS
To modify initialization, the eight-bit values in
i2c_rom.v can be altered, or additional lines can be added
(and the hex address values changed). To make sure that
the additional lines are written to the HMC5883L by the
I2C state machine, it would also be necessary to change
the localparams called SETUP_5883_START and
SETUP_5883_END that appear in the top_HMC5883L.v
There are two additional lines in the i2c_rom.v file
that command a read of the status register, and a read of
the six bytes of the X, Y, and Z measured values.
Additional commands, reads, etc., can be added on new
lines. The localparams called HMC5883_READ and
HMC_5883_READ_END would need to be modified for the
added lines to be used by the state machine.
Figure B. Initialization ROM contents.
Figure A. HMC5883L register list.