GY-521 Gyroscope Accelerometer Sensor Module


৳ 220.00

Description

This simple module contains everything required to interface to the Arduino and other controllers via I2C (use the Wire Arduino library) and give motion sensing information for 3 axes – X, Y and Z.

Specifications

  • Accelerometer ranges: ±2, ±4, ±8, ±16g
  • Gyroscope ranges: ± 250, 500, 1000, 2000 °/s
  • Voltage range: 3.3V – 5V (the module include a low drop-out voltage regulator)

Package 1 X GY-521 Gyroscope Accelerometer Sensor Module

Code

  1. /*
  2. Arduino and MPU6050 Accelerometer and Gyroscope Sensor Tutorial
  3. by Dejan, https://howtomechatronics.com
  4. */
  5. #include <Wire.h>
  6. const int MPU = 0x68; // MPU6050 I2C address
  7. float AccX, AccY, AccZ;
  8. float GyroX, GyroY, GyroZ;
  9. float accAngleX, accAngleY, gyroAngleX, gyroAngleY, gyroAngleZ;
  10. float roll, pitch, yaw;
  11. float AccErrorX, AccErrorY, GyroErrorX, GyroErrorY, GyroErrorZ;
  12. float elapsedTime, currentTime, previousTime;
  13. int c = 0;
  14. void setup() {
  15. Serial.begin(19200);
  16. Wire.begin(); // Initialize comunication
  17. Wire.beginTransmission(MPU); // Start communication with MPU6050 // MPU=0x68
  18. Wire.write(0x6B); // Talk to the register 6B
  19. Wire.write(0x00); // Make reset – place a 0 into the 6B register
  20. Wire.endTransmission(true); //end the transmission
  21. /*
  22. // Configure Accelerometer Sensitivity – Full Scale Range (default +/- 2g)
  23. Wire.beginTransmission(MPU);
  24. Wire.write(0x1C); //Talk to the ACCEL_CONFIG register (1C hex)
  25. Wire.write(0x10); //Set the register bits as 00010000 (+/- 8g full scale range)
  26. Wire.endTransmission(true);
  27. // Configure Gyro Sensitivity – Full Scale Range (default +/- 250deg/s)
  28. Wire.beginTransmission(MPU);
  29. Wire.write(0x1B); // Talk to the GYRO_CONFIG register (1B hex)
  30. Wire.write(0x10); // Set the register bits as 00010000 (1000deg/s full scale)
  31. Wire.endTransmission(true);
  32. delay(20);
  33. */
  34. // Call this function if you need to get the IMU error values for your module
  35. calculate_IMU_error();
  36. delay(20);
  37. }
  38. void loop() {
  39. // === Read acceleromter data === //
  40. Wire.beginTransmission(MPU);
  41. Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
  42. Wire.endTransmission(false);
  43. Wire.requestFrom(MPU, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
  44. //For a range of +-2g, we need to divide the raw values by 16384, according to the datasheet
  45. AccX = (Wire.read() << 8 | Wire.read()) / 16384.0; // X-axis value
  46. AccY = (Wire.read() << 8 | Wire.read()) / 16384.0; // Y-axis value
  47. AccZ = (Wire.read() << 8 | Wire.read()) / 16384.0; // Z-axis value
  48. // Calculating Roll and Pitch from the accelerometer data
  49. accAngleX = (atan(AccY / sqrt(pow(AccX, 2) + pow(AccZ, 2))) * 180 / PI)0.58; // AccErrorX ~(0.58) See the calculate_IMU_error()custom function for more details
  50. accAngleY = (atan(1 * AccX / sqrt(pow(AccY, 2) + pow(AccZ, 2))) * 180 / PI) + 1.58; // AccErrorY ~(-1.58)
  51. // === Read gyroscope data === //
  52. previousTime = currentTime; // Previous time is stored before the actual time read
  53. currentTime = millis(); // Current time actual time read
  54. elapsedTime = (currentTime – previousTime) / 1000; // Divide by 1000 to get seconds
  55. Wire.beginTransmission(MPU);
  56. Wire.write(0x43); // Gyro data first register address 0x43
  57. Wire.endTransmission(false);
  58. Wire.requestFrom(MPU, 6, true); // Read 4 registers total, each axis value is stored in 2 registers
  59. GyroX = (Wire.read() << 8 | Wire.read()) / 131.0; // For a 250deg/s range we have to divide first the raw value by 131.0, according to the datasheet
  60. GyroY = (Wire.read() << 8 | Wire.read()) / 131.0;
  61. GyroZ = (Wire.read() << 8 | Wire.read()) / 131.0;
  62. // Correct the outputs with the calculated error values
  63. GyroX = GyroX + 0.56; // GyroErrorX ~(-0.56)
  64. GyroY = GyroY – 2; // GyroErrorY ~(2)
  65. GyroZ = GyroZ + 0.79; // GyroErrorZ ~ (-0.8)
  66. // Currently the raw values are in degrees per seconds, deg/s, so we need to multiply by sendonds (s) to get the angle in degrees
  67. gyroAngleX = gyroAngleX + GyroX * elapsedTime; // deg/s * s = deg
  68. gyroAngleY = gyroAngleY + GyroY * elapsedTime;
  69. yaw = yaw + GyroZ * elapsedTime;
  70. // Complementary filter – combine acceleromter and gyro angle values
  71. roll = 0.96 * gyroAngleX + 0.04 * accAngleX;
  72. pitch = 0.96 * gyroAngleY + 0.04 * accAngleY;
  73. // Print the values on the serial monitor
  74. Serial.print(roll);
  75. Serial.print(“/”);
  76. Serial.print(pitch);
  77. Serial.print(“/”);
  78. Serial.println(yaw);
  79. }
  80. void calculate_IMU_error() {
  81. // We can call this funtion in the setup section to calculate the accelerometer and gyro data error. From here we will get the error values used in the above equations printed on the Serial Monitor.
  82. // Note that we should place the IMU flat in order to get the proper values, so that we then can the correct values
  83. // Read accelerometer values 200 times
  84. while (c < 200) {
  85. Wire.beginTransmission(MPU);
  86. Wire.write(0x3B);
  87. Wire.endTransmission(false);
  88. Wire.requestFrom(MPU, 6, true);
  89. AccX = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
  90. AccY = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
  91. AccZ = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
  92. // Sum all readings
  93. AccErrorX = AccErrorX + ((atan((AccY) / sqrt(pow((AccX), 2) + pow((AccZ), 2))) * 180 / PI));
  94. AccErrorY = AccErrorY + ((atan(1 * (AccX) / sqrt(pow((AccY), 2) + pow((AccZ), 2))) * 180 / PI));
  95. c++;
  96. }
  97. //Divide the sum by 200 to get the error value
  98. AccErrorX = AccErrorX / 200;
  99. AccErrorY = AccErrorY / 200;
  100. c = 0;
  101. // Read gyro values 200 times
  102. while (c < 200) {
  103. Wire.beginTransmission(MPU);
  104. Wire.write(0x43);
  105. Wire.endTransmission(false);
  106. Wire.requestFrom(MPU, 6, true);
  107. GyroX = Wire.read() << 8 | Wire.read();
  108. GyroY = Wire.read() << 8 | Wire.read();
  109. GyroZ = Wire.read() << 8 | Wire.read();
  110. // Sum all readings
  111. GyroErrorX = GyroErrorX + (GyroX / 131.0);
  112. GyroErrorY = GyroErrorY + (GyroY / 131.0);
  113. GyroErrorZ = GyroErrorZ + (GyroZ / 131.0);
  114. c++;
  115. }
  116. //Divide the sum by 200 to get the error value
  117. GyroErrorX = GyroErrorX / 200;
  118. GyroErrorY = GyroErrorY / 200;
  119. GyroErrorZ = GyroErrorZ / 200;
  120. // Print the error values on the Serial Monitor
  121. Serial.print(“AccErrorX: “);
  122. Serial.println(AccErrorX);
  123. Serial.print(“AccErrorY: “);
  124. Serial.println(AccErrorY);
  125. Serial.print(“GyroErrorX: “);
  126. Serial.println(GyroErrorX);
  127. Serial.print(“GyroErrorY: “);
  128. Serial.println(GyroErrorY);
  129. Serial.print(“GyroErrorZ: “);
  130. Serial.println(GyroErrorZ);
  131. }

Based on 0 reviews

0.0 overall
0
0
0
0
0

Be the first to review “GY-521 Gyroscope Accelerometer Sensor Module”

There are no reviews yet.