Go Down

Topic: Issues with the HMC5883L Magnetometer Compass (Read 1 time) previous topic - next topic


Hello All,

I am currently working with the HMC5883L Magnetic Compass right now and I am having problems getting it to work properly. I have the SCL connected to A5 and the SDA connected to A4. I have two 10 kohm resistors going from the SCL lines and SDA lines to VCC and VCC is connected to the 3.3V pin on the Arduino. I am using the HMC5883L library. The issue I am having, and I noticed a couple other people having this issue, is that I cannot get the values that are printed to the serial window to change, even when I turn the compass.
One other piece of information that might help lead to the solution to my problem is that if I take the compass out of the circuit, the values on the serial window will not change, which suggest to me that there is no connection in the first place. However, what I also find is that when I push down on the compass, occasionally the values will change. However, I still can't get the values to change purely by rotating the compass, which is kinda important. Anybody else experience this problem and know the solution? Or any suggestions of where to start trouble shooting? Here is my code:

#include <HMC5883L.h>

#include <Wire.h>

//Testing the magnometer compass//

HMC5883L compass;

int error=0;

void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
   Serial.print("   ");   
   Serial.print("   ");   
   Serial.print("   \tScaled:\t");
   Serial.print("   ");   
   Serial.print("   ");   

   Serial.print("   \tHeading:\t");
   Serial.print(" Radians   \t");
   Serial.println(" Degrees   \t");

void setup()
  Serial.println("Application Started");
  compass = HMC5883L();
  error = compass.SetScale(1.3);
  if(error != 0)
  error = compass.SetMeasurementMode(Measurement_Continuous);
  if(error != 0)

void loop()
  MagnetometerRaw raw = compass.ReadRawAxis();
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  float heading = atan2(raw.YAxis, raw.XAxis);
  if(heading < 0)
    heading += 2*PI;
  float headingDegrees = heading * 180/M_PI;
  Output(raw, scaled, heading, headingDegrees);


Hey everyone,

This problem has been resolved. After consulting an I2C testing program that I found online, I found that my compass was not communicating with the Arduino, as I suspected. I then had a hunch that maybe one of the soldering points wasn't getting a good connection, particularly with the SDA and SCL connections. I tested each one and found that my SCL pin did not have a good connection. I went and re-soldered the pin and now I am getting good readings from my compass. And to think that was the problem. There goes 5 hours of my life...

Go Up