Chapter 26 Attitude Sensor MPU6050
In this chapter, we will learn about a MPU6050 attitude sensor which integrates an accelerometer and gyroscope.
Project 26.1 Read a MPU6050 Sensor Module
In this project, we will read acceleration and gyroscope data of the MPU6050 sensor
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
|
Breadboard x1
|
||
LED x1
|
MPU6050 x1 |
|
Component knowledge
MPU6050
MPU6050 sensor module is a complete 6-axis motion tracking device. It combines a 3-axis gyroscope, a 3-axis accelerometer and a DMP (Digital Motion Processor) all in a small package. The settings of the accelerometer and gyroscope of MPU6050 can be changed. A precision wide range digital temperature sensor is also integrated to compensate data readings for changes in temperature, and temperature values can also be read. The MPU6050 Module follows the I2C communication protocol and the default address is 0x68.
MPU6050 is widely used to assist with balancing vehicles, robots and aircraft, mobile phones and other products which require stability to control stability and attitude or which need to sense same.
The port description of the MPU6050 module is as follows:
Pin name |
Pin number |
Description |
|---|---|---|
VCC |
1 |
Positive pole of power supply with voltage 3.3V/5V |
GND |
2 |
Negative pole of power supply |
SCL |
3 |
I2C communication clock pin |
SDA |
4 |
I2C communication clock pin |
XDA |
5 |
I2C host data pin which can be connected to other devices. |
XCL |
6 |
I2C host clock pin which can be connected to other devices. |
AD0 |
7 |
I2C address bit control pin. Low level: the device address is 0x68 High level: the device address is 0x69 |
INT |
8 |
Output interrupt pin |
For more detail, please refer to datasheet.
Circuit
Note that the power supply voltage for MPU6050 module is 3.3V in the circuit.
Schematic diagram |
|
Hardware connection If you need any support, please feel free to contact us via: support@freenove.com
|
Sketch
How to install the library
In this project, we will read the acceleration data and gyroscope data of MPU6050, and print them out.
We use the third party library MPU6050_tockn . If you haven’t installed it yet, please do so before learning.
The steps to add third-party Libraries are as follows: open arduino -> Sketch -> Include library -> Manage libraries.
Enter “ MPU6050_tockn “ in the search bar and select “ MPU6050_tockn “ for installation.
Sketch_26.1_Acceleration_Detection
Download the code to ESP32-WROVER, open the serial port monitor, set the baud rate to 115200 and you can use it to measure the distance between the ultrasonic module and the object. As shown in the following figure:
The following is the program code:
1/**********************************************************************
2 Filename : Acceleration detection
3 Description : Read the MPU6050 data and print it out through the serial port
4 Auther : www.freenove.com
5 Modification: 2024/06/19
6**********************************************************************/
7#include <MPU6050_tockn.h>
8#include <Wire.h>
9
10#define SDA 13
11#define SCL 14
12
13MPU6050 mpu6050(Wire);//Attach the IIC
14int16_t ax,ay,az;//define acceleration values of 3 axes
15int16_t gx,gy,gz;//define variables to save the values in 3 axes of gyroscope
16
17long timer = 0;
18
19void setup() {
20 Serial.begin(115200);
21 Wire.begin(SDA, SCL); //attach the IIC pin
22 mpu6050.begin(); //initialize the MPU6050
23 mpu6050.calcGyroOffsets(true); //get the offsets value
24}
25
26void loop() {
27 if(millis() - timer > 1000){ //each second printf the data
28 mpu6050.update(); //update the MPU6050
29 getMotion6(); //gain the values of Acceleration and Gyroscope value
30 Serial.print("\na/g:\t");
31 Serial.print(ax); Serial.print("\t");
32 Serial.print(ay); Serial.print("\t");
33 Serial.print(az); Serial.print("\t");
34 Serial.print(gx); Serial.print("\t\t");
35 Serial.print(gy); Serial.print("\t\t");
36 Serial.println(gz);
37 Serial.print("a/g:\t");
38 Serial.print((float)ax / 16384); Serial.print("g\t");
39 Serial.print((float)ay / 16384); Serial.print("g\t");
40 Serial.print((float)az / 16384); Serial.print("g\t");
41 Serial.print((float)gx / 131); Serial.print("d/s \t");
42 Serial.print((float)gy / 131); Serial.print("d/s \t");
43 Serial.print((float)gz / 131); Serial.print("d/s \n");
44 timer = millis();
45 }
46}
47void getMotion6(void){
48 ax=mpu6050.getRawAccX();//gain the values of X axis acceleration raw data
49 ay=mpu6050.getRawAccY();//gain the values of Y axis acceleration raw data
50 az=mpu6050.getRawAccZ();//gain the values of Z axis acceleration raw data
51 gx=mpu6050.getRawGyroX();//gain the values of X axis Gyroscope raw data
52 gy=mpu6050.getRawGyroX();//gain the values of Y axis Gyroscope raw data
53 gz=mpu6050.getRawGyroX();//gain the values of Z axis Gyroscope raw data
54}
Two library files “ MPU6050_tockn.h “ and “ Wire.h “ are used in the code and will be compiled with others. Class MPU6050 is used to operate the MPU6050. When using it, please instantiate an object first.
1MPU6050 mpu6050(Wire);//Attach the IIC
In the setup function, IIC and MPU6050 are initialized and the offset difference of MPU6050 is obtained.
1void setup() {
2 Serial.begin(115200);
3 Wire.begin(SDA, SCL); //attach the IIC pin
4 mpu6050.begin(); //initialize the MPU6050
5 mpu6050.calcGyroOffsets(true); //get the offsets value
6}
The getMotion6 function is used to obtain the x, y, z axis acceleration raw data and the Gyroscope raw data.
1void getMotion6(void){
2 ax=mpu6050.getRawAccX();//gain the values of X axis acceleration raw data
3 ay=mpu6050.getRawAccY();//gain the values of Y axis acceleration raw data
4 az=mpu6050.getRawAccZ();//gain the values of Z axis acceleration raw data
5 gx=mpu6050.getRawGyroX();//gain the values of X axis Gyroscope raw data
6 gy=mpu6050.getRawGyroX();//gain the values of Y axis Gyroscope raw data
7 gz=mpu6050.getRawGyroX();//gain the values of Z axis Gyroscope raw data
8}
Finally, the original data of the gyroscope is updated and acquired every second, and the original data, the processed acceleration and angular velocity data are printed out through the serial port.
1void loop() {
2 if(millis() - timer > 1000){ //each second printf the data
3 mpu6050.update(); //update the MPU6050
4 getMotion6(); //gain the values of Acceleration and Gyroscope value
5 Serial.print("\na/g:\t");
6 Serial.print(ax); Serial.print("\t");
7 Serial.print(ay); Serial.print("\t");
8 Serial.print(az); Serial.print("\t");
9 Serial.print(gx); Serial.print("\t\t");
10 Serial.print(gy); Serial.print("\t\t");
11 Serial.println(gz);
12 Serial.print("a/g:\t");
13 Serial.print((float)ax / 16384); Serial.print("g\t");
14 Serial.print((float)ay / 16384); Serial.print("g\t");
15 Serial.print((float)az / 16384); Serial.print("g\t");
16 Serial.print((float)gx / 131); Serial.print("d/s \t");
17 Serial.print((float)gy / 131); Serial.print("d/s \t");
18 Serial.print((float)gz / 131); Serial.print("d/s \n");
19 timer = millis();
20 }
21}
Reference
- Class MPU6050
This is a class library used to operate MPU6050, which can directly read and set MPU6050. Here are some member functions:
MPU6050 mpu6050(Wire): Associate MPU6050 with IIC.
begin(): Initialize the MPU6050.
calcGyroOffsets(true): If the parameter is true, get the gyro offset and automatically correct the offset. If the parameter is false, the offset value is not obtained and the offset is not corrected.
getRawAccX(): Gain the values of X axis acceleration raw data.
getRawAccY(): Gain the values of Y axis acceleration raw data.
getRawAccZ(): Gain the values of Z axis acceleration raw data.
getRawGyroX(): Gain the values of X axis Gyroscope raw data.
getRawGyroY(): Gain the values of Y axis Gyroscope raw data.
getRawGyroZ(): gain the values of Z axis Gyroscope raw data.
getTemp(): Gain the values of MPU6050’temperature data.
update(): Update the MPU6050. If the updated function is not used, the IIC will not be able to retrieve the new data.






