BLOG記事用論壇

 找回密碼
 註冊
搜索
熱搜:
查看: 2074|回復: 0

MPU6050 gy-521搭配Arduino nano使用

[複製鏈接]
發表於 2021-7-5 23:37:50 | 顯示全部樓層 |閱讀模式

說明MPU6050是晶片
我使用gy 521上面幫你做了一些接線


接線方式
gy-521可只接4隻腳位
VCC (電壓3.3V and 5V sources.)
GND
SCL (Serial Clock Line of the I2C protocol.)
SDA (Serial Data Line of the I2C protocol.)

與Arduino nano接線
VCC接27腳5V
GND接29腳GNN
SCL接A5
SDA接A4
如圖
GY-521接線.jpg


使用Library涵式庫,讓你不用寫底層處理,直接使用寫code比較快
https://github.com/jrowberg/i2cdevlib
安裝方式將檔案用zip下載
然後資料夾Arduino\MPU6050還有Arduino\I2Cdev丟到我的文件\Arduino\libraries
然後重開arduino會讀到

使用範例MPU6050\examples\MPU6050_raw
  1. // I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
  2. // 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
  3. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  4. //
  5. // Changelog:
  6. //      2013-05-08 - added multiple output formats
  7. //                 - added seamless Fastwire support
  8. //      2011-10-07 - initial release

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

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

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

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

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

  33. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  34. // is used in I2Cdev.h
  35. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  36.     #include "Wire.h"
  37. #endif

  38. // class default I2C address is 0x68
  39. // specific I2C addresses may be passed as a parameter here
  40. // AD0 low = 0x68 (default for InvenSense evaluation board)
  41. // AD0 high = 0x69
  42. MPU6050 accelgyro;
  43. //MPU6050 accelgyro(0x69); // <-- use for AD0 high

  44. int16_t ax, ay, az;
  45. int16_t gx, gy, gz;



  46. // uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated
  47. // list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,
  48. // not so easy to parse, and slow(er) over UART.
  49. #define OUTPUT_READABLE_ACCELGYRO

  50. // uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit
  51. // binary, one right after the other. This is very fast (as fast as possible
  52. // without compression or data loss), and easy to parse, but impossible to read
  53. // for a human.
  54. //#define OUTPUT_BINARY_ACCELGYRO


  55. #define LED_PIN 13
  56. bool blinkState = false;

  57. void setup() {
  58.     // join I2C bus (I2Cdev library doesn't do this automatically)
  59.     #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  60.         Wire.begin();
  61.     #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  62.         Fastwire::setup(400, true);
  63.     #endif

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

  68.     // initialize device
  69.     Serial.println("Initializing I2C devices...");
  70.     accelgyro.initialize();

  71.     // verify connection
  72.     Serial.println("Testing device connections...");
  73.     Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

  74.     // use the code below to change accel/gyro offset values
  75.     /*
  76.     Serial.println("Updating internal sensor offsets...");
  77.     // -76        -2359        1688        0        0        0
  78.     Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
  79.     Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
  80.     Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
  81.     Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
  82.     Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
  83.     Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
  84.     Serial.print("\n");
  85.     accelgyro.setXGyroOffset(220);
  86.     accelgyro.setYGyroOffset(76);
  87.     accelgyro.setZGyroOffset(-85);
  88.     Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
  89.     Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
  90.     Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
  91.     Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
  92.     Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
  93.     Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
  94.     Serial.print("\n");
  95.     */

  96.     // configure Arduino LED pin for output
  97.     pinMode(LED_PIN, OUTPUT);
  98. }

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

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

  105.     #ifdef OUTPUT_READABLE_ACCELGYRO
  106.         // display tab-separated accel/gyro x/y/z values
  107.         Serial.print("a/g:\t");
  108.         Serial.print(ax); Serial.print("\t");
  109.         Serial.print(ay); Serial.print("\t");
  110.         Serial.print(az); Serial.print("\t");
  111.         Serial.print(gx); Serial.print("\t");
  112.         Serial.print(gy); Serial.print("\t");
  113.         Serial.println(gz);
  114.     #endif

  115.     #ifdef OUTPUT_BINARY_ACCELGYRO
  116.         Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
  117.         Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
  118.         Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
  119.         Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
  120.         Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
  121.         Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
  122.     #endif

  123.     // blink LED to indicate activity
  124.     blinkState = !blinkState;
  125.     digitalWrite(LED_PIN, blinkState);
  126. }
複製代碼



可使用COM觀看加速度
加速度.png


您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|綜合討論區

GMT+8, 2026-6-27 20:16 , Processed in 0.027229 second(s), 10 queries , File On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回復 返回頂部 返回列表