# How to estimate yaw angle from tri-axis accelerometer and gyroscope

I would like to estimate the yaw angle from accelerometer and gyroscope data. For roll and pitch estimate I've used the following trigonometric equations:

roll  = atan2(Ax,Az)  * RAD_TO_DEG


and a simpified version of the Kalman Filter to consider also angular rates. The roll and pitch estimates are accurate (accelerometer values need to be filtered in presence of chassis vibrations).

In order to get the Yaw angle I'm using the following equation:

yaw = atan2(Ax,Ay)  * RAD_TO_DEG;


but the it doesn't work. Do you have any advice?

Gyroscopes will only give you the rate of change of the yaw angle, not the absolute yaw angle.

Unless you plan to set the yaw angle initially and have it drift further and further into garbage values (as you integrate the rate of change), you'll need another sensor to provide periodic updates on your actual yaw. This could be a magnetometer (compass), or a GPS, or a pair of GPSes, or some other method of finding your heading, which would provide input to a correction algorithm (e.g. Sebastian Madgwick's filter).

• What about the Madgwick Filter? It can measure the yaw angle from a 6 dof IMU and it does not drift. Don't know how it works... Mar 30 '15 at 8:00
• My understanding is that the Madgwick Filter uses a magnetometer and an accelerometer to prevent drift in the gyroscope measurement.
– Ian
Mar 30 '15 at 12:37
• I've noticed that this filter runs also on a 6 dof (acc + gyro) and produces an estimate of the yaw angle. I'm using an MPU6050 and this library. Mar 30 '15 at 15:17
• I should have written "magnetometer and/or accelerometer". These sensors don't have inherent drift in them, so they can be fused with the gyro measurements to correct for drift there.
– Ian
Apr 2 '15 at 12:56

Assuming your vehicle is roughly horizontal to the ground, you won't be able get a good estimate of yaw from the accelerometer. Consider the nominal case: when your accelerometer is pointing straight down (Ax=0, Ay=0, Az=g) the reading will never change as you change yaw angle.

Normally, to get yaw angle vehicles use a magnometer (measure earth's magnetic field) or cameras and track the scene.

• Unfortunatly I threw away my magnetometer after having noticed that angular positions were not steady because of the magnetic filed of the motors. There are 4 brushless motors around the shield. That's why I'm trying to estimate roll, pitch and yaw Oct 5 '14 at 23:09
• Yup, that's a common problem. You can get around that to some extent by using very low bandwidth filters. Oct 5 '14 at 23:11
• I should have tried it. I had to filter accelerometer's data to suppress noise which was causing the same problem. Now I changed the circuit, the magnetometer has no place anymore. Is the last formula incorrect? Oct 5 '14 at 23:15
• I think it's correct (not sure though, would have to think about it more) but it is undefined for Ax=0 and Ay=0 and, since you are near hover both Ax and Ay are close to zero so the noise dominates. Oct 5 '14 at 23:58
• I can't even upvote you...not enough rep. I can't use a camera in this application. I think the only solution is a complete Kalman Filter... I need to check this link out Oct 6 '14 at 0:21

yaw can be measured by rate gyro and magnetometer not with accelerometer because accelerometer values depends on gravity component but on rotation in z axis only there is no change in gravity componets

• How does the 6 dof mpu 6050 estimates the yaw angle then? It has only an accelerometer and a gyroscope and produces a really good yaw angle estimate Mar 30 '15 at 7:56
• I don't really understand this, but from what I can tell you can fuse the accelerometer data with gyroscope data to get much more precise readings because the accelerometer data is meaningful when you rotate the device (gravity no longer looks downward, and you can use that to say "hey, I'm tilted this much"). In contrast if you are working with yaw, rotation about yaw doesn't affect the gravity vector so it isn't useful. Jun 5 '16 at 7:59