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() {
  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)
void loop() {
  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));
  // delay

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:

59 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

    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.

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

  3. Shengjie

    Hi Michael,
    Thanks for our demo code for I2C transmission, however, if I modified your delay into lets say 10ms, the serial monitor port stop refreshing. I guess it’s like what you said a “flooding the serial monitor with messages”. How to avoid that, and what’s the cause of that.

    Best Regards!

  4. Maurizio

    Hallo Michael, I would use the GY-521 to build a simply shoot training sistem: one point on the screen (using Processing software) that moves in all directions using the GY-521 installed under a gun and the data X,Y,Z read from board. Do you think is it possibile ? How ?
    Thanks for your reply

  5. Corian

    Hallo Michael,

    Ich habe das Programm mal abgeschrieben und in zwei Arduino geladen.
    ( meine Programmierkenntnisse sind eher mangelhaft)
    An den Arduino ( chin. Nano / chin. ProMini ) sind jeweils ein MPU6050 (GY521)
    Der Nano werkelt einwandfrei, Daten kommen am Serial-monitor an, und ändern sich bei bewegung.

    Der ProMini zeigt auf allen Achsen nur -1 und sonst macht er….. nichts?
    – beschaltung beide Arduino ist gleich, GND, Vcc, INT, scl,sda
    Wo beginnt die Fehlersuche?
    Mfg Corian

  6. Arfa Hassan

    Can you please tell me what algorithm is responsible for the interfacing of IMU sensor with Arduino.
    I have used MPU 6050 with Arduino for fault detection; I am searching for the relevant algorithm to validate my experimentation of detecting fault by connecting multiple sensors with Arduino and recording result.

  7. Hassan Arfa

    I have used Arduino interfaced with MPU6050 for fault detection. (Introduced fault and recorded the results). I want some help in identifying the algorithm for this fault detection for validation purpose.
    Have you used any algorithm (on which you based your fault detection methodology)?

  8. Joe

    Hello, thank you for your tutorial. I followed the instructions in the video and on this page, but when I run the application and view the monitor the values do not change.

    aX = -1 | aY = -1 | aZ = -1 | tmp = 36.53 | gX = -1 | gY = -1 | gZ = -1

    Any idea what could be wrong? Ive triple checked the connections, not sure what else could be the issue.


      1. mattD

        With either 0x68 or 0x69 setting I get same
        aX = -1 | aY = -1 | aZ = -1 | tmp = 36.53 | gX = -1 | gY = -1 | gZ = -1

  9. Valentin

    Hi, thank you for this tutorial!
    I have a question and I hope you have the answer,
    I have to use this sensor for a project and I was wondering myself what is the unit of the figures given by arduino,
    I guess that the temperature is in Celsius but what about the gyro and the accelerometer? Are they hexadecimals value?
    Thanks in advance

  10. Jakob

    I would like to measure distance. I will press button first time and then move it and press button second time and than it would measure made distance betwen these two points. Is it posible to made this and how? Thanks

    1. mschoeffler Post author

      GY-521/MPU6050 measures rotational movements. I guess what you need is something that is able to measure translational movements (e.g. ultrasonic, GPS, camera-based,…)

      1. Chuck Summers

        equations of motion
        x = x0 + v0*Ax + 0.5*Ax*t^2
        x0 initial location
        v0 initial velocity (normally 0)
        Ax is acceleration in x dimension.

  11. 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.

  12. AL20878

    This expression: “accelerometer_x = Wire.read()<<8 | Wire.read();" (and all other similar in the code sample above) is not well-formed because you cannot assume the first read() occurs before the second. Remember, in C(++) the expression terms can be evaluated in any order (the only exceptions are the logical ||, && and the ternary ?:), and the order can change between the versions of the same compiler, as well as depending on whether or not the compilation is optimized (even for the same version of the compiler!). As a result, there's no guarantee in the above expression that the bytes will be placed in the right order into the accelerometer_x variable. A well-formed program would use a series of sequential assignments, something like that:
    accelerometer_x = Wire.read();
    accelerometer_x <<= 8;
    accelerometer_x |=Wire.read();
    (the first two can be combined into one "Wire.read() << 8", but IMO three-statement looks more clear and uniform).

  13. 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.

  14. 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?

    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).

  15. David

    How do you change the scale of the accelerometer reading in the code? It seems to be stuck on the +/-2g setting despite the datasheet saying it can be adjusted

    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.

  16. choch

    I have the GY-521 Hiletgo Gyro/Acc hooked up to the Raspberry Pi.

    I am not getting Accelerometer data at all. Do you have any idea what could be wrong ?

  17. 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!

  18. 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.

  19. 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.

    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);

  20. 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.

  21. 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.

    1. mschoeffler Post author

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

  22. 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

    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.

  23. Peter B

    Hello, I tried using your code however it prints this out over and over no matter the orientation:
    aX = -1 | aY = -1 | aZ = -1 | tmp = 36.53 | gX = -1 | gY = -1 | gZ = -1
    Could you please help me out?

  24. Sammy Heslop

    Hello! Thank you for the tutorial – really helped with setting up!

    I too am having the same problem where my code is producing a print of :

    aX = -1 | aY = -1 | aZ = -1 | tmp = 36.53 | gX = -1 | gY = -1 | gZ = -1

    Does anyone have any idea what is causing the problem? Thank you!

  25. mark

    I got the same problem after running this code :
    aX = -1 | aY = -1 | aZ = -1 | tmp = 36.53 | gX = -1 | gY = -1 | gZ = -1

    the connection and WhoAmI returns correct.


Leave a 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.