108个传感器之-三轴陀螺仪GY-521(31)

108个传感器之-三轴陀螺仪GY-521(31)

介绍

GY-521模块是一款基于MPU-6050 MEMS(微机电系统)的分线板(Breakout Board),MPU-6050具有一个3轴陀螺仪、一个3轴加速度计、数字运动处理器(DMP)和温度传感器。数字运动处理器可用于直接在电路板上处理复杂算法。通常,DMP处理将来自传感器的原始值转换为稳定位置数据的算法。本篇文章将简要介绍GY-521 / MPU-6050模块。主要介绍如何检索原始传感器值。通过使用I2C串行数据总线检索传感器值,该总线仅需要两条线(SCL和SDA)。如果您打算使用所有功能,或者需要可靠且稳定的位置数据,那么我建议您先看看现成的库.

原理图

前期准备

  1. Esp32-c3-supermini x 1

esp32-c3-supermini

  1. 数据线 x 1

ESP32C3引脚映射

引脚连接

Tips: 用例使用的是 esp32-c3-supermini,对应的引脚是 A4、A5,如果在 platformIO 上开发的话,直接输入引脚名称会提示当前的引脚对应数字号。

Arduino Sensor
5 V +V
GND GND
Pin A4 SCL
Pin A5 SDA

代码示例

1
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
#include <Arduino.h>
#include "Wire.h"
// This library allows you to communicate with I2C devices.
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data
char tmp_str[7]; // temporary variable used in convert function
char *convert_int16_to_str(int16_t i)
{ // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
sprintf(tmp_str, "%6d", i);
return tmp_str;
}
void setup()
{
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
}
void loop()
{
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
Wire.requestFrom(MPU_ADDR, 7 * 2, true); // request a total of 7*2=14 registers

// "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
accelerometer_x = Wire.read() << 8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
accelerometer_y = Wire.read() << 8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
accelerometer_z = Wire.read() << 8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
temperature = Wire.read() << 8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
gyro_x = Wire.read() << 8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
gyro_y = Wire.read() << 8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
gyro_z = Wire.read() << 8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

// print out data
Serial.print("aX = ");
Serial.print(convert_int16_to_str(accelerometer_x));
Serial.print(" | aY = ");
Serial.print(convert_int16_to_str(accelerometer_y));
Serial.print(" | aZ = ");
Serial.print(convert_int16_to_str(accelerometer_z));
// the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
Serial.print(" | tmp = ");
Serial.print(temperature / 340.00 + 36.53);
Serial.print(" | gX = ");
Serial.print(convert_int16_to_str(gyro_x));
Serial.print(" | gY = ");
Serial.print(convert_int16_to_str(gyro_y));
Serial.print(" | gZ = ");
Serial.print(convert_int16_to_str(gyro_z));
Serial.println();

// delay
delay(1000);
}

如果代码正常运行的话,终端就会提示类似以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
aX = -13748 | aY =  -5500 | aZ =   1668 | tmp = 18.74 | gX =   4967 | gY =    -34 | gZ =    -70
aX = -13820 | aY = -5500 | aZ = 1544 | tmp = 18.79 | gX = 5007 | gY = -44 | gZ = -76
aX = -13740 | aY = -5508 | aZ = 1580 | tmp = 18.74 | gX = 4986 | gY = -32 | gZ = -42
aX = -13788 | aY = -5576 | aZ = 1572 | tmp = 18.84 | gX = 5001 | gY = -26 | gZ = -85
aX = -13828 | aY = -5544 | aZ = 1556 | tmp = 18.84 | gX = 5009 | gY = -63 | gZ = -80
aX = -13788 | aY = -5420 | aZ = 1460 | tmp = 18.74 | gX = 4991 | gY = -16 | gZ = -35
aX = -13632 | aY = -5448 | aZ = 1500 | tmp = 18.79 | gX = 4749 | gY = -115 | gZ = -666
aX = -13552 | aY = -5452 | aZ = 1680 | tmp = 18.74 | gX = 5001 | gY = -113 | gZ = -239
aX = -13844 | aY = -5584 | aZ = 1580 | tmp = 18.84 | gX = 4982 | gY = -41 | gZ = -102
aX = -13816 | aY = -5524 | aZ = 1552 | tmp = 18.79 | gX = 5006 | gY = -43 | gZ = -98
aX = -13768 | aY = -5500 | aZ = 1624 | tmp = 18.79 | gX = 5004 | gY = -42 | gZ = -55
aX = -13732 | aY = -5412 | aZ = 1540 | tmp = 18.84 | gX = 4989 | gY = -47 | gZ = -126
aX = -13916 | aY = -5468 | aZ = 1568 | tmp = 18.74 | gX = 5026 | gY = -102 | gZ = -14
aX = -13732 | aY = -5552 | aZ = 1696 | tmp = 18.69 | gX = 5017 | gY = -5 | gZ = -52
aX = -13784 | aY = -5380 | aZ = 1612 | tmp = 18.84 | gX = 4994 | gY = -23 | gZ = -31
aX = -13752 | aY = -5580 | aZ = 1596 | tmp = 18.74 | gX = 5002 | gY = -17 | gZ = -52

小结

待完善…

参考

dev 的艺术空间

示例的库:https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050。

108个传感器之-三轴陀螺仪GY-521(31)

http://blog.jzxer.cn/20250102/20250102-xyz/

作者

dev

发布于

2025-01-02

更新于

2025-01-04

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×