MPU6050 adopts IIC communication, the structure of IIC communication is as follows
IIC stands for Inter-Integrated Circuit (Integrated Circuit Bus). This bus type is a simple, two-way, two-wire, synchronous serial bus designed by Philips Semiconductors in the early 1980s. It is mainly used to connect the overall circuit. ( ICS ), IIC is a multi-directional control bus , which means that multiple chips can be connected to the same bus structure , and each chip can be used as a control source for real-time data transmission. This approach simplifies the bus interface for signal transmission.
The IIC bus has four wires in total, namely VCC, GND, SCL, and SDA . SCL is the clock line and SDA is the data line. Two-way data transmission is carried out in units of bytes (8 bits) between the master device and the slave device on the I2C bus .
The address of the MPU6050 data in the register:
0x3C , the low eight bits of the X-axis component of the accelerometer
0x3D , the high eight bits of the Y-axis component of the accelerometer
0x3E , the low eight bits of the Y-axis component of the accelerometer
0x3F , the Z of the accelerometer The high eight bits of the axis component
are 0x40 , the high eight bits of the Z axis component of the accelerometer are
0x41 , the high eight bits of the current temperature TEMP are
0x42 , the low eight bits of the current temperature TEMP are
0x43 , the high eight bits of the angular velocity around the X axis
are 0x44 , and the high eight bits of the rotation around the X axis are 0x44. Angular velocity is low eight bits
0x45 , angular velocity around the Y axis is high eight bits
0x46 , angular velocity around the Y axis is low eight bits
0x47 , angular velocity around the Z axis is high eight bits
0x48 , angular velocity around the Z axis is low eight bits
The next step is to read the data
MPU6050 Arduino uno
VCC —— 3.3V/5V
GND —— GND
SCL —— SCL
SDA —— SDA
#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
unsigned char DATA_H, DATA_L;
//float ax,ay,az,gx,gy,gz,temp;
void setup() {
Wire.begin();
Serial.begin(9600);
delay(10);
MPU6050Reset();
}
void loop() {
mpu6050_TEMP_OUT_data();
mpu6050_ACCEL_X_data();
mpu6050_ACCEL_Y_data();
mpu6050_ACCEL_Z_data();
mpu6050_GYRO_X_data();
mpu6050_GYRO_Y_data();
mpu6050_GYRO_Z_data() ;
Serial.print(" ACCEL_X "); Serial.print(mpu6050_ACCEL_X_data()); Serial.print(",");
Serial.print(" ACCEL_Y "); Serial.print(mpu6050_ACCEL_Y_data()); Serial.print(",");
Serial.print(" ACCEL_Z "); Serial.print(mpu6050_ACCEL_Z_data()); Serial.print(",");
Serial.print(" GYRO_X "); Serial.print(mpu6050_GYRO_X_data()); Serial.print(",");
Serial.print(" GYRO_Y "); Serial.print(mpu6050_GYRO_Y_data()); Serial.print(",");
Serial.print(" GYRO_Z "); Serial.print(mpu6050_GYRO_Z_data()); Serial.print(",");
Serial.print(" TEMP_OUT ");Serial.print(mpu6050_TEMP_OUT_data());
Serial.println();
}
void MPU6050Write(unsigned char adr, unsigned char dat)
{
Wire.beginTransmission(0x68); //开启MPU6050的传输
Wire.write(adr); //指定寄存器地址
Wire.write(dat); //写入一个字节的数据
Wire.endTransmission(true); //结束传输,true表示释放总线
}
void MPU6050Reset()
{
MPU6050Write(0x6B, 0x00);
MPU6050Write(0x19, 0x07);
MPU6050Write(0x1A, 0x06);
MPU6050Write(0x1B, 0x18); //2000
MPU6050Write(0x1C, 0x10); //4g
/*寄存器的设置参考此文章http://blog.sina.com.cn/s/blog_8240cbef01018i10.html */
}
unsigned char ReadMPUReg(int nReg) {
Wire.beginTransmission(0x68);
Wire.write(nReg);
Wire.requestFrom(0x68, 1, true);
Wire.endTransmission(true);
return Wire.read();
}
//读 加速度计X轴 数据
int mpu6050_ACCEL_X_data()
{
DATA_H = ReadMPUReg(ACCEL_XOUT_H);
DATA_L = ReadMPUReg(ACCEL_XOUT_L);
return (DATA_H << 8) + DATA_L;
}
//读 加速度计Y轴 数据
int mpu6050_ACCEL_Y_data()
{
DATA_H = ReadMPUReg(ACCEL_YOUT_H);
DATA_L = ReadMPUReg(ACCEL_YOUT_L);
return (DATA_H << 8) + DATA_L;
}
//读 加速度计Z轴 数据
int mpu6050_ACCEL_Z_data()
{
DATA_H = ReadMPUReg(ACCEL_ZOUT_H);
DATA_L = ReadMPUReg(ACCEL_ZOUT_L);
return (DATA_H << 8) + DATA_L;
}
//读 陀螺仪计X轴 数据
int mpu6050_GYRO_X_data()
{
DATA_H = ReadMPUReg(GYRO_XOUT_H);
DATA_L = ReadMPUReg(GYRO_XOUT_L);
return (DATA_H << 8) + DATA_L;
}
//读 陀螺仪计Y轴 数据
int mpu6050_GYRO_Y_data()
{
DATA_H = ReadMPUReg(GYRO_YOUT_H);
DATA_L = ReadMPUReg(GYRO_YOUT_L);
return (DATA_H << 8) + DATA_L;
}
//读 陀螺仪计Z轴 数据
int mpu6050_GYRO_Z_data()
{
DATA_H = ReadMPUReg(GYRO_ZOUT_H);
DATA_L = ReadMPUReg(GYRO_ZOUT_L);
return (DATA_H << 8) + DATA_L;
}
//读 温度 数据
int mpu6050_TEMP_OUT_data()
{
DATA_H = ReadMPUReg(TEMP_OUT_H);
DATA_L = ReadMPUReg(TEMP_OUT_L);
return (DATA_H << 8) + DATA_L;
}
//by Sunlife
Select the serial port monitor in the ide to see the data
The baud rate is set to 9600