Go Down

Topic: Issues with the HMC5883L Magnetometer Compass (Read 578 times) 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//
#include<Serial.h>

HMC5883L compass;

int error=0;

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

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

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("Application Started");
  compass = HMC5883L();
 
  error = compass.SetScale(1.3);
  if(error != 0)
    Serial.println(compass.GetErrorText(error));
 
  error = compass.SetMeasurementMode(Measurement_Continuous);
  if(error != 0)
    Serial.println(compass.GetErrorText(error));
}

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);
 
  delay(1000);
}

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