Find the heading using magnetometer BMX055

Hi guys,
I have played with the BMX055 sensor from Bosch. I want to find the heading of the system using the integrated magnetometer but I can not find the heading of the system.
I have tried using below equation but it did not work.

Heading = atan2(Y_magnetometer, X_magnetometer) * 180 / PI;

the code for reading the magnetometer data:

// Addr_Mag 0x13 
void BMX055_Mag()
{
  int data[8];
  for (int i = 0; i < 8; i++)
  {
    Wire.beginTransmission(Addr_Mag);
    Wire.write((0x42 + i));    // Select data register
    Wire.endTransmission();
    Wire.requestFrom(Addr_Mag, 1);    // Request 1 byte of data
    // Read 6 bytes of data
    // xMag lsb, xMag msb, yMag lsb, yMag msb, zMag lsb, zMag msb
    if (Wire.available() == 1)
      data[i] = Wire.read();
  }
  // Convert the data
  xMag = ((data[1] <<8) | data[0])>>3;
  if (xMag > 4095)  xMag -= 8192;
  yMag = ((data[3] <<8) | data[2])>>3;
  if (yMag > 4095)  yMag -= 8192;
  zMag = ((data[5] <<8) | data[4])>>1;
  if (zMag > 16383)  zMag -= 32768;
}

My question is:

  1. Is there any problem with my provided code?
  2. How to calculate the heading using a magnetometer?
    Thank you for your help,

Magnetometers do not work "out of the box" and need to be calibrated. The most comprehensive tutorial on calibration is Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers, but you can find simpler approaches on line that will give approximate directions.

If you want help with code, post ALL of it. Snippets are useless.

Thank you for your reply,
That is a helpful link. I will read through the suggestion link and calibrate the magnetometer. I will post the result when I succeed.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.