HMC5883l heelp please :)

Hi guys!

I started learning arduino preatty recently. I have the sensor HMC5883L for measure the magnetic field, however, I can’t make him function.

Can somebody help me please?
I’m using this code, but the values that I get are always the same.

#include <Wire.h>
#include <HMC5883L.h>

HMC5883L compass;

int error = 0;
int ledpin = 13;

void setup()
{
Serial.begin(9600);
pinMode(ledpin, OUTPUT);
Serial.println(“Starting the I2C interface.”);
Wire.begin(); // Start the I2C interface.

Serial.println(“Constructing new HMC5883L”);
compass = HMC5883L(); // Construct a new HMC5883 compass.

Serial.println(“Setting scale to +/- 1.3 Ga”);
error = compass.SetScale(1.3); // Set the scale of the compass.
if(error != 0) // If there is an error, print it out.
Serial.println(compass.GetErrorText(error));

Serial.println(“Setting measurement mode to continous.”);
error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
if(error != 0) // If there is an error, print it out.
Serial.println(compass.GetErrorText(error));
}

void loop()
{

MagnetometerRaw raw = compass.ReadRawAxis();

MagnetometerScaled scaled = compass.ReadScaledAxis();

int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

float heading = atan2(scaled.YAxis, scaled.XAxis);

float declinationAngle = 0.3764;
heading += declinationAngle;

if(heading < 0)
heading += 2*PI;

if(heading > 2PI)
heading -= 2
PI;

float headingDegrees = heading * 180/M_PI;

Output(raw, scaled, heading, headingDegrees);

}

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

1 Like

There are "fake" HMC5883 modules out there. The "fakes" work the same as a HMC5883 but have a different I2C address. This page explains how to tell the difference.

Where did you get that library. Is that code from the library examples? This line, in setup(), looks suspicious:

 Serial.println("Constructing new HMC5883L");
  compass = HMC5883L(); // Construct a new HMC5883 compass.

You already constructed an instance of the HMC5883L class with:

HMC5883L compass;

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Please edit your post to add code tags, as described in "How to use this forum".

groundFungus:
There are "fake" HMC5883 modules out there. The "fakes" work the same as a HMC5883 but have a different I2C address. This page explains how to tell the difference.

Where did you get that library. Is that code from the library examples? This line, in setup(), looks suspicious:

 Serial.println("Constructing new HMC5883L");

compass = HMC5883L(); // Construct a new HMC5883 compass.



You already constructed an instance of the HMC5883L class with:


HMC5883L compass;




Read the [how to use this forum-please read sticky](https://forum.arduino.cc/index.php/topic,148850.0.html) to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Thanks!!
It was actually the fake one, I had no idea.