简介
MPU-6050集成了3轴MEMS陀螺仪,3轴MEMS加速度计,以及一个可扩展的数字运动处理器DMP,可用IIC接口连接一个第三方的数字传感器。MPU-6050对陀螺仪和加速度计分别用了三个16位的ADC,将其测量的模拟量转化为可输出的数字量。为了精确跟踪快速和慢速的运动,传感器的测量范围都是用户可控的,陀螺仪可测范围为250,500,1000,2000/秒(dps),加速度计可测范围为2,4,8,16g。
模块引脚说明
名称 | 描述 |
---|---|
VDD | DC+5v |
GND | GND |
SCL | IIC串行时钟线/SPI串行时钟端口 |
SDA | IIC串行数据线/SPI串行数据输入 |
XDA | 连接其他IIC设备的主机数据口 |
XCL | 给IIC设备提供主时钟 |
ADO | IIC器件地址选择位/SPI串行数据输出 |
INT | 中断引脚 |
接线
5v——VCC
GND——GND
SCL——SCL
SDA——SDA
Arduino例程代码
include "I2Cdev.h"
include "MPU6050.h"
if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
endif
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
define OUTPUT_READABLE_ACCELGYRO
define LED_PIN 13
bool blinkState = false;
void setup() {
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
Serial.begin(38400);
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
pinMode(LED_PIN, OUTPUT);
}
void loop() {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
ifdef OUTPUT_READABLE_ACCELGYRO
Serial.print("a/g:\t");
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.print(az); Serial.print("\t");
Serial.print(gx); Serial.print("\t");
Serial.print(gy); Serial.print("\t");
Serial.println(gz);
endif
ifdef OUTPUT_BINARY_ACCELGYRO
Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
endif
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
打开串口检测器,把波特率设置为38400
总结
像MPU-6050这种带有SCL,SDA串行IIC接口的模块,可以连接Arduino的IIC接口通过例程进行简单使用,也方便对模块进行测试。