Interfacing MPU6050 Sensor with Arduino

Are you Working on a project that requires the use of the MPU6050 Gyroscope? Do you want to know what is MPU6050 Gyroscope sensor and how it works? Do you want to make your own drone or any other RC flying toy?  In this article, we will be covering all these topics and interfacing this sensor with Arduino. I hope you will like this article. We have already uploaded articles on various topics such as Arduino tutorials, Arduino Projects, Embedded Projects, and Basic Electronics on our website. You can check them out if you are a beginner stepping into the new world of electronics and want to learn more.

What is the MPU6050 Gyroscope Sensor?

MPU6050 gyroscope sensor is a MEMS (Micro Electro Mechanical System) based 3-axis(X, Y, and Z) sensor which can measure the tilt or inclination and also measure temperature and also work as an accelerometer. Because of these many functions, this finds excessive use in many areas and DIY projects. One can use this in many projects such as Flying copters and drones which require stability along with directions and velocity to maintain their orientation. Self-balancing robots and toys which are the most trending projects can also be made using this.

How Does the Sensor Work?

The sensor uses capacitive plates to calculate the change in force to calculate the acceleration and orientation of the device. The data is then shared with the controlling device to respond using the I2C communication protocol. This can calculate the acceleration, motion, and direction which makes this ideal for use in any project that requires these functions especially in flying objects such as drones which require precise control over their pitch, yaw, and roll.

The sensor can measure acceleration in the ranges of ±2g, ±4g, ±8g, and ±16g using its Accelerometer sensor. It can measure the change in directions in the range of ±250 degree/s, ±500 degree/s, ±1000 degree/s, and ±2000 degree/s. This sensor also has Additional pins to connect I2C based other external sensors to increase 3 axes to its present capabilities. Although there is a temperature sensor built-in, it reads the temperature of the chip itself and not the ambient temperature. hence, it is only useful mainly for the calibration of the sensor and to get rough temperatures.

Components Required

  • Arduino UNO
  • MPU6050 Gyroscope Sensor
  • Connecting wires
  • Breadboard
  • USB cable to connect Arduino UNO with Computer.

Interfacing MPU6050 Sensor with Arduino Circuit Diagram

Interfacing MPU6050 Sensor with Arduino Circuit Diagram

  • VCC of Sensor to 5V pin of Arduino UNO
  • GND of sensor to GND of Arduino UNO
  • SCL of sensor to A5 pin of Arduino UNO
  • SDA of sensor to A4 pin of Arduino UNO

Interfacing MPU6050 Arduino Code

NOTE- Install the Wire.h in your Arduino IDE before uploading the below code.

download library

 #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(300);  
 }  

After Uploading the code, you can get the data from the sensor to your Arduino, While the data can be used in many ways in some other projects. I hope you liked this tutorial. If you have any queries or doubts, please tell us in the comment section below.