Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. //Interface MPU6050 Accelerometer and Gyroscope to Arduino Uno and display accelerometer data
  2. //on Serial Monitor
  3.  
  4. /*Copyright (c) 2019, ProteShea LLC
  5. All rights reserved.
  6.  
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. 3. Neither the name of the copyright holders nor the
  15. names of its contributors may be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17.  
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  19. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29.  
  30. #include <Wire.h>
  31.  
  32. #define MPUaddr0 0x68 //addr used if pin AD0 is set to 0 (left unconnected)
  33. #define MPUaddr1 0x69 //addr used if pin AD0 is set to 1 (wired to VDD)
  34.  
  35. float AccelX, AccelY, AccelZ = 0;
  36. float accelXangle, accelYangle = 0;
  37. float pitch, roll = 0;
  38.  
  39. void setup() {
  40. Serial.begin(115200); //set baud rate to 115200 for serial transmission
  41. Wire.begin();
  42. resetMPU(); //reset MPU to default settings
  43. setAccelSensitivity(0x00); //programmable range of +/-2g, +/-4g, +/-8g, +/-16g
  44. delay(10000); //wait for MPU6050 to stabilize
  45. }
  46.  
  47. void loop() {
  48. readAccel(16384.0); //read XYZ Accel data from registers 0x3B to 0x40
  49. displaySerial(); //display yaw, pitch, and roll on Serial Monitor
  50. delay(10);
  51.  
  52. }
  53.  
  54. void displaySerial(void){
  55. Serial.print(AccelX);
  56. Serial.print("\t");
  57. Serial.print(AccelY);
  58. Serial.print("\t");
  59. Serial.print(AccelZ);
  60. Serial.println("\t");
  61.  
  62. Serial.print(" Pitch: ");
  63. Serial.print(accelYangle);
  64. Serial.print(" Roll: ");
  65. Serial.println(accelXangle);
  66.  
  67. }
  68.  
  69. void readAccel(float accelDivisor){
  70. //NOTE: as you increase the accelerometer's range, the resolution decreases
  71. //+/-2g, use divisor of 16384 (14-bit resolution)
  72. //+/-4g, use divisor of 8192 (13-bit resolution)
  73. //+/-8g, use divisor of 4096 (12-bit resolution)
  74. //+/-16g, use divisor of 2048 (11-bit resolution)
  75.  
  76. Wire.beginTransmission(MPUaddr0);
  77. Wire.write(0x3B);
  78. Wire.endTransmission();
  79. Wire.requestFrom(MPUaddr0, 6); //read 6 consecutive registers starting at 0x3B
  80. if (Wire.available() >= 6){
  81. int16_t temp0 = Wire.read() << 8; //read upper byte of X
  82. int16_t temp1 = Wire.read(); //read lower byte of X
  83. AccelX = (float) (temp0 | temp1);
  84. AccelX = AccelX / accelDivisor;
  85.  
  86. temp0 = Wire.read() << 8; //read upper byte of Y
  87. temp1 = Wire.read(); //read lower byte of Y
  88. AccelY = (float) (temp0 | temp1);
  89. AccelY = AccelY / accelDivisor;
  90.  
  91. temp0 = Wire.read() << 8; //read upper byte of Z
  92. temp1 = Wire.read(); //read lower byte of Z
  93. AccelZ = (float) (temp0 | temp1);
  94. AccelZ = AccelZ / accelDivisor;
  95.  
  96. }
  97. //You can only calculate roll and pitch from accelerometer data
  98. accelXangle = (atan2(AccelY, AccelZ)) * 180 / PI; //calculate roll
  99. accelYangle = (atan2(-AccelX, sqrt(pow(AccelY, 2) + pow(AccelZ, 2)))) * 180 / PI; //calculate pitch
  100.  
  101. }
  102.  
  103. void setAccelSensitivity(uint8_t g){
  104. //Config AFS_SEL[1:0] bits 4 and 3 in register 0x1C
  105. //0x00: +/-2g (default)
  106. //0x08: +/-4g
  107. //0x10: +/-8g
  108. //0x18: +/-16g
  109.  
  110. Wire.beginTransmission(MPUaddr0); //initialize comm with MPU @ 0x68
  111. Wire.write(0x1C); //write to register 0x1C
  112. Wire.write(g); //setting bit 7 to 1 resets all internal registers to default values
  113. Wire.endTransmission(); //end comm
  114. }
  115.  
  116. void resetMPU(void){
  117. Wire.beginTransmission(MPUaddr0); //initialize comm with MPU @ 0x68
  118. Wire.write(0x6B); //write to register 0x6B
  119. Wire.write(0x00); //reset all internal registers to default values
  120. Wire.endTransmission(); //end comm
  121. delay(100);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement