MPU6050 Six-Axis Sensor
The MPU6050 is a 6-axis motion processing component launched by InvenSense. It integrates a 3-axis gyroscope and a 3-axis accelerometer, and includes an I2C interface for connecting to external magnetometers. Using its built-in Digital Motion Processor (DMP) hardware acceleration engine, it outputs complete 9-axis fusion data to the application end via the main I2C interface.
InvenSense provides a motion processing driver library based on the DMP, which greatly reduces the burden on the microcontroller for motion processing calculations and simplifies programming. This module is widely used in electronic products such as flight controllers and pedometers.
Module Source
Purchase Link: https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-19589090137.10.478736b4NYZQSb&id=609979451344
Baidu Netdisk Download Link: https://pan.baidu.com/s/1dNDqcp76L9QdM7iSZYfz_A
Password: 4eum
Specifications
Operating Voltage: 3-5V (The module includes an LDO)
Operating Current:5MA
Communication Method:I2C
Hardware Connection
- VCC: Connect to the 3.3V of the development board.
- GND: Connect to the GND of the development board.
- SCL (Clock Line): Connect to A5 (SCL) on the development board.
- SDA (Data Line): Connect to A4 (SDA) on the development board.
- INT (Optional): Interrupt line, can be connected to a digital pin on the development board for interrupt requests (used in this example).
Usage Method
Install Library
To read data from the MPU6050, we can use the MPU6050
library. You can install it through the Arduino IDE's library manager: open the Arduino IDE, go to Tools > Manage Libraries, then search for MPU6050
and install the Adafruit MPU6050
library.
Enter the code:
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 9, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
void setup(void) {
Serial.begin(115200);
while (!Serial) delay(10); // 等待串行连接开启
Serial.println("Adafruit MPU6050 test!");
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
// 设置所需的传感器范围
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.println("MPU6050 Ready!");
delay(100);
}
void loop() {
sensors_event_t a, g, temp;
/* 读取加速度、陀螺仪、温度数据 */
mpu.getEvent(&a, &g, &temp);
/* 显示这些数据 */
Serial.print("Acceleration X: "); //加速度
Serial.print(a.acceleration.x);
Serial.print(", Y: ");
Serial.print(a.acceleration.y);
Serial.print(", Z: ");
Serial.print(a.acceleration.z);
Serial.println(" m/s^2");
Serial.print("Rotation X: "); //陀螺仪原始数据
Serial.print(g.gyro.x);
Serial.print(", Y: ");
Serial.print(g.gyro.y);
Serial.print(", Z: ");
Serial.print(g.gyro.z);
Serial.println(" rad/s");
Serial.print("Temperature: ");//温度
Serial.print(temp.temperature);
Serial.println(" degC");
// 计算倾斜角度
double ax = a.acceleration.x, ay = a.acceleration.y, az = a.acceleration.z;
double roll = atan2(ay, az) * 180 / M_PI;
double pitch = atan2(-ax, sqrt(ay * ay + az * az)) * 180 / M_PI;
// 显示计算结果
Serial.print("Roll: "); //翻滚角
Serial.print(roll);
Serial.print(", Pitch: "); //俯仰角
Serial.println(pitch);
delay(500);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Usage Testing
The module will output acceleration/gyroscope/temperature/angle data.