Interfacing HMC5883L compass module with Arduino

I'm using an HMC5883l compass module to find the angle at which my module is pointed from magnetic North. I found this code as an example with "HMC5883L_Simple.h" library.

#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>

// Create a compass
HMC5883L_Simple Compass;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
    
  
  Compass.SetDeclination(1, 56, 'E');  
  
  Compass.SetSamplingMode(COMPASS_CONTINUOUS);

  Compass.SetScale(COMPASS_SCALE_088);

  Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
  
}

void loop()
{
   float heading = Compass.GetHeadingDegrees();
   
   Serial.print("Heading: \t");
   Serial.println( heading );   
   delay(1000);
}

And the results I'm getting right now are like this.

Heading: 294.57
Heading: 294.44
Heading: 286.88

etc....

Is it the angle at which my module is pointed from magnetic North in degrees? If not, How can find the pointing angle?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .

Turn the module until you get a reading of 0 deg and then look at the module, there should be some markings indicating magnetic orientation, the relevant mark should be pointing MAGNETIC North.

Tom... :slight_smile:

Thanks for your reply, But I'm not getting Zero anywhere. So is there any calibration kind of thing that I have to do before using it?

Here is a page that describes calibrating the HMC5883L. Calibration needs to be done to compensate for local magnetic fields (nearby ferrous metal for instance).