DEV Community

Cover image for Measure Angles Easily with MPU6050 and ESP32: Part 1

Posted on

Measure Angles Easily with MPU6050 and ESP32: Part 1

Using the MPU6050 to find angles can be a tricky business, requiring sophisticated forms of sensor fusion and offset calculation. Many tutorials online demonstrate how to calculate the angle with simple sets of formulas, which frustratingly do not work in practice, as you may have found out. There is a convenient library i2cdevlib that utilizes sensor fusion and does the heavy work for us to give us accurate orientation values in both static and dynamic sensor applications. We will walk through how to set that up in this tutorial using PlatformIO IDE extension in VSCode with the Arduino framework. By the end of it, you will have code that provides accurate sensor values without needing to understand the complicated theory of sensor fusion algorithms.

Before reading the remainder, be sure to subscribe and support the channel if you have not!




Step 1-) Setup Physical Connection

Image description

Set up your connection as shown in the diagram with 4 jumper wires. You can buy the ESP32 and the MPU6050 with the links below; they come pre-soldered which allows you to save time and money.



Step 2-) VSCode Project Setup

We use PlatformIO to set up our project in this tutorial (the same steps can be followed with Arduino IDE essentially). PlatformIO is a user-friendly and extensible integrated development environment with a set of professional development instruments, providing modern and powerful features to speed up yet simplify the creation and delivery of embedded products. In my opinion, it is much more easy to work with and visually appealing than Arduino IDE, also you can utilize all of the great extensions in VSCode, particularly GH CoPilot which some people find essential these days.
You can get started with PlatformIO by downloading the extension, more details here, and in the Youtube video above.
Once you have the extension you can set up a new project as shown here; select the name and board, in this case ESP32 Dev Module is the board. Of course, make sure your device is plugged into the computer so that VSCode can find the device.

Image description

Once you have that you want to download the zipfile of the repository that contains the code here.

Unzip the contents and move both the Arduino/MPu6050 and Arduino/I2Cdev code into your lib directory in PlatformIO. We do not need all of the source code in the repo, in fact, most of it does not pertain to our use case! Our project structure should look as follows:

Image description

Step 3-) Code Setup

Now that we have the library in the needed directory we can go ahead and copy one of their examples and do some slight modifications… very convenient of them to include this.

Image description

Simply copy the contents from the MPU6050_DMP6.ino into your main.cpp file in the src folder of your project. Change the default baud rate to 9600 and upload the code to the board. You should be prompted to enter any key to proceed with DMP. The first time you run the code you will need to replace the offsets it gives you shown below. Subtracting the offsets is needed to have more accurate angle values, all sensors come with some inherent bias and need to be calibrated. Thankfully the library does that for us we just need to substitute the offsets into the code accordingly. In my case, it gave me the offsets shown below:

Image description

If you are interested in the simple theory and math behind calculating offsets for the MPU6050, you can watch this past tutorial:

I substitute the offsets as (this section is already in the code you just nred to replace the numbers):

// supply your own gyro offsets here, scaled for min sensitivity
    mpu.setZAccelOffset(2048); // 1688 factory default for my test chip
Enter fullscreen mode Exit fullscreen mode

We can now rerun the code to get more accurate angle values. The angle values will display as ypr (yaw, pitch, and roll), you can add a delay as needed. It is best to calibrate the sensor on a tabletop utilizing a breadboard or calibration block so the accelerometer stays as flat as possible. All angles will be relative to the initial position, so keep that in mind, and they should be about 0 at the beginning until you start to move your sensor.
Once you have that, you have successfully utilized the i2cdev library to get accurate angle measurements for your application. The specifics of how the library utilizes sensor fusion to get accurate results are beyond the scope of this video. There are engineers dedicating their lives to sensor calibration and fusion, so do not be discouraged if you do not understand the underlying processes, there is a lot of theory involved to get accurate results.
Note that the library has some shortcomings, but compared to the rest of the techniques on the internet this is a great way to satisfy many application needs while keeping it simple and not having to understand the math involved with angle measurement. I leave it up to the reader to further indulge in the topic if he/she is interested!


Hope you stay tuned for part 2 where we will show how to utilize a graphic representation using processing and the code to allow you to visualize the MPU6050 angles in free space. It will be pretty cool and allow you to verify the results that you retrieved in this part. Let me know if you have any questions I will try my best to answer them! Preferably, you can comment on Youtube so everyone can see our discussion. Once again do not forget to subscribe and like the video, we are approaching 1, 000 subscribers soon, and would be happy to have you along the journey! Thanks for reading ;)

Top comments (0)