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!




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:


MPU Library For MicroPython:


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:


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!


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

How to Use ADS1115 with the Raspberry Pi (Part 1)

How to Use ADS1115 with the Raspberry Pi (Part 1)

Discover how to expand your Raspberry Pi projects by integrating the ADS1115 ADC for precise analog signal reading....

How to Install Pip Packages in AWS Lambda Using Docker and ECR

How to Install Pip Packages in AWS Lambda Using Docker and ECR

Learn how to streamline AWS Lambda deployments by using Docker and Amazon Elastic Container Registry (ECR) to package...

Create Tabular Product Descriptions on Your Shopify Store

Create Tabular Product Descriptions on Your Shopify Store

Enhance your Shopify store's product pages with our comprehensive guide on implementing tabular descriptions. Learn how to add a...

SSH Into Raspberry Pi with Tailscale VPN

SSH Into Raspberry Pi with Tailscale VPN

Effortlessly access and manage your Raspberry Pi from anywhere using Tailscale's secure mesh VPN.

Send Email with Lua and the ESP32

Send Email with Lua and the ESP32

In this tutorial, we delve into sending emails with the ESP32-S3 using Lua, focusing on the Xedge IDE's built-in SMTP...

How to Code with Lua on ESP32 with XEdge32

How to Code with Lua on ESP32 with XEdge32

Learn how to set up Xedge32 and start coding on the ESP32-S3 with Lua programming!

Stream Audio From Raspberry Pi to Local Computer

Stream Audio From Raspberry Pi to Local Computer

Discover the simplicity of streaming live audio directly from a USB microphone connected to your Raspberry Pi to...

SSH Raspberry Pi via Cell Phone

SSH Raspberry Pi via Cell Phone

This beginner-friendly guide will walk you through remotely controlling your Raspberry Pi using SSH through your cell phone.

Remotely Control Raspberry Pi via SSH from External Network

Remotely Control Raspberry Pi via SSH from External Network

Learn how to SSH into your Raspberry Pi from any network. This is critical in IoT since you can control...

Stream Video from Raspberry Pi Camera to YouTube Live

Stream Video from Raspberry Pi Camera to YouTube Live

Learn how to stream to YouTube from a Raspberry Pi Camera.

Back to blog

Leave a comment

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