Tutorial: How to use the GY-521 module (MPU-6050 breakout board) with the Arduino Uno

GY-521 breakout board with an MPU-6050 MEMS.

GY-521 breakout board with an MPU-6050 MEMS.

The GY-521 module is a breakout board for the MPU-6050 MEMS (Microelectromechanical systems) that features a 3-axis gyroscope, a 3-axis accelerometer, a digital motion processor (DMP), and a temperature sensor. The digital motion processor can be used to process complex algorithms directly on the board. Usually, the DMP processes algorithms that turn the raw values from the sensors into stable position data. This tutorial gives only a brief introduction to the GY-521/MPU-6050. In particular, it is shown how to retrieve the raw sensor values. The sensor values are retrieved by using the I2C serial data bus, which requires only two wires (SCL and SDA). If you plan to use the full range of features or require reliable and stable position data, then I recommend to have also a look at ready-to-use libraries. Please follow this link to find an excellent library with many examples: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050.

List of materials:
– Arduino Uno [Search on Aliexpress | Amazon]
– Jumper wires [Search on Aliexpress | Amazon]
– Breadboard [Search on Aliexpress | Amazon]
– GY-521 module [Search on Aliexpress | Amazon]

Wiring scheme:
The GY-521 breakout has eight pins:

  • VCC (The breakout board has a voltage regulator. Therefore, you can connect the board to 3.3V and 5V sources.)
  • GND
  • SCL (Serial Clock Line of the I2C protocol.)
  • SDA (Serial Data Line of the I2C protocol.)
  • XDA (Auxiliary data => I2C master serial data for connecting the module to external sensors.)
  • XCL (Auxiliary clock => I2C master serial clock for connecting the module to external sensors.)
  • AD0 (If this pin is LOW, the I2C address of the board will be 0x68. Otherwise, if the pin is HIGH, the address will be 0x69.)
  • INT (Interrupt digital output)

Wiring layout:

Fritzing file that shows how to wire the GY-521 breakout board to an Arduino Uno.

Fritzing file that shows how to wire the GY-521 breakout board to an Arduino Uno.

In this tutorial we will make use only of the first four pins: VCC, GND, SDA, and SCL. First, we connect the module’s VCC to the Arduino’s 5V pin. Then, the module’s GND is connected to one of the Arduino’s GND pins.
Next, we have to set up the I2C connection between the module and the Arduino. Most Arduino Uno variants have an SCL and SDA pin. If you have such an Arduino Uno, just connect SCL to SCL and SDA to SDA.
If you can’t find an SCL and SDA pin on your Arduino, you have to use other pins. Unfortunately, you cannot use just any pin. For each type of Arduino, SCL and SDA are tied to different pins:

  • Arduino Uno, Arduino Ethernet, Arduino Nano: A4 (SDA), A5 (SCL)
  • Arduino Mega2560: 20 (SDA), 21 (SCL)
  • Arduino Leonardo: 2 (SDA), 3 (SCL)
  • Arduino Due: 20 (SDA), 21 (SCL)

So, if you have an Arduino Uno without SCL and SDL pins, then connect the Arduino’s A4 pin to the module’s SDA pin. Next, connect the Arduino’s A5 pin to the module’s SCL pin.

Example source code:

We make use of the Arduino platform’s in-built library (Wire) to establish an I2C connection between the Arduino Uno and the GY-521 sensor. At the beginning of our source code, the Wire library’s header file is included. Next, we define and declare some variables.
Then, a convert-function is defined. The convert-function makes sure that all sensor values have the same width when they are printed out to the serial monitor later.
In the setup function, a serial connection is established. Moreover, we start our first I2C transmission to the GY-521 board to wake it up from sleep mode.
In the loop function, seven sensor values (3x accelerometer, 1x temperature, and 3x gyro) are requested from the GY-521 module. The MPU-6050 has many registers which can be read. Fourteen of these registers contain the sensor values that we need. As a first step, we tell the GY-521 module where we are going to start reading (“Wire.write(0x3B);”). Then, we request to read 14 registers (“Wire.requestFrom(MPU_ADDR, 7*2, true);”). If you are wondering, why 14 registers are read instead of 7 registers, the reason is quite simple: Each sensor value has a size of 2 byte. As each register has a size of one byte, a single sensor value must be retrieved by accessing two registers. The first register contains the so-called “high byte” and the second register contains the “low byte”. Next, all values are retrieved and printed out to the serial connection. At the end of the loop function, a delay of one second is added in order to avoid flooding the serial monitor with messages.

// (c) Michael Schoeffler 2017, http://www.mschoeffler.de

#include "Wire.h" // This library allows you to communicate with I2C devices.

const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.

int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data

char tmp_str[7]; // temporary variable used in convert function

char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
  sprintf(tmp_str, "%6d", i);
  return tmp_str;
}

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
  Wire.write(0x6B); // PWR_MGMT_1 register
  Wire.write(0); // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
}
void loop() {
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
  Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
  Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers
  
  // "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
  accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
  
  // print out data
  Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
  Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
  Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));
  // the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
  Serial.print(" | tmp = "); Serial.print(temperature/340.00+36.53);
  Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
  Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
  Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));
  Serial.println();
  
  // delay
  delay(1000);
}

If the code is compiled and transferred to the Arduino Uno, you should see the sensor values in the serial monitor of the Arduino IDE. Moreover, when the GY-521 board is rotated or moved, the sensor values should change according to the movement.
As mentioned before, this is basically a “hello world program” for the GY-521. if you plan to use the board more seriously, I highly recommend to dig deeper into the possibilities of the MPU-6050 MEMS.

If the GY-521 is moved (left hand side), the sensor values should change accordingly (right hand side).

If the GY-521 is moved (left hand side), the sensor values should change accordingly (right hand side).

Video Tutorial:

28 thoughts on “Tutorial: How to use the GY-521 module (MPU-6050 breakout board) with the Arduino Uno

  1. Tony Boorer

    Hi – Thank you for this tutorial. Could you give me your recommended method for getting the accelerometer to talk to music software on a MAC so that it will control patches through gesture. I use Ableton Live with MAX for Live . Will in talk to the software via midi and OSC – using a bridge such as Osculator or similar ? Thanks

    Reply
    1. mschoeffler Post author

      Hi Tony, that’s a tough one ;) What about using an Arduino in-between the accelerometer and the MAC? I’ve seen some projects in which an Arduino is used as MIDI device (e.g., this might be a good start => https://www.arduino.cc/en/Reference/MIDIUSB). In the end, the setup would look like this: GY-521 <= I2C => Arduino <= MIDI => MAC/Ableton.

      Reply
  2. Pingback: Example Application: GY-521 module (MPU-6050 breakout board) and Arduino Uno | Michael Schoeffler

  3. kiarash

    I really thank you for your help. you made my day. now i can complete my project with your help. it was one of the best videos on earth because it was so clear.

    Reply
  4. Tony Foale

    Many thanks for this. i am capable of having done similar myself but your code has saved me a lot of time.
    I am impressed that you did this without a bunch of 3rd party libraries. having found your site I shall view your other projects with good expectations.

    Reply
  5. Paul

    Hi can this be used to also measure from 3 MPU6050 sensors? Another question, where can you set if the sensors will measure in +-2g or +-4g and+-8g?

    Reply
    1. mschoeffler Post author

      Regarding your first question: Two sensors should work smoothly since you are able to change the SPI address with the A0 input (0x68/0x69). To be honest, I don’t have the perfect solution for a third sensor in mind right now. I would start by looking into how to change the third sensor to another address (which is not 0x68 or 0x69).

      Reply
    1. mschoeffler Post author

      Imho you can use the same code for the Arduino Nano. In case you run into any problems, my first look would be at the pin configuration for I2C.

      Reply
  6. Peter Charlesworth

    Great thanks. It worked first time. It’s my first board to connect and get running on Arduino and Its great that I get some output. Any recommendations for using the other features on board would be appreciated.
    I’ll take a look at the manual and see what I can do next.
    The English language manual is not yet released, not so good!

    Reply
  7. Pingback: Beginners Guide on how to start with the Arduino Ecosystem + electronics | Michael Schoeffler

    1. mschoeffler Post author

      A thought: high standard deviation within a specific time-frame (e.g. 5sec) might indicate that an accident has occurred.

      Reply
  8. Tyrone

    Hi Michael

    Thanks for an awesome tutorial. Would you have any idea on how to communicate with multiple MPU6050 with a single Arduino?

    I am a masters student at a local university and would like some assistance if you can help me.

    Reply
    1. mschoeffler Post author

      Some thoughts:
      – The second MPU need another address (you can change the address with pin A0)
      –> const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.

      – You need to start two transmissions, where the second transmission has another address
      –> Wire.beginTransmission(MPU_ADDR);

      Reply
  9. Fred

    Could you please provide code to write this data to an SD card. It works great on the serial monitor but there is not much I can do with that. I apologize if this is an amateur question but I am! Thank you for all your time.

    Reply
  10. Joshua A

    Very nice tutorial and well worded I was thinking on integrating this into project of mine. For the sample code I was wondering what type of license are you using? Is the license MIT? or other type of copyright license. Also, thanks for the tutorial.

    Reply
    1. mschoeffler Post author

      Adding a license text to all my examples is on my bucket list for 2020 –> probably MIT.

      Reply
  11. Troels Søgaard Bock

    Hi Michael,

    Great tutorial! Do you know if it is possible to add two accelerometers to one Arduino and compare the accelerations? If yes, how would you do it?

    Kind regards
    Troels

    Reply
    1. mschoeffler Post author

      Yes that should be possible. Some thoughts:
      – Second accelerometer must have a different address (see MPU_ADDR, AD0 pin)
      – You have to start two transmissions –> Wire.beginTransmission(MPU_ADDR); One transmission goes to the first accelerometer, and the other one to the second accelerometer.

      Reply

Leave a Reply to Tony Boorer Cancel reply

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.