MPU 6050 Gyro,Accelerometer Communication With Arduino (Atmega328p)

Introduction: MPU 6050 Gyro,Accelerometer Communication With Arduino (Atmega328p)

About: DIY projects

The MPU6050 IMU has both 3-Axis accelerometer and 3-Axis gyroscope integrated on a single chip.

The gyroscope measures rotational velocity or rate of change of the angular position over time, along the X, Y and Z axis.

The outputs of the gyroscope are in degrees per second, so in order to get the angular position we just need to integrate the angular velocity.

On the other hand, the MPU6050 accelerometer measures acceleration by measuring gravitational acceleration along the 3 axes and using some trigonometry math we can calculate the angle at which the sensor is positioned. So, if we fuse, or combine the accelerometer and gyroscope data we can get very accurate information about the sensor orientation.

3-axis Gyroscope
The MPU-6050 consist of a 3 axis gyroscope which can detect rotational velocity along the x,y,z axis with micro electro mechanical system technology (MEMS). When the sensor is rotated along any axis a vibration is produced due to Coriolis effect which is detected by the MEMS.16-bit ADC is used to digitize voltage to sample each axis.+/- 250, +/- 500, +/- 1000, +/- 2000 are the full scale range of output.Angular velocity is measured along each axis in degree per second unit.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: MPU-6050 Module

The MPU-6050 module has 8 pins,

INT: Interrupt digital output pin.

AD0: I2C Slave Address LSB pin. This is 0th bit in 7-bit slave address of device. If connected to VCC then it is read as logic one and slave address changes.

XCL: Auxiliary Serial Clock pin. This pin is used to connect other I2C interface enabled sensors SCL pin to MPU-6050.

XDA: Auxiliary Serial Data pin. This pin is used to connect other I2C interface enabled sensors SDA pin to MPU-6050.

SCL: Serial Clock pin. Connect this pin to microcontrollers SCL pin. SDA: Serial Data pin. Connect this pin to microcontrollers SDA pin.

GND: Ground pin. Connect this pin to ground connection.

VCC: Power supply pin. Connect this pin to +5V DC supply. MPU-6050 module has Slave address (When AD0 = 0, i.e. it is not connected to Vcc) as,

Slave Write address(SLA+W): 0xD0

Slave Read address(SLA+R): 0xD1

Step 2: Calculations

Gyroscope and Accelerometer sensor data of MPU6050 module consists of 16-bit raw data in 2’s complement form.

Temperature sensor data of MPU6050 module consists of 16-bit data (not in 2’s complement form).

Now suppose we have selected,

  • - Accelerometer full scale range of +/- 2g with Sensitivity Scale Factor of 16,384 LSB(Count)/g.
  • - Gyroscope full scale range of +/- 250 °/s with Sensitivity Scale Factor of 131 LSB (Count)/°/s. then,

To get sensor raw data, we need to first perform 2’s complement on sensor data of Accelerometer and gyroscope. After getting sensor raw data we can calculate acceleration and angular velocity by dividing sensor raw data with their sensitivity scale factor as follows--

Accelerometer values in g (g force)

  • Acceleration along the X axis = (Accelerometer X axis raw data/16384) g.
  • Acceleration along the Y axis = (Accelerometer Y axis raw data/16384) g.
  • Acceleration along the Z axis = (Accelerometer Z axis raw data/16384) g.

Gyroscope values in °/s (degree per second)

  • Angular velocity along the X axis = (Gyroscope X axis raw data/131) °/s.
  • Angular velocity along the Y axis = (Gyroscope Y axis raw data/131) °/s.
  • Angular velocity along the Z axis = (Gyroscope Z axis raw data/131) °/s.

Temperature value in °/c (degree per Celsius)

Temperature in degrees C = ((temperature sensor data)/340 + 36.53) °/c.

For example,

Suppose, after 2’ complement we get accelerometer X axes raw value = +15454

Then Ax = +15454/16384 = 0.94 g.

More,

So we know we are running at a sensitivity of +/-2G and +/- 250deg/s but how do our values correspond to those accelerations/angles.

These are both straight line graphs and we can work out from them that for 1G we will read 16384 and for 1degree/sec we will read 131.07(Although the .07 will get ignore due to binary) these values were just worked out by drawing the straight line graph with 2G at 32767 and -2G at -32768 and 250/-250 at the same values.

So now we know our sensitivity values (16384 and 131.07) we just need to minus the offsets from our values and then devide by the sensitivity.

These will work fine for the X and Y values but as the Z was recorded at 1G and not 0 we will need to minus off 1G (16384) before we divide by our sensitivity.

Step 3: MPU6050-Atmega328p Connections

  • Just Connect everyting as given in the diagram....

The connections are given as follows:-

  • MPU6050<======> Arduino Nano.

VCC <======> 5v out pin

GND <======>Ground pin

SDA <======> A4 pin //serial data

SCL<======> A5 pin // serial clock

Pitch and Roll Calculation:
Roll is the rotation around x-axis and pitch is the rotation along y-axis.

The result is in radians. (convert to degrees by multiplying by 180 and dividing by pi)

Step 4: Codes and Explanations

#include <Wire.h>

const int MPU=0x68; //I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; //16-bit integers
int AcXcal,AcYcal,AcZcal,GyXcal,GyYcal,GyZcal,tcal; //calibration variables
double t,tx,tf,pitch,roll;

void setup()
{
    Wire.begin(); //initiate wire library and I2C
    Wire.beginTransmission(MPU); //begin transmission to I2C slave device
    Wire.write(0x6B); // PWR_MGMT_1 register
    Wire.write(0); // set to zero (wakes up the MPU-6050)  
    Wire.endTransmission(true); //ends transmission to I2C slave device
    Serial.begin(9600); //serial communication at 9600 bauds
}</p><p>void loop()
{
    Wire.beginTransmission(MPU); //begin transmission to I2C slave device
    Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false); //restarts transmission to I2C slave device
    Wire.requestFrom(MPU,14,true); //request 14 registers in total  </p><p>    //Acceleration data correction
    AcXcal = -950;
    AcYcal = -300;
    AcZcal = 0;</p><p>    //Temperature correction
    tcal = -1600;</p><p>    //Gyro correction
    GyXcal = 480;
    GyYcal = 170;
    GyZcal = 210;</p><p>    //read accelerometer data
    AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) 0x3C (ACCEL_XOUT_L)  
    AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) 0x3E (ACCEL_YOUT_L) 
    AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) 0x40 (ACCEL_ZOUT_L)
  
    //read temperature data 
    Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) 0x42 (TEMP_OUT_L) 
  
    //read gyroscope data
    GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) 0x44 (GYRO_XOUT_L)
    GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) 0x46 (GYRO_YOUT_L)
    GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) 0x48 (GYRO_ZOUT_L) </p><p>    //temperature calculation
    tx = Tmp + tcal;
    t = tx/340 + 36.53; //equation for temperature in degrees C from datasheet
    tf = (t * 9/5) + 32; //fahrenheit</p><p>    //get pitch/roll
    getAngle(AcX,AcY,AcZ);
  
    //printing values to serial port
    Serial.print("Angle: ");
    Serial.print("Pitch = "); Serial.print(pitch);
    Serial.print(" Roll = "); Serial.println(roll);
  
    Serial.print("Accelerometer: ");
    Serial.print("X = "); Serial.print(AcX + AcXcal);
    Serial.print(" Y = "); Serial.print(AcY + AcYcal);
    Serial.print(" Z = "); Serial.println(AcZ + AcZcal); </p><p>    Serial.print("Temperature in celsius = "); Serial.print(t);  
    Serial.print(" fahrenheit = "); Serial.println(tf);  
  
    Serial.print("Gyroscope: ");
    Serial.print("X = "); Serial.print(GyX + GyXcal);
    Serial.print(" Y = "); Serial.print(GyY + GyYcal);
    Serial.print(" Z = "); Serial.println(GyZ + GyZcal);
  
    delay(1000);
}

//function to convert accelerometer values into pitch and roll
void getAngle(int Ax,int Ay,int Az) 
{
    double x = Ax;
    double y = Ay;
    double z = Az;    pitch = atan(x/sqrt((y*y) + (z*z))); //pitch calculation
    roll = atan(y/sqrt((x*x) + (z*z))); //roll calculation</p><p>    //converting radians into degrees
    pitch = pitch * (180.0/3.14);
    roll = roll * (180.0/3.14) ;
}
-----------------------------------------------------------------------------------------------
Results:-

--------------------------------------------------------------------------------------

Angle: Pitch = 88.89 Roll = -0.47<br>Accelerometer: X = 15974 Y = -440 Z = 312
Temperature in celsius = 29.38 fahrenheit = 84.88
Gyroscope: X = -111 Y = 341 Z = 211
Angle: Pitch = 89.41 Roll = -0.27
Accelerometer: X = 16102 Y = -380 Z = 172
Temperature in celsius = 29.42 fahrenheit = 84.96
Gyroscope: X = -115 Y = 373 Z = 228
Angle: Pitch = 89.28 Roll = -0.34
Accelerometer: X = 16058 Y = -400 Z = 204
Temperature in celsius = 29.42 fahrenheit = 84.96
Gyroscope: X = -98 Y = 354 Z = 224
Angle: Pitch = 88.83 Roll = -0.54
Accelerometer: X = 15978 Y = -460 Z = 320
Temperature in celsius = 29.33 fahrenheit = 84.79
Gyroscope: X = -124 Y = 376 Z = 207
Angle: Pitch = 89.21 Roll = -0.31
Accelerometer: X = 15978 Y = -392 Z = 228
Temperature in celsius = 29.42 fahrenheit = 84.96
Gyroscope: X = -121 Y = 364 Z = 189
Angle: Pitch = 89.00 Roll = -0.56
Accelerometer: X = 15890 Y = -464 Z = 260
Temperature in celsius = 29.38 fahrenheit = 84.88
Gyroscope: X = -111 Y = 361 Z = 221
Angle: Pitch = 88.67 Roll = -0.65
Accelerometer: X = 16018 Y = -492 Z = 360
Temperature in celsius = 29.38 fahrenheit = 84.88
Gyroscope: X = -130 Y = 340 Z = 216
Angle: Pitch = 88.53 Roll = -0.43
Accelerometer: X = 16110 Y = -428 Z = 432
Temperature in celsius = 29.42 fahrenheit = 84.96
Gyroscope: X = -92 Y = 380 Z = 217
Angle: Pitch = 88.85 Roll = -0.60
Accelerometer: X = 15930 Y = -476 Z = 304
Temperature in celsius = 29.47 fahrenheit = 85.05
Gyroscope: X = -102 Y = 374 Z = 219
Angle: Pitch = 88.87 Roll = -0.24
Accelerometer: X = 16222 Y = -372 Z = 344
Temperature in celsius = 29.52 fahrenheit = 85.13
Gyroscope: X = -96 Y = 351 Z = 226
Angle: Pitch = 89.05 Roll = -0.26
Accelerometer: X = 15970 Y = -376 Z = 284
Temperature in celsius = 29.38 fahrenheit = 84.88
Gyroscope: X = -105 Y = 367 Z = 201
Angle: Pitch = 89.13 Roll = -0.62
Accelerometer: X = 16034 Y = -484 Z = 200
Temperature in celsius = 29.52 fahrenheit = 85.13
Gyroscope: X = -110 Y = 391 Z = 207
Angle: Pitch = 88.98 Roll = -0.51
Accelerometer: X = 16178 Y = -452 Z = 280
Temperature in celsius = 29.47 fahrenheit = 85.05
Gyroscope: X = -117 Y = 379 Z = 221
Angle: Pitch = 89.27 Roll = -0.43
Accelerometer: X = 16066 Y = -428 Z = 192
Temperature in celsius = 29.42 fahrenheit = 84.96
Gyroscope: X = -101 Y = 359 Z = 208
Angle: Pitch = 89.31 Roll = -0.19
Accelerometer: X = 16150 Y = -356 Z = 212
Temperature in celsius = 29.52 fahrenheit = 85.13
Gyroscope: X = -115 Y = 361 Z = 189
Angle: Pitch = 88.76 Roll = -0.51
Accelerometer: X = 16026 Y = -452 Z = 348
Temperature in celsius = 29.42 fahrenheit = 84.96
Gyroscope: X = -139 Y = 368 Z = 192
Angle: Pitch = 88.57 Roll = -0.69
Accelerometer: X = 16086 Y = -504 Z = 388
Temperature in celsius = 29.33 fahrenheit = 84.79
Gyroscope: X = -118 Y = 352 Z = 214</p>

Step 5: Understanding Tilt Angle

Accelerometer

The earth’s gravity is a constant acceleration where the force is always pointing down to the centre of the Earth.

When the accelerometer is parallel with the gravity, the measured acceleration will be 1G, when the accelerometer is perpendicular with the gravity, it will measure 0G.

Tilt angle can be calculated from the measured acceleration by using this equation:

θ = sin-1 (Measured Acceleration / Gravity Acceleration)

Gyro
Gyro (a.k.a. rate sensor) is used to measure the angular velocity (ω).

In order to get the tilt angle of a robot, we need to integrate the data from the gyro as shown in the equation below:

ω = dθ / dt ,

θ = ∫ ω dt

Gyro and Accelerometer Sensor Fusion
After studying the characteristics of both gyro and accelerometer, we know that they have their own strengths and weakness. The calculated tilt angle from the accelerometer data has slow response time, while the integrated tilt angle from the gyro data is subjected to drift over a period of time. In other words, we can say that the accelerometer data is useful for long term while the gyro data is useful for short term.

Link for better understanding: Click Here

Be the First to Share

    Recommendations

    • Finish It Already Speed Challenge

      Finish It Already Speed Challenge
    • Arduino Contest 2020

      Arduino Contest 2020
    • First Time Author Contest

      First Time Author Contest

    Discussions