Saturday, July 12, 2014

MPU6050 (GY-521 Breakout) + Arduino Mega 2560 Accelerometer and Gyroscope Application

Hi All,

I’m going to show you today how we can use the GY-521 Breakout with the Arduino Mega 2560 to get the readings from the sensor.

First I have to say that, I am totally new to Arduino and I have a limited knowledge in working with the registers and all the deep stuff. I’m trying to learn them and yes, this will be a beginning. J

So first let me show you what I have already bought.
First GY-521 Breakout for MPU-6050

 

Let me provide you some brief introduction about this product.
The InvenSense MPU-6050 sensor contains a MEMS accelerometer and a MEMS gyro in a single chip. It is very accurate, as it contains 16-bits analog to digital conversion hardware for each channel. Therefor it captures the x, y, and z channel at the same time.
- Chip: MPU-6050
- Power supply: 3.5V (But as there is a voltage regulator on the breakout board, you can use 5V directly)
- Communication mode: standard IIC communication protocol
- Chip built-in 16bit AD converter, 16bit data output
- Gyroscopes range: +/- 250 500 1000 2000 degree/sec
- Acceleration range: +/- 2g, +/- 4g, +/- 8g, +/- 16g
MPU-6050 Datasheet can be found here. Register map can be found here
Here is the schematic of GY-521



Now we have to get the Arduino Mega 2560 Board.



All the information related to this product can be found here.
So we have our items and the next important thing is to hookup these two together. Note the wiring is different for Arduino UNO. This wiring is for Arduino Mega 2560.



Here the connections are
GY-521                                  Arduino Mega
VCC                                        3.3V or 5V
GND                                      GND
SCL                                         Pin 21 (SCL)
SDA                                        Pin 20 (SDA)
INT                                         Pin 2 (Interrupt 0) – for more interrupt pins in Arduino click here


Now for the program. As we have a lot of resources regarding the usage of Arduino, let’s try the code from them (Although I had to spend several hours to work with them :P ).

First let’s try to find whether our device is connected with the Arduino. For that we can use the I2C Scanner code by Krodal to find out the connected I2C devices to Arduino. You can find the code here
Also following is the code. Compile the code and upload it to Arduino. Open the serial monitor and reset the Arduino.



It will find your GY-521 at the address 0x68 or 0x69. If it doesn’t find the IMU at that location, there might be an issue with your wiring. Please check it again. 


Now we can use the Krodal’s next code to get the raw outputs from the sensor. Krodal’s sketch can be found here. Please scroll to the end of the page and you’ll be able to find the code. Also I’ll be posting it here.


Here's the output.


Now we are ready to try the code from Jeff Rowberg. Download the I2C library from github. Here’s the link.

Now unzip the file and find the Arduino folder. Copy the I2Cdev and MPU6050 libraries to your Arduino libraries located at C:\Program Files (x86)\Arduino\libraries.



Now you are ready to use the I2Cdev and MPU6050 libraries. Locate example code in \Arduino\MPU6050\Examples\MPU6050_DMP6 and upload it to the Arduino. Now you will get the output from the Serial monitor. The output provides you the sensible values from the sensor unlike the raw outputs. Here Jeff has used the onboard Digital Motion Processor™ (DMP™) capable of processing complex 9-axis MotionFusion algorithms. You can find Jeff’s work here.






Now we can use the Processing demo provided by Jeff with the MPU6050 example. You can download Processing hereYou also have to install the toxiclib for Processing. You can find that here.

To install the toxic library just copy the contents of the zip file to the library folder in Pocessing projects directory.

Also create a folder named MPUTeapot in the Processing projects folder and copy the MPUTeapot.pde file to that folder from MPU6050 example code location.

Now you have to do the following changes in the Jeff’s MPU6050_DMF code.
  1. Comment the line  : #define OUTPUT_READABLE_YAWPITCHROLL
  2. Uncomment the line : #define OUTPUT_TEAPOT
  3. Now load the updated MPU6050_DMP code to Arduino.
  4. Run the MPUTeapot.pde in Processing
  5. You’ll see the small plane moving
So hope this would help someone. 
Thank You.