Problems with Heading of HMC5883L 3 Axis Compass Module

Hi All,

I am playing around with a 9DOF Sparkfun Sensor Stick SEN-10724 (SparkFun 9 Degrees of Freedom - Sensor Stick - SEN-10724 - SparkFun Electronics). I am using Arduino UNO. I am able to retrieve raw Magnetometer sensor data from the arduino chip using the code below. However I am not sure of how to convert the data to degrees.

  #include <Wire.h> //I2C Arduino Library

#define address 0x1E //0011110b, I2C 7bit address of HMC5883

void setup(){
  //Initialize Serial and I2C communications
  Serial.begin(9600);
  Wire.begin();
  
  //Put the HMC5883 IC into the correct operating mode
  Wire.beginTransmission(address); //open communication with HMC5883
  Wire.send(0x02); //select mode register
  Wire.send(0x00); //continuous measurement mode
  Wire.endTransmission();
}
void loop(){
  
  int x,y,z; //triple axis data

  //Tell the HMC5883L where to begin reading data
  Wire.beginTransmission(address);
  Wire.send(0x03); //select register 3, X MSB register
  Wire.endTransmission();
  
 
 //Read data from each axis, 2 registers per axis
  Wire.requestFrom(address, 6);
  if(6<=Wire.available()){
    x = Wire.receive()<<8; //X msb
    x |= Wire.receive(); //X lsb
    z = Wire.receive()<<8; //Z msb
    z |= Wire.receive(); //Z lsb
    y = Wire.receive()<<8; //Y msb
    y |= Wire.receive(); //Y lsb
  }
  
  //Print out values of each axis
  Serial.print("x: ");
  Serial.print(x);
  Serial.print("  y: ");
  Serial.print(y);
  Serial.print("  z: ");
  Serial.println(z);
  
  delay(250);
}
[\code]

If the compass is level, with the z axis vertical, the heading in radians is given by atan2(magy, magx), where magy and magx are the axial magnetometer readings (floating point values).

Google "freescale tilt compensated compass" for a more sophisticated procedure.

You will need to calibrate the magnetometer. Currently, the best procedure is described here.