Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. /*
  2. ===Contact & Support===
  3. Website: http://eeenthusiast.com/
  4. Youtube: https://www.youtube.com/EEEnthusiast
  5. Facebook: https://www.facebook.com/EEEnthusiast/
  6. Patreon: https://www.patreon.com/EE_Enthusiast
  7. Revision: 1.0 (July 13th, 2016)
  8. ===Hardware===
  9. - Arduino Uno R3
  10. - MPU-6050 (Available from: http://eeenthusiast.com/product/6dof-mpu-6050-accelerometer-gyroscope-temperature/)
  11. ===Software===
  12. - Latest Software: https://github.com/VRomanov89/EEEnthusiast/tree/master/MPU-6050%20Implementation/MPU6050_Implementation
  13. - Arduino IDE v1.6.9
  14. - Arduino Wire library
  15. ===Terms of use===
  16. The software is provided by EEEnthusiast without warranty of any kind. In no event shall the authors or
  17. copyright holders be liable for any claim, damages or other liability, whether in an action of contract,
  18. tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in
  19. the software.
  20. */
  21.  
  22. #include <Wire.h>
  23.  
  24. long accelX, accelY, accelZ;
  25. float gForceX, gForceY, gForceZ;
  26.  
  27. long gyroX, gyroY, gyroZ;
  28. float rotX, rotY, rotZ;
  29.  
  30. void setup() {
  31. Serial.begin(9600);
  32. Wire.begin();
  33. setupMPU();
  34. }
  35.  
  36.  
  37. void loop() {
  38. recordAccelRegisters();
  39. recordGyroRegisters();
  40. printData();
  41. delay(100);
  42. }
  43.  
  44. void setupMPU(){
  45. Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
  46. Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
  47. Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
  48. Wire.endTransmission();
  49. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  50. Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
  51. Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
  52. Wire.endTransmission();
  53. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  54. Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
  55. Wire.write(0b00000000); //Setting the accel to +/- 2g
  56. Wire.endTransmission();
  57. }
  58.  
  59. void recordAccelRegisters() {
  60. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  61. Wire.write(0x3B); //Starting register for Accel Readings
  62. Wire.endTransmission();
  63. Wire.requestFrom(0b1101000,6); //Request Accel Registers (3B - 40)
  64. while(Wire.available() < 6);
  65. accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
  66. accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
  67. accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
  68. processAccelData();
  69. }
  70.  
  71. void processAccelData(){
  72. gForceX = accelX / 16384.0;
  73. gForceY = accelY / 16384.0;
  74. gForceZ = accelZ / 16384.0;
  75. }
  76.  
  77. void recordGyroRegisters() {
  78. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  79. Wire.write(0x43); //Starting register for Gyro Readings
  80. Wire.endTransmission();
  81. Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 - 48)
  82. while(Wire.available() < 6);
  83. gyroX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
  84. gyroY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
  85. gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
  86. processGyroData();
  87. }
  88.  
  89. void processGyroData() {
  90. rotX = gyroX / 131.0;
  91. rotY = gyroY / 131.0;
  92. rotZ = gyroZ / 131.0;
  93. }
  94.  
  95. void printData() {
  96. Serial.print("Gyro (deg)");
  97. Serial.print(" X=");
  98. Serial.print(rotX);
  99. Serial.print(" Y=");
  100. Serial.print(rotY);
  101. Serial.print(" Z=");
  102. Serial.print(rotZ);
  103. Serial.print(" Accel (g)");
  104. Serial.print(" X=");
  105. Serial.print(gForceX);
  106. Serial.print(" Y=");
  107. Serial.print(gForceY);
  108. Serial.print(" Z=");
  109. Serial.println(gForceZ);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement