25. Chapter Attitude Sensor MPU6050
In this chapter, we will learn about a MPU6050 Attitude sensor, which integrates an Accelerometer and Gyroscope.
25.1. Project Read a MPU6050 Sensor Module
In this project, we will read Acceleration and Gyroscope Data of the MPU6050 Sensor.
25.1.1. Component List
|
Jumper Wires x4 |
MPU6050 x1 |
|
25.1.2. Component knowledge
25.1.2.1. 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.
The port description of the MPU6050 Module is as follows:
Pin name |
Pin number |
Description |
|---|---|---|
VCC |
1 |
Positive pole of power supply with voltage 5V |
GND |
2 |
Negative pole of power supply |
SCL |
3 |
I2C communication clock pin |
SDA |
4 |
I2C communication data 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 the MPU6050 datasheet.
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.
25.1.3. Circuit
Note that the power supply voltage for MPU6050 module is 5V in this circuit.
Schematic diagram
|
Hardware connection. If you need any support,please feel free to contact us via:
|
25.1.4. Code
25.1.4.1. C Code MPU6050RAW
First, observe the project result, and then learn about the code in detail.
Hint
If you have any concerns, please contact us via: support@freenove.com
Use
cdcommand to enter 25.1.1_MPU6050RAW directory of C code.
$ cd ~/Freenove_Kit/Code/C_Code/25.1.1_MPU6050
Use following command to compile
MPU6050RAW.c,MPU6050.cppandI2Cdev.cpp, and generate executable fileMPU6050RAW.
$ gcc MPU6050RAW.cpp MPU6050.cpp I2Cdev.cpp -o MPU6050RAW
Then run the generated file “MPU6050RAW”.
$ sudo ./MPU6050RAW
After the program is executed, the Terminal will display active accelerometer and gyroscope data of the MPU6050, as well as the conversion to gravity acceleration and angular velocity as units of data. As shown in the following figure:
The following is the program code:
1/**********************************************************************
2* Filename : MPU6050RAW.c
3* Description : Read data of MPU6050
4* Author : www.freenove.com
5* modification: 2019/12/27
6**********************************************************************/
7#include <stdio.h>
8#include <stdint.h>
9#include <unistd.h>
10#include "I2Cdev.h"
11#include "MPU6050.h"
12
13MPU6050 accelgyro; //creat MPU6050 class object
14
15int16_t ax, ay, az; //store acceleration data
16int16_t gx, gy, gz; //store gyroscope data
17
18void setup() {
19 // initialize device
20 printf("Initializing I2C devices...\n");
21 accelgyro.initialize(); //initialize MPU6050
22
23 // verify connection
24 printf("Testing device connections...\n");
25 printf(accelgyro.testConnection() ? "MPU6050 connection successful\n" : "MPU6050 connection failed\n");
26}
27
28void loop() {
29 // read accel/gyro values of MPU6050
30 accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
31 // display accel/gyro x/y/z values
32 printf("a/g: %6hd %6hd %6hd %6hd %6hd %6hd\n",ax,ay,az,gx,gy,gz);
33 printf("a/g: %.2f g %.2f g %.2f g %.2f d/s %.2f d/s %.2f d/s \n",(float)ax/16384,(float)ay/16384,(float)az/16384,
34 (float)gx/131,(float)gy/131,(float)gz/131);
35}
36
37int main()
38{
39 setup();
40 while(1){
41 loop();
42 }
43 return 0;
44}
Two library files “MPU6050.h” and “I2Cdev.h” are used in the code and will be compiled with others. Class MPU6050 is used to operate the MPU6050 Sensor. When used, first it initiates an object.
1MPU6050 accelgyro;
In the setup function, the MPU6050 is initialized and the result of the initialization will be tested.
1void setup() {
2 // initialize device
3 printf("Initializing I2C devices...\n");
4 accelgyro.initialize(); //initialize MPU6050
5
6 // verify connection
7 printf("Testing device connections...\n");
8 printf(accelgyro.testConnection() ? "MPU6050 connection successful\n" : "MPU6050 connection failed\n");
9}
In the loop function, read the original data of MPU6050, display them and then convert the original data into the corresponding acceleration and angular velocity values, then display the converted data out.
1void loop() {
2 // read accel/gyro values of MPU6050
3 accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
4 // display accel/gyro x/y/z values
5 printf("a/g: %6hd %6hd %6hd %6hd %6hd %6hd\n",ax,ay,az,gx,gy,gz);
6 printf("a/g: %.2f g %.2f g %.2f g %.2f d/s %.2f d/s %.2f d/s \n",(float)ax/16384,(float)ay/16384,(float)az/16384,
7 (float)gx/131,(float)gy/131,(float)gz/131);
8}
Finally, the main functions, called setup function and loop function respectively.
1int main()
2{
3 setup();
4 while(1){
5 loop();
6 }
7 return 0;
8}
About class MPU6050:
- Class MPU6050
This is a class library used to operate the MPU6050, which can directly read and set the MPU6050. Here are its functions:
MPU6050()/MPU6050(uint8_t address);
Constructor. The parameter is I2C address, and the default I2C address is 0x68.
void initialize();
Initialization function, used to wake up MPU6050. Range of accelerometer is ±2g and range of gyroscope is ±250 degrees/sec.
void getMotion6(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz);
Get the original data of accelerometer and gyroscope.
int16_t getTemperature();
Get the original temperature data of MPU6050.
For details about more relevant member functions, pleases refer to MPU6050.h or visit: https://github.com/jrowberg/i2cdevlib



