MPU6050_raw.ino 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 for
  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. }