Compass Readings

The information printed on serial makes little sense to me. I was anticipating a 0*-360* reading on each of the 3 axis. I have run another code that was more basic with similar results. I did search for solutions, with no luck.

HMC5883L_Example.pde - Example sketch for integration with an HMC5883L triple axis magnetomerwe.
Copyright (C) 2011 Love Electronics (

This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <>.


// Reference the I2C Library
#include <Wire.h>
// Reference the I2C Library
#include <HMC5883L.h>

// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;

// Out setup routine, here we will configure the microcontroller and compass.
void setup()
  // Initialize the serial port.
  pinMode(22, OUTPUT);
  digitalWrite(22, LOW);

  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("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.

// Our main program loop.
void loop()
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  // Values are accessed like so:
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here:
  // Mine is: 2� 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.0215;
  heading += declinationAngle;
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 

  // Output the data via the serial port.
  Output(raw, scaled, heading, headingDegrees);

  // Normally we would delay the application by 66ms to allow the loop
  // to run at 15Hz (default bandwidth for the HMC5883L).
  // However since we have a long serial out (104ms at 9600) we will let
  // it run at its natural speed.
  // delay(66);

// Output the data down the serial port.
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");
[li]Constructing new HMC5883L
Setting scale to +/- 1.3 Ga
Entered scale was not valid, valid gauss values are: 0.88, 1.3, 1.9, 2.5, 4.0, 4.7, 5.6, 8.1
Setting measurement mode to continous.
Entered scale was not valid, valid gauss values are: 0.88, 1.3, 1.9, 2.5, 4.0, 4.7, 5.6, 8.1

Raw:	480   -926   -161   	Scaled:	441.60   -851.92   -148.12   	Heading:	5.21 Radians   	298.63 Degrees 
Raw:	261   -549   -246   	Scaled:	240.12   -505.08   -226.32   	Heading:	5.18 Radians   	296.66 Degrees 
Raw:	329   -1186   -102   	Scaled:	302.68   -1091.12   -93.84   	Heading:	5.00 Radians   	286.74 Degrees 
Raw:	139   -891   -148   	Scaled:	127.88   -819.72   -136.16   	Heading:	4.89 Radians   	280.10 Degrees

The first set of raw data was with the compass facing in a northerly direction, the second in an easterly direction, third was westerly, fourth was southerly. [/li]

The information printed on serial makes little sense to me.

But, you're not going to share it? Well, be that way. 8)

Compass Readings

You taped a compass to the Arduino? Or, did you connect some specific device to the Arduino, using some wires? If so, which one and how?

Useful background reading

I attached an edited print out of the serial info at the bottom of the code. The heading info doesn't reflect the actual orientation of the HMC5883L compass. The compass is mounted in close proximity of a 12 volt lead acid battery.