Motion Processing Unit – MPU9250 for RTPT [using Filters,auto – calibration ]

Main Page – Real time planet tracking system

Motion Processing is an important concept to know if you want to interact with real time data you should be able to interact with motion parameters such as Linear acceleration, Angular acceleration, Magnetic North of the planet with a reference point on the object.


MPU9250 has an accelerometer ,gyroscope and a magnetometer.The things that we can get from a processed MPU9250’s raw readings are:

  1. Yaw Angle
  2. Pitch Angle
  3. Roll Angle


I will only deal with Yaw here in this post.

Overview of working of MPU9250

This MPU has 16bit registers for each sensor that is accelerometer ,gyroscope ,magnetometer.They store the data from the sensors and the data is transmitted over I2C Serially.

1.We take the data serially 8 bit at a time and then concatenate them to form 16bit again.

Look at the following snippet from kriswiners code:


2.The data is received is calibrated according to the users environment.The calibration of magnetometer is only required because the MAGNETIC DECLINATION changes with location the value of calibrated magbias is according to user environment.There are two variables that has to calibrated : 1. yaw 2.magbias

The below shows yaw calibration for a specific magnetic declination (at Potheri,Chennai ,India).This data can be obtained from different sites :                                                                         


See the below snippet [the given snippet the data for calibration of magbias comes from another function (magcalMPU9250(float * dest1, float * dest2))  :


Auto Calibration

This is one of the most simple and important part of the MPU code.The function magcalMPU9250(float * dest1, float * dest2)  Reads magnetometer reading [whe you move it in “8” shape ] it stores the maximum and minimum readings and takes its average . This is how magbias gets auto calibrated .The following is the code snippet :

for more details please go to the source:

Permanent Calibration for a Particular Place

If you dont want to waste your time everytime by moving the device in “8” shape then you just have to note the average values of magbias[] after it has been calculated and use this snippet :


The values 470,120,125 is fixed for my location So after doing this there is no need of void magcalMPU9250(float * dest1, float * dest2)  function so you can either comment it or remove it also dont forget to comment the calling statement as well :see the snippet below-


3. The calibrated data is usually in raw for resolved in x component ,y component ,z component.

4.As the data consists of a lot of noise we use certain Filters as (Madgwick/Mahony/Kalman) to fuse reading from accelerometer ,gyroscope and magnetometer and convert them to Quaternion.See the snippet for filter :


5.As the data changes very rapidly we take the data for some amout of time and take its average .We do it in following snippet of the code (its 50ms for the below code)


6.Finally we get the readings in form of yaw pitch and roll from the quaternions.


Getting readings from MPU9250 with Arduino  Mega 2560

We have lots of libraries for MPU9250.One of the popular one is given by kriswiner

MPU-9250 Arduino Library by Kriswiner

Once you save the library to your arduino folder you are ready to go.Open the example  MPU9250BasicAHRS.ino  Also have this  setup ready :

Hardware setup:
MPU9250 Breakout ——— Arduino
VIN ———————- 5V
SDA ———————– SDA (Pin 20)
SCL ———————– SCL (Pin 21)
GND ———————- GND

These wires shouldnt be very long as I2C connections dont work well for long wires.After connecting the wires

Now Clean up the  MPU9250BasicAHRS code ,it has a LCD display  code in it but we dont need it so keep removing unnecessary lines.

Also I have added a part of auto calibration code.

Here is the modified code without unnecessary code lines and auto calibration part : Github

Now upload the code to your arduino and do the connections stated above. Open the Serial Terminal and change the baud rate to 115200 . You should see this output.


If you see this .


This means there is definitely some loose connection or wiring problem ( or in the worst case mpu/arduino fault ) try to rectify it and proceed …

If everything goes and you see “MPU is online” and “Mag Calibration: Wave device in a figure eight until done!” then everything is working and you should turn your mpu in “8” shaped motion for some time…..This is the Auto Calibration part….After some time you should be able to get readings of YAW,PITCH and ROLL as below…


Awesome! now you have the data…and you can play with real time motion 😀

Auto Azimuth(Yaw) calibration of RTPT using P- controller

We first convert yaw to from (-180 to +180) to (0 to 360) by doing

Then we just find the error in yaw using a simple Proportional controller and then add the error back to yaw and then do the servo mappings with the new yaw. :


This completes my RTPT project.Hope you have learned new things from it. 🙂

The final code is in the main RTPT page.

Thank you 😀

RTPT (Real Time Planet Tracking System and Trajectory Prediction) Copyright © 2016 Shubham Paul , Samhita Ganguly ,Rohit Kumar  GNU GPL3+

This entry was posted in Embedded Software, Planet Tracking System. Bookmark the permalink.

1 Response to Motion Processing Unit – MPU9250 for RTPT [using Filters,auto – calibration ]

  1. Pingback: Real-Time Planet Tracking System & Trajectory Prediction |

Leave a Reply

Your email address will not be published. Required fields are marked *

3172 page views, 39 database queries in 0.582 seconds.