GY-521 MPU6050 3 Axis Accelerometer Gyroscope Module Review and Code

This post talks about using the GY-521 MPU6050 3 Axis Accelerometer Gyroscope with an Arduino.

GY-521 MPU6050 3 Axis Accelerometer Gyroscope

The GY-521 MPU6050 3 Axis Accelerometer Gyroscope is a relatively complex module.

The gyroscope allows you to tell both the angle it is positioned in all three axis directions and the movement in each of those directions. In addition this is able to report the temperature of the die so it can be used to monitor the rough temperature.

Opposite to the pins there are two mounting holes which allow you to attach it to something to be held in place.

Wiring it up to an Arduino

This sensor have 8 pins but you will typically only need to use 4 of them to connect to your Arduino.

Pin on SensorPin on ArduinoComments
SCLA5 or SCLFor most Arduino’s the SCL pin will be A5. However you may need to confirm this.
SDAA4 or SDAFor most Arduino’s the SDA pin will be A4. However you may need to confirm this.
XDANot connectedAuxiliary Serial Data (Which can be used to connect another device to the MPU6050)
XCLNot connectedAuxiliary Serial Clock (Which can be used to connect another device to the MPU6050)
AD0Not connected / GND / HIGHThis pin allows modifying of the I2C address which will allow you to use up to two of these on the same I2C bus.
If it is wired to “Logic Low” then the address of the device will be 1101000
If it is wired to “Logic High” then the address of the device will be 1101001
INTNot connectedThis pin does not need to be connected.

To use it with an Arduino I am wiring up VCC, GND, SCL and SDA.

Programming the Arduino with the GY-521 MPU6050 3 Axis Accelerometer Gyroscope

To talk to the GY-521 MPU6050 3 Axis Accelerometer Gyroscope I am going to use the MPU6050 Light Arduino Library. This will mean that I do not need to write the specific I2C pulses to talk to the sensor.

You can add this to the Arduino IDE by importing the “MPU6050_light” by rfetick which can be done using “manage library” section or by downloading it from Github.

This exposes some useful functions such as getTemp() and getAngleX/Y/Z() which allows you to easily access the data. Below is some example code to use the gyroscope.

#include "Wire.h"
#include <MPU6050_light.h>
MPU6050 mpu(Wire);

void setup() {

void loop() {
  float tmp = mpu.getTemp();
  float angle[3] = {mpu.getAngleX(),mpu.getAngleY(),mpu.getAngleZ()};
  float gyro[3] = {mpu.getGyroX(), mpu.getGyroY(), mpu.getGyroZ()};
  float accel[3] = {mpu.getAccX(), mpu.getAccY(), mpu.getAccZ()};

  Serial.print("Device Temperature: ");Serial.print(tmp);Serial.println(" Celsius");
  Serial.print("Angles (degrees): x=");Serial.print(angle[0]);
  Serial.print(", y=");Serial.print(angle[1]);
  Serial.print(", z=");Serial.println(angle[2]);

  Serial.print("Gyro (degrees/s): x=");Serial.print(gyro[0]);
  Serial.print(", y=");Serial.print(gyro[1]);
  Serial.print(", z=");Serial.println(gyro[2]);

  Serial.print("Acceleration (g aka 9.81m/s^2): x=");Serial.print(accel[0]);
  Serial.print(", y=");Serial.print(accel[1]);
  Serial.print(", z=");Serial.println(accel[2]);

First I import the two libraries needed (Wire and MPU6050_light) so that they can be accessed and used. Once done I then set up an instance of the MPU6050 object with the Wire object.

In the setup function the serial connection is set up so we can output the data. Then the wire and MPU6050 is initialised and the gyroscope offsets are calculated. This sets up the MPU6050 object so that it is ready to receive data from the chip.

In the loop function we call the update function which polls the device for data and then call a variety of data functions to get the angles, gyroscope angle data, and accelerometer data. The functions available from this library are as follows.

getAccX-Y-Z()Acceleration on X, Y or Zg (=9.81m/s²)
getGyroX-Y-Z()Angular speed en X, Y or Zdeg/s
getAccAngleX-Y()Angles computed from accelero data. Warning: noisydeg
getAngleX-Y-Z()Angles computed with complementary filterdeg
getTemp()Device temperature°C
Data taken from Github

When I run the program it will print out the following information:

Device Temperature: 30.01 Celsius
Angles (degrees): x=1.13, y=5.86, z=0.12
Gyro (degrees/s): x=0.09, y=0.29, z=0.17
Acceleration (g aka 9.81m/s^2): x=-0.09, y=0.02, z=1.02

Final Review

This sensor combined with the MPU6050 light library is an easy to use gyroscope and accelerometer. You are able to easily get information about how fast the sensor is moving (acceleration) and also the angles at which it is being held which gives it a wide range of uses.

In addition reporting the temperature of the chip can be used to see roughly the temperature around the chip.

This is a relatively easy to use chip and for any project where I need to know the movement speed or direction I would consider using it.

Would buy again!

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.