Arduino Accelerometer & Gyroscope Tutorial – MPU-6050 6DOF

///Arduino Accelerometer & Gyroscope Tutorial – MPU-6050 6DOF

Arduino Accelerometer & Gyroscope Tutorial – MPU-6050 6DOF

Arduino MPU-6050 Accelerometer & Gyro Implementation

In this tutorial, we will be walking you through the MPU-6050 implementation. We will show you exactly what you need to do in order to retrieve all the important data. We will be also explaining what you need to know about the registers used in the MPU. Lastly, the final program will output all of the X,Y & Z readings for the Accelerometer and Gyroscope.

The importance of a datasheet

We’ve mentioned it several times on my YouTube video and will say it again. You need to become familiar with the datasheet. Within this document, lie the answers to all of your questions. I have referred to the document on multiple occasions throughout the video and recommend that you do the same.
MPU 6050 Datasheet: Click Here
MPU 6050 Register Reference: Click Here

Hardware & Software for the Project

In this project, we will be using the MPU-6050 as well as an Arduino Uno R3. You can purchase an MPU-6050 directly through this website: MPU-6050 Accelerometer & Gyroscope. For simplicity purposes, the MPU-6050 breakout board is placed on a breadboard which is hooked back to the Arduino with jumper cables.
The connections are quite simple. You will need to connect the SCL pin on the MPU to the SCL pin on the Arduino. Repeat for the SDA pin and the VCC & GND pins. Follow the pinout below to locate the SCL and SDA pins which are labeled as A4 and A5 on the Arduino Uno R3.
Arduino Uno R3 Pinout

The full program we will be implementing in this project is available here: Arduino MPU-6050 Implementation. The Arduino IDE used is version 1.6.9. Lastly, we will be utilizing the Arduino Wire library which you can reference here: Arduino Wire Library.

Reading the data from the MPU-6050 registers

In this section, we will be going over the program which was written for this tutorial. Each section will be explained in detail. If you have any questions after you’ve gone through the tutorial, feel free to post them below.

long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
void setup() {

In the code above, we begin by importing the Wire library. We then create all of the required variables to store our future data. Lastly, the setup function is initiating Serial communication, begins Wire transmission and calls the setupMPU() function.

void setupMPU(){
Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
Wire.write(0b00000000); //Setting the accel to +/- 2g

The above code shows the setupMPU() function. The purpose of this function is to establish all of the required parameters on the MPU in order to begin communication. The function begins with a beginTransmission(0b1101000) command. The address which is passed as a parameter is obtained from the MPU datasheet. It specifies that the I2C address shall be 0b110100X where X is the voltage on the AD0 pin. This pin is normally pulled down. Pull it to VCC if you wish to change the address to 0b1101001.

The following instructions are accessing different registers on the MPU. The first one is the 0x6B register. If you refer to the datasheet and register map, you will notice that this register needs to be set to 0 in order to turn-off SLEEP mode.

The 1B and 1C registers are responsible for the Gyroscope and Accelerometer settings. They dictate what the limits will be once you start polling the data. They also have extra testing features which will not be covered in this tutorial. However, you are encouraged to explore them through the documentation.

Each segment has the MPU address, register address, the bits to be written to the register and a termination statement.

void loop() {

The loop function contains two read functions and a printData() function. The record functions are responsible for storing data where appropriate and the printData() function will output the data to the Serial port.

void recordAccelRegisters() {
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x3B); //Starting register for Accel Readings
Wire.requestFrom(0b1101000,6); //Request Accel Registers (3B - 40)
while(Wire.available() < 6); accelX =<<8|; //Store first two bytes into accelX accelY =<<8|; //Store middle two bytes into accelY accelZ =<<8|; //Store last two bytes into accelZ processAccelData(); } void processAccelData(){ gForceX = accelX / 16384.0; gForceY = accelY / 16384.0; gForceZ = accelZ / 16384.0; } void recordGyroRegisters() { Wire.beginTransmission(0b1101000); //I2C address of the MPU Wire.write(0x43); //Starting register for Gyro Readings Wire.endTransmission(); Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 - 48) while(Wire.available() < 6); gyroX =<<8|; //Store first two bytes into accelX gyroY =<<8|; //Store middle two bytes into accelY gyroZ =<<8|; //Store last two bytes into accelZ processGyroData(); } void processGyroData() { rotX = gyroX / 131.0; rotY = gyroY / 131.0; rotZ = gyroZ / 131.0; }

The function for the Gyro is very similar to the one for the Accelerometer. The purpose of these functions is to initiate transmission and to store the data which is sent back on the I2C bus. Notice that the data will be stored in yet another set of registers on the MPU-6050. For the Accelerometer, you will find it in the 3B-40, where as the Gyroscope, it will be in the 43-48 registers. Similarly to the example before, we initiate communication with the MPU, send the address of the first register, then wait for the 6 bytes to come back. Note that you need to reassemble the data via a bit shift operator (<< or >>) and store it.

The final functions (processAccelData() and processGyroData()) are required in order to translate the data into something meaningful. They take the raw values and divide them by the sensitivity values for the full scale ranges you've selected. These operations will give a final result of degrees/second for the gyroscope and gs for the accelerometer.

void printData() {
Serial.print("Gyro (deg)");
Serial.print(" X=");
Serial.print(" Y=");
Serial.print(" Z=");
Serial.print(" Accel (g)");
Serial.print(" X=");
Serial.print(" Y=");
Serial.print(" Z=");

The print function is straight forward. It will output all of the processed data onto the serial port for us to see.

The purpose and functionality of Accelerometer and Gyroscope

If you've gotten this far, you may be wondering what does the data represent exactly. The answer is quite simple. The gyroscope will measure the rotational speed around any of the 3 axis. The accelerometer will measure the forces acting along any of the 3 axis. With both of these tools, you can easily build a self balancing robot, a quadcopter, a positioning robot, etc.


At this point, you should be fully equipped to get the MPU-6050 setup on your own. If you have any questions, feel free to comment in the section below.

Thank you for reading & watching,
- EEEnthusiast

By | 2017-05-16T20:28:10-07:00 July 20th, 2016|Arduino, Tutorials|4 Comments

About the Author:

Welcome to My name is Vlad, I'm an Electrical Engineer and I love to build and teach. I graduated in 2013 and have tried my best to share my electrical knowledge and experience with viewers and readers from all over the world. You can find me on my personal site or LinkedIn!


  1. Cronus Phone August 22, 2016 at 11:13 am

    so could send me a schematic of this.

  2. naqash khan August 27, 2016 at 4:52 am

    i am not able to read my GYRO readings correctly, it is best with ACCEL readings.

    when I tilt my MPU the rates of velocity changes, but they are not set at one reading i.e (0) at stationary position. 🙁

  3. Malav Shastri September 14, 2016 at 2:36 am

    Thank you very much for such informative video.
    I want to control 8 motors in set of 5 and 3 . i.e. when set of 5 motors are controlled the set of 3 motors should remain inactive. Also the motors will be controlled using this sensor data. Could you help me with the concept and coding required? Awaiting reply.

    Thanks and regards,
    Malav Shastri

  4. Abhinav Garg May 6, 2018 at 10:02 am

    I am using stm32 board to access the values of mpu6050, the values are coming right initially but it gradually gets settled to zero.

Leave A Comment

CommentLuv badge

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

Show Buttons
Hide Buttons