GY-271 HMC5883L 3-Axis Magnetic Electronic Compass Module


Senior Member
Has anyone succeeded in getting an accurate reading of the X axis in real degrees on a GY-271 HMC5883L 3-Axis Magnetic Electronic Compass Module?


Senior Member

You need two Axes (X-Y , X-Z or Y-Z) to determine an angle. Have you managed to read any (signed) values from the device?

One you have data (field strengths) for two (orthogonal) Axes, I've written several Code Snippets (of varying complexity) for the required ATAN2 function.

Cheers, Alan.


Senior Member

I've not used that device and it's obviously a little complicated, but the data sheet seems quite straightforward. The Default settings look reasonable, but I would at least select the "continuous" mode.

As a "sanity check" I would first read all the Configuration, Mode, Status and Identification registers (0-2 and 9-12) and check that they are "as expected" (from the data sheet). Then you need to convert the three High/Low Byte values to PICaxe Words, noting that the MSB is sent first whilst PICaxe uses LSB first.

You will need to understand the "Twos Complement" number format and send the X, Y and Z values to the Terminal Emulator. Preferably rotate the sensor about 30 - 45 degrees each time around the Z-axis for a full revolution and post what you measure. The following subroutine may help with the 2s complement format (12 bits) as it appears to be used by the device.
; Demo Values
w1 = 1234 : call showW1signed
w1 = 2048 : call showW1signed
w1 = -4096 : call showW1signed
w1 = 65500 : call showW1signed

showW1signed:       ; Send a 2's complement value in w1 to the terminal
w2 = w1
if w2 => $F800 then
      w2 = -w2
      sertxd(" -")
if w2 = $F000 then
      sertxd(" OVERFLOW ")
else if w2 => 2048 then
            sertxd(" ERROR ")
      sertxd(#w2," ")
Cheers, Alan.