How to Calibrate MPU6050

Calibrating a sensor is an integral part of achieving accurate results in practice. The tutorial will demonstrate how to calibrate the MPU6050 sensor using MicroPython and the Raspberry Pi Pico W. However the same technique can be applied to any setup, be it Arduino, or any other MicroController. We are calibrating because the sensor values the MPU6050 gives us are not entirely accurate.  We proceed with the calibration shown in this tutorial to achieve more accurate representations of the sensor values. If you have not already, here is where you can purchase the MPU6050 pre-soldered!

- Use Discount Code SHILLEHTEK for 30% Off!

Buy MPU6050 on ShillehTek

- Or buy it on Amazon

Buy MPU6050 on Amazon

The MPU6050 is a highly capable 6-axis motion tracking device, combining a 3-axis gyroscope and a 3-axis accelerometer on a single chip. It provides real-time motion data, which is essential for various applications in robotics, motion sensing, and interactive technology. Its versatility and functionality make it an invaluable component for hobbyists and professionals alike working on motion-related projects.

Recognizing the technical challenges that can come with micro-soldering delicate components, our MPU6050 modules at ShillehTek come pre-soldered. This not only saves you time and effort but also ensures that you get reliable and consistent performance right out of the box. Whether you're building your first project or a seasoned creator, our pre-soldered MPU6050 modules provide a convenient and hassle-free solution, letting you focus on the creative aspects of your projects.

Also, be sure to subscribe and support the channel if you have not!

Subscribe:

Youtube

Support:

https://www.buymeacoffee.com/mmshilleh

Part 1 - Calibrating the Gyroscopic Values AKA Angular Acceleration:

Step 1a-) Keep your sensor Plugged in, and on, and make sure it's not moving

You need to have a steady state to get accurate calibration values. If you move during calibration you will risk skewing the data. The aim of calibrating is to get the inherent error in the sensor readings; this is most easily done when the sensor is not moving because we know the angular acceleration values Gx, Gy, and Gz should be expectedly zero degrees per second in this situation. However, the truth is, if you are laying your sensor on the table at this moment without moving it, you will see you are probably getting non-zero values for your angular acceleration. We need to calculate three offset values that bring the resting sensor readings closer to zero. This is gyroscopic calibration in a nutshell. 

Part 1b-) Utilize the following code from my GitHub page to retrieve the 3 offsets

Gyro Calibration Code Python:

  • https://github.com/shillehbean/youtube-channel/blob/main/gyro_calibration_mpu6050.py

MPU Library For MicroPython:

  • https://github.com/shillehbean/youtube-channel/blob/main/imu.py

If your MPU6050 is connected just fine in Thonny with your Raspberry Pi Pico (assuming you have the library file saved to your Raspberry Pi), this code works right away, it will return three offsets that you can subtract from your sensor readings. In practice, you can utilize the offset sensor readings for measurement which should be more accurate than the measurements you took without the offset. 

However, for those of you who aren't utilizing MicroPython, you may be wondering what the idea of the code is. The idea is that I am allowing the sensor to take a running sum (for a given time) of the gyro readings in all three degrees of angular acceleration. I know the true value of the sensor should be zero for all readings of angular acceleration, however, the sensor never gives me the true value; it gives me the true value plus some offset. Thus because we know,

 true_sensor_value = measured_sensor_value - offset

and when we are calibrating in a non-moving state the true_sensor_value is zero, the value we are measuring is just the offset. So we are taking a running sum of the offset and then taking the average... that is all we are doing!

Part 1c-) Add the offsets to your readings, calibration is done!

I calculated the offset to be -2.145951, 1.384315, and -0.5084849. Yours should be a little different or even a lot different. I subtracted them as follows

Part 2 - Calibrating the Linear Acceleration Values:

Linear calibration is a little more complicated; we instead have to perform 9 calibrations, 3 for each axis of linear acceleration. In the linear acceleration case, the offset we are calculating would be a line of best fit, where y = (m * x) + b. We are looking to get the m and the b values. To do this, we need to calibrate in 3 orientations, one where the axis of calibration is upwards against gravity (expected value is 1g), one where the axis is downwards towards gravity (expected value is -1g), and the other where the axis is perpendicular to gravity (expected value is 0g). The variable g is the acceleration due to gravity which is 9.81 meters per second squared.

This will give us many offset values on a 2D plane, where y is the offset value and x is the expected acceleration. With all of these points, we can find a line of best fit using a numerical technique called least squares regression. Numerical techniques are preferable on the Pico because it cannot store external libraries that calculate m and b for you in one line of code, so we need to do some math... sadly.

We are just going to calibrate the z-axis of linear acceleration in this tutorial. But the same process should be applied to the x-axis and y-axis. 

Step 2a-) Use the code from my GitHub page and follow the instructions in the code:

Acceleration Calibration Code Python:

  • https://github.com/shillehbean/youtube-channel/blob/main/linear_accel_calibration.py

Running this function for axis = 2 you will begin calibrating for the z-axis. The code will instruct you to do the following:

  • Orient the axis against gravity (upwards). For the z-axis this will look like this:

Wait the allotted time. Ideally, you want to use a calibration block when doing this but I just used my hand to orient it (not good practice but I didn't have a block). 

  • Orient the axis downwards (towards) gravity. For the z-axis this will look like this:

Once again wait the allotted time

  • Finally, orient it perpendicular to gravity. For the z-axis this will look like this:

Finally, you should wait again. Once this is done the program will give you an m (slope) and b (y-intercept) value, which is all you need to define a line. 

Step 2b-) Utilize the offset function in your readings:

To calculate the offset at each reading, all you are going to do is plug the value into the actuation of the line. In practice, let us say you calibrated the z-axis and you got m = 0.05 and b = 0.8g. If a sensor reading in practice is Az = 1g we can calculate the offset to be 

offset = 0.05 * 1g + 0.8g = 0.85g

We then subtract the offset from the sensor value. In your program this would resemble this:

*Note that your slope and y-intercept will be different and you have to do the same calibration process for the x-axis and y-axis.

Step 2c-) Finish calibration for all axes and subtract offsets in real time

Finally, you are finished and you can add all 6 offsets you calculate to the corresponding DOF!

Conclusion:

Hope you enjoyed the quick tutorial on how to easily calibrate the MPU6050 and you improved your sensor readings in real life! If it helped you in any way please feel free to subscribe and comment on my Youtube Channel. Looking forward to making more content, helping more people, and developing new technology in the IoT space. 

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Controlling a 5V Laser Diode with Raspberry Pi Pico W

Controlling a 5V Laser Diode with Raspberry Pi Pico W

Learn how to safely control high-power components like a ShillehTek 5V laser diode using the Raspberry Pi Pico W and...

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

How to Use L298N Motor Driver with Pico W

How to Use L298N Motor Driver with Pico W

Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.