Arduino with MPU6050

Objective:

Connect MPU6050(6 axis Accelerometer and Gyroscope) with Arduino, download and setup necessary libraries in Arduino IDE, get data from MPU6050 and control 6 LEDs connected to digital pins of Arduino according to pitch,roll and yaw values from MPU6050.

Working of MPU6050

MPU6050 is a 6-axis Micro Electro Mechanical Sensor(MEMS) that has Accelerometer and Gyroscope in a single chip, It uses I2C protocol for communication and at any time gives out 6 values, three values from the accelerometer and three from the gyroscope .

Resources needed

SNo Component Quantity
1 Arduino uno 1
2 MPU6050 1
3 LED 6
4 Jumper wire 19
5 Breadboard 1

Libraries needed

SNo Library Name & link Description
1 MPU6050 Library for MPU6050 that does Digital motion processing and gives us accelerometer and gyroscope data
2 I2Cdev To establish I2C coomuncation with any I2C compatible electronic device

Setting up the libraries

Once we download the libraries from the above links, launch Arduino IDE, go to Sketch ->Include library ->Add.Zip Library and select the downloaded file and press open. Do the same for other library. Now go to File -> Examples -> MPU6050 -> MPU6050_DMP6, we will end up opening example code from the library, MPU6050_DMP6. Make modifications to the code as highlighted below.

Fritzing:

cod screen shot

Code

Snap shot of my working code

cod screen shot cod screen shot cod screen shot cod screen shot

Code Explanation

Load the code MPU6050_DMP6 as what it is and open the serial port. Send any character via serial terminal. Yaw, pitch, and roll values will start streaming down out serial terminal. Wait for it to stabilize for the current position of our MPU6050 and choose the upper and lower limit values of your choice for Yaw, pitch and roll and use simple if and else statements to turn ON and OFF the LEDs when YPR values go out of range.

My pin setup code goes inside void setup() function and my if and else statements go inside void loop() function.

Here is the link to the video that shows the working of the above project.