How to read MPU-6050 using Arduino?

In this Arduino with MPU-6050 beginner tutorial we will show how to interface MPU-6050 with Arduino and how to get gyroscope and accelerometer reading and simply display on serial monitor. MPU-6050 can be used to make variety of innovative application for example see Virtual Reality Example using Arduino and MPU-6050 sensor. Learning how to use the sensor in your next project can be useful.

About MPU-6050

The MPU-6050 is an Integrated Circuit(IC) which consist of rotation, acceleration and temperature sensor and Digital Motion Processor (DMP). Thus it is used to detect rotation(angular momentum), acceleration and temperature. Instruments that measures rotation are called gyroscopes and instruments that measures acceleration is called accelerometers. MPU-6050 can measure rotation around all 3 axis(roll, pitch, jaw) and can measure acceleration in all 3-axis(x, y and z-axis) and hence MPU-6050 is called 3-axis gyroscope, 3-axis accelerometer. The accelerometer and gyroscope inside MPU-6050 are made up of MEMS(Micro Electro Mechanical Systems) sensor technology. These MEMS sensors detects rotation and acceleration due to vibration in different orientation. The vibration are converted to capacitance changes which are in turn converted to voltage signal.

The MPU-6050 are available on break out board such as GY-512. The following picture shows GY-512 MPU6050 break out board with MPU-6050, 3.3V voltage regular, led on it.

GY-512 MPU6050

The schematic circuit of the
above GY-512 MPU6050 break out board is below.

Specification and Application of MPU6050

Major Specifications:

- Contains 16 bit ADC(Analog to Digital Converter) 

- 16 bit data output

- Follows IIC communication protocol standard 

- Gryo Range: ± 250 500 1000 and 2000 degree/s 

 - Acceleration Range: ± 2g ± 4g ± 8g and ± 16g
- Motion sensing games
- Augmented Reality application
- Electronic Image Stabilization
- Optical Image Stabilization
- Zero touch Gesture User Interface
- Pedestrian Navigation 
- Motion, shock, vibration detection sensor

Interfacing and Schematic Circuit of Arduino Nano and MPU-6050

The following is schematic circuit wiring diagram of connecting Arduino Nano with MPU-6050.

Schematic Circuit of Arduino Nano and MPU-6050
Four connection are needed to communicate Arduino and MPU6050 which are:

- Arduino Nano Pin A4(SDA) is connected to MPU-6050 SDA pin.

- Arduino Nano Pin A5(SCL) is connected to MPU-6050 SCL pin.

- MPU6050 VCC pin is connected to +5V pin of Arduino Nano

- MPU6050 GND pin is connected to GND pin of Arduino Nano 


The following picture shows MPU6050 Arduino wiring via breadboard.

MPU-6050 Arduino

Programming & Coding

In this section we will show how to get sensor reading from MPU6050 using two libraries- Adafruit and Jess Rowberg.


A. Using Adafruit library

In this tutorial we will be using Adafruit MPU6050 library so if so to follow this tutorial you have to install the following libraries:

1) Adafruit MPU6050

2) Adafruit Unified Sensor Driver

3) Adafruit Bus IO Library

To install the libraries open the library manager in the Arduino IDE. To do this go to Tools > Manage Libraries. In the search bar, type the above libraries and install them.


Program Code

To read the angular momentum, acceleration and temperature we have the following code:

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup(void) {

  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
  Serial.println("MPU6050 Found!");

  // set accelerometer range to +-8G

  // set gyro range to +- 500 deg/s

  // set filter bandwidth to 21 Hz


void loop() {
  /* Get new sensor events with the readings */
  sensors_event_t a, r, t;
  mpu.getEvent(&a, &r, &t);

  /* Print out the values */
  Serial.print("Acceleration X: ");
  Serial.print(", Y: ");
  Serial.print(", Z: ");
  Serial.println(" m/s^2");

  Serial.print("Rotation X: ");
  Serial.print(", Y: ");
  Serial.print(", Z: ");
  Serial.println(" rad/s");

  Serial.print("Temperature: ");
  Serial.println(" degC");


The following shows the data sample read from MPU6050 and displayed on the serial monitor.

B. Using Korneliusz Jarzębski MPU-6050 library

Another library useful to get data from MPU6050 is the Korneliusz Jarzębski mpu6050 library. This can be downloaded from the GitHub link below.

Download the zip file as shown below.

To add this library go to Sketch > Include Library  and click on Add .ZIP library as shown in the ffigure below.

Then browse to the ZIP file where you downloaded it previously and add it.

To check the library installation and see example of using the library to to File > Examples and then find Arduino-MPU6050-master. Within it you will find examples of using this library.

Here for illustration example, we will open the MPU6050_gryo_roll_pitch_yaw sketch. 

If you run the sketch you will get the output on the serial monitor which is shown below.

The following the Arduino sketch to read the pitch, roll and yaw value from MPU6050.

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

// Timers
unsigned long timer = 0;
float timeStep = 0.01;

// Pitch, Roll and Yaw values
float pitch = 0;
float roll = 0;
float yaw = 0;

void setup() 

  // Initialize MPU6050
  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
  // Calibrate gyroscope. The calibration must be at rest.
  // If you don't want calibrate, comment this line.

  // Set threshold sensivty. Default 3.
  // If you don't want use threshold, comment this line or set 0.

void loop()
  timer = millis();

  // Read normalized values
  Vector norm = mpu.readNormalizeGyro();

  // Calculate Pitch, Roll and Yaw
  pitch = pitch + norm.YAxis * timeStep;
  roll = roll + norm.XAxis * timeStep;
  yaw = yaw + norm.ZAxis * timeStep;

  // Output raw
  Serial.print(" Pitch = ");
  Serial.print(" Roll = ");
  Serial.print(" Yaw = ");

  // Wait to full timeStep period
  delay((timeStep*1000) - (millis() - timer));

Following is short video demonstration.

In summary, we have shown how to read acelerometer, gyroscope and temperature data from MPU-6050 gy521 module using two MPU6050 library with Arduino Nano.

Post a Comment

Previous Post Next Post