Calibration of compass by using X,Y,Z Axiss

Hi to every one in the arduino forum.

I struggled with one problem for so many days.

I am using GY-88 , Arduino UNO.

my problem is calculating the compass value using the 3 Axis.

I am able to calculate the compass by TWO axis is very success full, by using atan2 function.

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

but how to calculate the compass values by 3 axis values.

my complete code and library is here,

// SDA PIN A4  // blue
// SCL PIN A5  // Green
// +3.3V   // red
// GND    // black

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

// Store our compass as a variable.
HMC5883L compass;
MPU6050 accelgyro;
// 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()
  accelgyro.setI2CBypassEnabled(true) ;
  // Initialize the serial port.

  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.0457;
  heading += declinationAngle;*/
  // Set declination angle on your location and fix heading
  // You can find your declination on:
  // (+) Positive or (-) for negative
  // For Bytom / Poland declination angle is 4'26E (positive)
  // Formula: (deg + (min / 60.0)) / (180 / M_PI);
  float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / M_PI);
  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);
  Serial.println(" ");

  // 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("Raw:\t");
   Serial.print("   ");   
   Serial.print("   ");   
   Serial.print("   \tScaled:\t");
   Serial.print("   ");   
   Serial.print("   ");   

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

please help me.

Thanks in advance. (126 KB)

but how to calculate the compass values by 3 axis values.

The compass values relative to what? What does a heading of 149 degrees mean when the compass is tilted?

The google search term you are looking for is Euler angles.

You normally use the dot product with 3-vectors for determining angle between vectors.

Also you probably need to look up projecting a vector to a plane, which can be done with
cross products.