极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 21308|回复: 4

请教一下MPU6050的使用问题,谢谢

[复制链接]
发表于 2012-5-25 17:22:34 | 显示全部楼层 |阅读模式
  1. // I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
  2. // 10/7/2011 by Jeff Rowberg <[email protected]>
  3. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  4. //
  5. // Changelog:
  6. //     2011-10-07 - initial release

  7. /* ============================================
  8. I2Cdev device library code is placed under the MIT license
  9. Copyright (c) 2011 Jeff Rowberg

  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:

  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.

  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. ===============================================
  26. */

  27. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  28. // is used in I2Cdev.h
  29. #include "Wire.h"

  30. // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
  31. // for both classes must be in the include path of your project
  32. #include "I2Cdev.h"
  33. #include "MPU6050.h"

  34. // class default I2C address is 0x68
  35. // specific I2C addresses may be passed as a parameter here
  36. // AD0 low = 0x68 (default for InvenSense evaluation board)
  37. // AD0 high = 0x69
  38. MPU6050 accelgyro;

  39. int16_t ax, ay, az;
  40. int16_t gx, gy, gz;

  41. #define LED_PIN 13
  42. bool blinkState = false;

  43. void setup() {
  44.     // join I2C bus (I2Cdev library doesn't do this automatically)
  45.     Wire.begin();

  46.     // initialize serial communication
  47.     // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
  48.     // it's really up to you depending on your project)
  49.     Serial.begin(38400);

  50.     // initialize device
  51.     Serial.println("Initializing I2C devices...");
  52.     accelgyro.initialize();

  53.     // verify connection
  54.     Serial.println("Testing device connections...");
  55.     Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

  56.     // configure Arduino LED for
  57.     pinMode(LED_PIN, OUTPUT);
  58. }

  59. void loop() {
  60.     // read raw accel/gyro measurements from device
  61.     accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  62.     // these methods (and a few others) are also available
  63.     //accelgyro.getAcceleration(&ax, &ay, &az);
  64.     //accelgyro.getRotation(&gx, &gy, &gz);

  65.     // display tab-separated accel/gyro x/y/z values
  66.     Serial.print("a/g:\t");
  67.     Serial.print(ax); Serial.print("\t");
  68.     Serial.print(ay); Serial.print("\t");
  69.     Serial.print(az); Serial.print("\t");
  70.     Serial.print(gx); Serial.print("\t");
  71.     Serial.print(gy); Serial.print("\t");
  72.     Serial.println(gz);

  73.     // blink LED to indicate activity
  74.     blinkState = !blinkState;
  75.     digitalWrite(LED_PIN, blinkState);
  76. }
复制代码

这是我用官方程序在桌子上不动时得到的数据


  1. #include "Wire.h"
  2. #include "I2Cdev.h"
  3. #include "MPU6050.h"
  4. MPU6050 accelgyro;

  5. int16_t ax, ay, az;
  6. int16_t gx, gy, gz;


  7. bool blinkState = false;

  8. void setup() {

  9.     Wire.begin();
  10.     Serial.begin(9800);

  11.     accelgyro.initialize();


  12. }

  13. void loop() {
  14. delay(100);
  15.     accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  16.     Serial.print("a/g:\t");
  17.     Serial.print(ax/4096); Serial.print("\t");
  18.     Serial.print(ay/4096); Serial.print("\t");
  19.     Serial.print(az/4096); Serial.print("\t");
  20.     Serial.print(gx/32.8); Serial.print("\t");
  21.     Serial.print(gy/32.8); Serial.print("\t");
  22.     Serial.println(gz/32.8);
  23.     blinkState = !blinkState;

  24. }
复制代码
下面是我改过程序得到的数据,加速度数据基本没啥变化,不知道这是什么情况,难道是我买的芯片有问题?希望高手给解惑,谢谢!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2012-5-25 20:46:17 | 显示全部楼层
不知道这个实验是有有帮助
http://www.geek-workshop.com/for ... thread&tid=1017
回复 支持 反对

使用道具 举报

发表于 2012-5-25 21:00:34 | 显示全部楼层
今天把我的模块地址改成0x69了,跑你的程序正常。你的程序有错误,应为/4096.0,否则取整输出了
回复 支持 反对

使用道具 举报

发表于 2012-5-25 21:36:00 | 显示全部楼层
syfok 发表于 2012-5-25 21:00
今天把我的模块地址改成0x69了,跑你的程序正常。你的程序有错误,应为/4096.0,否则取整输出了

首先,这个数字我之前也出现过,ADXL345的数字,我测过好几块是这样的,我初步怀疑的原因是芯片没有焊对在焊盘上所导致,毕竟芯片,只要你不上太高的电压是不会那么容易的坏掉的,还有你说的那个小数点,确实是需要加上.0才会有明显的小数点变化,你的问题,可以自己去用风枪重新吹一下芯片!
回复 支持 反对

使用道具 举报

发表于 2013-5-15 13:55:19 来自手机 | 显示全部楼层
为我的总是编译出错呢?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 16:12 , Processed in 0.051754 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表