代码:原始读数// MPU-6050:直接读 I2C,不用库
#include <Wire.h>
const int MPU = 0x68; // AD0 为低:地址 0x68
int16_t ax, ay, az, gx;
void wake() {
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1:写入 0 唤醒芯片(它上电时处于睡眠状态)
Wire.write(0);
Wire.endTransmission();
}
void readImu() {
Wire.beginTransmission(MPU);
Wire.write(0x3B); // ACCEL_XOUT_H:从这里开始的 14 个字节依次是加速度 X、Y、Z,温度,陀螺仪 X、Y、Z
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14);
ax = Wire.read() << 8 | Wire.read();
ay = Wire.read() << 8 | Wire.read();
az = Wire.read() << 8 | Wire.read();
Wire.read(); Wire.read(); // 温度:跳过
gx = Wire.read() << 8 | Wire.read();
Wire.read(); Wire.read(); Wire.read(); Wire.read();
}
void setup() {
Serial.begin(9600);
Wire.begin();
wake();
Serial.println("mpu6050: awake, +-2 g, +-250 deg/s");
}
void loop() {
readImu();
Serial.print("ax ");
Serial.print(ax / 16384.0, 2); // ±2 g 量程下 16384 个计数 = 1 g
Serial.print(" ay ");
Serial.print(ay / 16384.0, 2);
Serial.print(" az ");
Serial.print(az / 16384.0, 2);
Serial.print(" g | gx ");
Serial.print(gx / 131.0, 1); // ±250 °/s 量程下 131 个计数 = 1 °/s
Serial.println(" deg/s");
delay(1000);
}