How to use the GY521 Gyro & Accelerometer with Arduino

How to use the GY521 Gyro & Accelerometer with Arduino

GY521 Gyro & Accelerometer for Arduino

 

INTRODUCTION

The Gyro (Gyroscope not the food!) and Accelerometer both are used to detect the position and orientation of any device. The Gyro uses earth gravity to determine the x,y and z-axis positions and accelerometer measures acceleration based on the rate of the change of movement. This sensor has both the Gyro and an Accelerometer on one platform which makes it really handy.

APPLICATIONS

Its applications include a hand gesture controlled robot, vehicle accident alert system, earthquake detector alarm and also an aircraft orientation measurement device.

WIRING DIAGRAM

VCC - 5V

GND - GND

SCL - A5

SDA - A4

INT - D2 (used for interrupt)

(The other pins do not need to be connected for this example)

 

GY521 Gyro Arduino Pin Connections

CODE

 

#include <Wire.h>
const int MPU=0x68; 
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B); 
  Wire.write(0);    
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,12,true);  
  AcX=Wire.read()<<8|Wire.read();    
  AcY=Wire.read()<<8|Wire.read();  
  AcZ=Wire.read()<<8|Wire.read();  
  GyX=Wire.read()<<8|Wire.read();  
  GyY=Wire.read()<<8|Wire.read();  
  GyZ=Wire.read()<<8|Wire.read();  
  
  Serial.print("Accelerometer: ");
  Serial.print("X = "); Serial.print(AcX);
  Serial.print(" | Y = "); Serial.print(AcY);
  Serial.print(" | Z = "); Serial.println(AcZ); 
  
  Serial.print("Gyroscope: ");
  Serial.print("X = "); Serial.print(GyX);
  Serial.print(" | Y = "); Serial.print(GyY);
  Serial.print(" | Z = "); Serial.println(GyZ);
  Serial.println(" ");
  delay(333);
}

END RESULT

Open the Serial Monitor where you will see the orientation of this module. Move it to different sides and rotate it to observe the values in the serial monitor change.

ADDITIONAL RESOURCES

https://create.arduino.cc/projecthub/Nicholas_N/how-to-use-the-accelerometer-gyroscope-gy-521-6dfc19

https://www.instructables.com/id/GY-521-MPU6050-3-Axis-Gyroscope-and-Accelerometer-/