arduino链接GY-521(MPU6050)模块
本例使用 `lilypad` 链接MPU6050模块, `arduino uno`作为usb下载器,具体连接方法请移步: http://www.cnblogs.com/hangxin1940/archive/2013/04/06/3002709.html
## lilypad 与 MPU6050的连接方法
![lilypad](http://images.cnblogs.com/cnblogs_com/hangxin1940/467679/o_700px-6_dof_mpu6050.png "lilypad")
lilypad与mpu6050相对应的引脚:
*+* -> *VCC*
*-* -> *GND*
*a4* -> *SDA*
*a5* -> *SCL*
## 下载i2cdev库
https://github.com/jrowberg/i2cdevlib/archive/master.zip
解压缩之后,将arduino文件夹下的代码添加至arduino ide的类库路径
以ubuntu为例:
cp -rf * /usr/share/arduino/libraries
## 测试程序
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
//板载led引脚默认为13
int LED_PIN = 13;
//led状态信息
bool blinkState = false;
void setup() {
//接入i2c总线
Wire.begin();
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");
//配置板载led引脚
pinMode(LED_PIN, OUTPUT);
}
//将原始值修正
void fix() {
ax /= 16384;
ay /= 16384;
az /= 16384;
gx /= 131;
gy /= 131;
gz /= 131;
}
//干干其他事
void process() {
}
void loop() {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
//对原始价值做处理
fix();
//干干其他事
process();
Serial.print("a/g:\t");
//x,y,z轴加速度
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.print(az); Serial.print("\t");
//x,y,z轴角速度
Serial.print(gx); Serial.print("\t");
Serial.print(gy); Serial.print("\t");
Serial.println(gz);
//控制板载led状态
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}
烧写进lilypad后,可以看到led几乎是长亮
ubuntu下查看串口信息
sudo ttylog -d /dev/ttyACM0 -b 38400
输出:
![mpu6050](http://images.cnblogs.com/cnblogs_com/hangxin1940/467679/o_arduino_mpu6050.png "mpu6050")