Go Down

Topic: Abnormal Raw Data from HMC5883L (Read 478 times) previous topic - next topic


Hi all,

I got a HMC5883L breakboard from here

I connected it to the Nano with connection SDA to A4, SCL to A5. I ran a quick test but the result looks quite weird. Despite the raw values at x and y axis seems to be ok, I could not get any useful data at the z axis. They keep jumping between 0 and -1 no matter how I rotated the board.

Can anyone give me some clue about the cause of this abnormal result? I hope this is not due to a defect inside the board.

The library I used is:


The code I used to read raw data from HMC5883L is:

Code: [Select]
#include <Wire.h>
#include <HMC5883L.h>

HMC5883L compass;

void setup()
  Serial.println("Initialize HMC5883L");
  while (!compass.begin())
    Serial.println("Could not find a valid HMC5883L sensor, check wiring!");

  /* Set up magetometer */
  compass.setOffset(0, 0);

void loop()
  Vector raw = compass.readRaw();
    Serial.print("X: ");
    Serial.print(", Y: ");
    Serial.print(", Z: ");


And the result I got is like this:

Code: [Select]
X: -410,  Y: 50,   Z: 0
X: -395,  Y: 48,   Z: 0
X: -384,  Y: 46,   Z: 0
X: -374,  Y: 73,   Z: -1
X: -367,  Y: 111,  Z: 0
X: -371,  Y: 120,  Z: -1
X: -368,  Y: 163,  Z: -1
X: -365,  Y: 156,  Z: 0
X: -345,  Y: 170,  Z: 1
X: -333,  Y: 173,  Z: -1
X: -302,  Y: 194,  Z: 0
X: -271,  Y: 217,  Z: 0


Could you try a more simple sketch, or a better tutorial ?
Do you use 5V to power the module ? The 5V is needed because the voltage regulator on the module makes 3.3V for the magnetometer.
That module does not have a level shifter for I2C. I suppose it has 10k resistors to 3.3V. It might work, but it is not perfect.

Try this tutorial : https://learn.adafruit.com/adafruit-hmc5883l-breakout-triple-axis-magnetometer-compass-sensor/


Thanks for your reply Koepel.

I tried the tutorial you suggest and I also switched the input voltage from 3.3V to 5V this time.

Unfortunately it does not make any improvement, the result is still the same. I double checked the datasheet to ensure that all the data have been read from correct registers. So I reckon neither the library nor the input is the problem at this stage.


Code: [Select]
HMC5883 Magnetometer Test

Sensor:       HMC5883
Driver Ver:   1
Unique ID:    12345
Max Value:    800.00 uT
Min Value:    -800.00 uT
Resolution:   0.20 uT

X: -20.64  Y: 6.91  Z: 0.00  uT
Heading (degrees): 174.09
X: -18.82  Y: 28.09  Z: 0.00  uT
Heading (degrees): 136.42
X: -6.36  Y: 40.27  Z: 0.10  uT
Heading (degrees): 111.58
X: 16.73  Y: 27.00  Z: 0.00  uT
Heading (degrees): 70.83
X: 17.55  Y: 17.55  Z: 0.00  uT
Heading (degrees): 57.61
X: -3.09  Y: 41.00  Z: 0.00  uT
Heading (degrees): 106.92
X: -12.45  Y: 11.73  Z: 0.00  uT
Heading (degrees): 149.33
X: -3.55  Y: 2.18  Z: 0.00  uT
Heading (degrees): 161.00
X: 14.45  Y: 15.18  Z: 0.10  uT
Heading (degrees): 59.01
X: 7.45  Y: 29.27  Z: 0.10  uT
Heading (degrees): 88.32
X: 10.55  Y: 21.45  Z: 0.00  uT
Heading (degrees): 76.43
X: -3.00  Y: 20.00  Z: 0.00  uT
Heading (degrees): 111.14
X: -1.27  Y: 35.91  Z: 0.00  uT
Heading (degrees): 104.63
X: -15.27  Y: 36.18  Z: 0.10  uT
Heading (degrees): 125.49
X: -29.73  Y: 11.91  Z: -0.10  uT
Heading (degrees): 170.77
X: -23.91  Y: -7.00  Z: 0.00  uT


Jun 22, 2016, 07:28 am Last Edit: Jun 22, 2016, 07:34 am by Koepel
After searching online, there are sometimes bad HMC5883L chips, and the Z-axis seems to have more problems than the X-axis and Y-axis.

Try to return the faulty HMC5883L and get a new one.

Good sellers ( Adafruit or Sparkfun ) sell good components. They also give schematics and example code and they have a forum.
On the other hand, if you are going to buy a cheap module, then buy the cheapest there is. The Jaycar/duinotech module costs 9.95 dollars, but they are 2.15 dollars on AliExpress (that is 2.15 dollars for a single module inclusive shipping). A real compass is 42 cents on AliExpress, which can be used to compare to the HMC5883L.

Once you have a working HMC5883L, you need to calibrate it.

Go Up