I am in the process of building a simple IMU for an autopilot. I am using Sparkfun, 9 Degrees of Freedom - Razor IMU. It is surprisingly user friendly.

I was able to get accelerometer, gyro and magnetometer data right out of the box. After calibration of the sensors and a simple complementary filter i have reliable roll and pitch values.

Now I have a problem with the yaw...

I found here http://www.pololu.com/file/download/...?file_id=0J434 the equation to get the yaw out of the magnetometer value:

$$X_h = X_m\cdot cos(Pitch) + Z_m \cdot sin(Pitch)$$ $$Y_h= X_m\cdot sin(Roll)\cdot sin(Pitch) + Y_m\cdot cos(Roll) - Z_m\cdot sin(Roll)\cdot cos(Pitch)$$ with Xm, Ym, and Zm are magnetic sensor measurements

$$Yaw = Heading = arctan( \frac{Y_h}{X_h} )$$

It sound be simple, but I can't do it. I hope that someone who already worked on IMUs can help me solve this problem.

Here (http://diydrones.com/forum/attachment/download?id=705844%3AUploadedFile%3A1327110) is an excel file with IMU data and equations.

The graph (in Excel file) shows results (first i rolled 90 degrees, then I pitched 90 degrees then I yawed 90 degrees).

Roll and pitch are calculated by the IMU (programmed with Arduino IDE), they are good.

Yaw is calculated in Excel using gyro (good but drift over time) and I also calculated Yaw using the equation above (green line) and it is totally wrong (it should follow the gyro line / purple).

Any help will be appreciated.

  • 1
    \$\begingroup\$ Without any electronic components wired/soldered/touched, this is simply a math and/or programming question. Off topic? FYI I just showed my kid a similar 9DOF yesterday, we used the ArduIMU firmware but didn't look behind the math (Google for more info). \$\endgroup\$
    – Ron J.
    Jul 13, 2013 at 12:36

1 Answer 1


It looks like the Gyro and the Magnetometer are slightly miscalibrated. In excel, it looks like they are about 135 degrees apart. Try changing your formula to be this instead:


Where Dcal is some calibration value. It looks like the data matches up fairly well when Dcal is about 135 degrees.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.