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.
Where to Buy MPU6050:
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.