# How to estimate yaw angle from magnetometer and gyroscope?

I'm using the MPU9250, it measures acceleration, magnetometer, gyro, etc. Complementary filter tutorial for this is here, however this tutorial does not have a magnetometer and I do, thus I thought I could eliminate the gyro drift. Seen here:

I've found this post already in which other users state that a magnetometer is required. However, they do not go into much detail (and because it is such an old post I didn't want to bump it).

My other complementary filters are calculated as such:

float angle_x = alpha*gyro_angle_x + (1.0 - alpha)*accel_angle_x;

float angle_y = alpha * gyro_angle_y + (1.0 - alpha) * accel_angle_y;

angle z = yaw

I have my local inclination angle which I have been previously told I need and I also have my magnetometer value.

My question is; How do I combine all my values into an equation for the complementary filter?

• robotics.stackexchange.com/a/19166/23148 please see this post :) sparkfun as far as i know has their own arduino implementation of the Madgwick filter so you dont have to use the adafruit library...assuming you’re using an arduino Aug 13, 2019 at 16:40
• @morbo can I use that filter along with my already implemented complementary filter? Aug 13, 2019 at 16:41
• It would replace your comp filter entirely. Aug 13, 2019 at 16:42
• I have seen that the Madgwick filter has better accuracy however I’ve also seen it’s harder to implement. I have a custom arduino sketch so that’s why everything is hard coded. Aug 13, 2019 at 16:43
• Its very easy to implement if you use any of the libraries from sparkfun or adafruit :) Aug 13, 2019 at 16:44