Introduction: MPU6050: Arduino 6 Axis Accelerometer + Gyro - GY 521 Test & 3D Simulation

Are you looking to make your own quadcopter drone or RC plane with Arduino but don't know how to get the gyro working? Perhaps you already bought some 6 axis MPU6050 modules (on the GY-521 breakout boards) only to find out they weren't as simple to use as you thought? Try this out!

You'll learn to wire a simple circuit to test your MPU6050 with an Arduino and simulate the YAW, PITCH and ROLL on a 3D model plane on the screen. This is intended as a learning tool to get you familiar with gyro modules, breakout boards and installing the necessary libraries to your Arduino IDE to allow you to make the best use of your MEMS gyro and save time instead of writing complex code from scratch.

What you need can be found on eBay (links below):

Step 1: Wire Circuit As Shown Below:

***NOTE: all red cables are VCC (+5V) and black cables are GND, check carefully when wiring up your circuit. The breakout board comes with pins but requires soldering.***

For your own learning:

  • the gyro module communicates with the Arduino through I2C serial communication via the serial clock (SCL) and data (SDA)
  • the MPU6050 chip needs 3.3V but a voltage regulator on the GY-521 board allows you to give it up to 5V
  • For more information on the module there is a great resource on this page in the Arduino Playground

Step 2: Install I2Cdev & MPU6050 Libraries

If we were to write the code from scratch, it would take ages and there would be a lot of reverse engineering required to make good use of the module's proprietary Digital Motion Processing (DMP) engine because Invensense intentionally released minimal data on its MPU6050. Good thing someone has already done the hard work for us; Jeff Rowberg wrote some Arduino libraries to obtain the accelerometer / gyro data and handle all the calculations. They are available as a zip file from here:

https://github.com/jrowberg/i2cdevlib/zipball/master

Once unzipped, find the Arduino folder within it and copy the two folders "I2Cdev" and "MPU6050" over to your Arduino "libraries" folder in the following directory:

C:\Program Files (x86)\Arduino\libraries

Then open the Arduino IDE and in the examples section, you should find MPU6050_DMP6 within MPU6050. Open it, plug your arduino in, select the appropriate COM Port and upload the sketch. In the Serial Window, select a baud rate of 115200. You should be prompted that the MPU6050 connection was successful. You can test the data collection by typing anything in the text bar and pressing enter, the data should start showing up.

Now we want to set the code to run the teapot demo to show the 3D simulation. Close the serial window, then find and comment out the line #define OUTPUT_READABLE_YAWPITCHROLL and uncomment the line //#define OUTPUT_TEAPOT. Select "save as" and choose where you want to save the modified code. Upload again but don't open the serial window this time.

Step 3: Download & Install Latest Version of Processing & ToxicLibs Library

To run a 3D simulation of the yaw / pitch / roll values on an airplane on the screen, we'll be running the teapot demo from the MPU6050_DMP6 example from Jeff Rowberg's MPU6050 libary. However the Arduino IDE will only be acquiring the data, to display the 3D simulation we'll need additional software: Processing. Download Processing from here, then unzip to wherever you like:

https://processing.org/download/?processing

We'll need one final library to get things running: ToxicLib. This library will be going into Processing's libraries folder instead of Arduino's. The latest version of the ToxicLibs library is here:

https://bitbucket.org/postspectacular/toxiclibs/downloads/

The "libraries" folder of Processing can be found by following (starting from within the processing folder): modes -> java -> libraries. Unzip ToxicLibs and place ALL the contents there.

Step 4: Run the Simulation

Last of all, open the Processing application file and then

File -> Open -> follow this directory C:\Program Files (x86)\Arduino\libraries\MPU6050\Examples\MPU6050_DMP6\Processing\MPUTeapot

and open the MPUTeapot file.

Click the play button and the system should calibrate for about 20-30 seconds, leave the gyro stationary during that period.

Now pick up the gyro and test out the yaw / pitch / roll. Once you're happy that it all works properly you can begin experimenting with it for your own projects. If you want to make full use of the I2Cdev or MPU6050 libraries and their functions, consult their header files.

I hope to do an instructable on making a DIY drone shortly, so if you found this one useful, why not stay tuned for future projects by following me on Instructables and on Facebook / Twitter / Google+.

I am a participant in the Amazon Services LLC Associates Program (and other potential affiliate networks), an affiliate advertising program designed to provide a means to earn fees by linking to Amazon.com and affiliated sites. While I may be compensated for sales derived through this website, my recommendations of these products are sincere and based on the first-hand experiences and reviews of real users I have uncovered through online research. If you enjoyed this instructable and would like to see more like this, please consider supporting me by purchasing your materials for this project through the provided links in the materials section.

Your support is greatly appreciated! Happy DIY'ing!

Comments

author
mario.julio (author)2017-08-08

Thanks for sharing!!! My video:

author
OumeymaB made it! (author)2017-04-22

Thanksssss

18073341_1939822789584115_1323609621_n.jpg
author
JessicaN21 made it! (author)2017-04-12

Success! The only tricky part was putting the folders in the right spot. It was way easier to just install ToxicLibs from the processing software!

20170412_202616.jpg
author
Waldo31 (author)2017-01-14

Really good tuto, but you missed to tell something very very important, at least for windows users on the last step (I had a white screen instead of the plane):

You have to comment the line: "String portName = Serial.list()[0];" (by adding two "/"), and uncomment the line: "//String portName = "COM4";" and change the "COM4" by the COM port on wich your arduino is connected (you can see it on the arduino IDE --> Tools --> serial ports). And It works perfectly then.

However, I have a question: Do you have a simple solution to change the plane in something else, something of my choice ? Thank you.

author
DilpreetK3 (author)Waldo312017-04-12

Thanks for this edit. My device was recognizing the earlier configuration.

author
Waldo31 (author)Waldo312017-01-18

I found a solution to change the plane in another (simple) 3 object. But first, I have to thanks Jeff Rowberg for his help on it (I send him a mail, this is the guy who created the MPUTeapot).

So, for doing that, you only need to change the lines 112-139 (the lines which are coding the plane), using the 3D programmation for processing (here is the tutorial I used: https://processing.org/tutorials/p3d/ ). So with this, you can do simples models likes boxes, cylinders, triangles and quadrilaterals, and It will be enough for the majority of us :) Hope It helped someone !

author
HobbyTransform (author)Waldo312017-02-01

Cool, it sounds easier than I thought. I might try it out some time.

author
Waldo31 (author)HobbyTransform2017-02-01

Yes you can, that's very interesting !

author
AineI (author)2016-10-20

how to make the yaw pitch roll data to show at serial immediately? Without having to send character to start. Because I will be sending the data through Xbee

author
Costynv (author)AineI2017-04-11

Just delete/comment these 4 lines from the MPU6050 DMP sketch:

Serial.println(F("\nSend any character to begin DMP programming and demo: "));

while (Serial.available() && Serial.read()); // empty buffer

while (!Serial.available()); // wait for data

while (Serial.available() && Serial.read()); // empty buffer again

author
HobbyTransform (author)AineI2016-10-29

Hi Ainel,

Apologies for the late reply, I was trying to find a solution to your answer online but I haven't found anything. If you're still looking for a solution you can either experiment with the code to see if there is a way to start printing data automatically, or if there isn't a way and that's just how the arduino serial window works, maybe you could experiment with other serial windows like PuTTY or Tera Term. Hopefully you find something that works, but if not it shouldn't matter too much because the serial window is mainly for debugging your project so when you send the data through an XBee there probably won't be a need for Serial Begin / Write / Read in your code.

author
gm13619 (author)2017-03-03

Thank you for uploading this code, it has been really helpful. It worked great for one sensor but I tried adding a second sensor by making the AD0 pin on the second sensor high but when I ran it, it said "FIFO overflow!" every 7th line. Do you have any ideas for how to fix this?

author
Costynv (author)gm136192017-04-11

FIFO overflows typically happen when your other code (doing other tasks) takes too much time and the FIFO buffer on the MPU DMP gets full. Do you have any delay()'s in your code, for example?

author
HobbyTransform (author)gm136192017-03-08

Hi, it's good to see you're experimenting beyond this instructable to see what else is possible with this module. My guess is: see if you can find in the code what is being done with the AD0 pin of the first sensor and try to replicate that behavior independently with the AD0 pin of the second sensor via a separate arduino pin and see what you get.

author
Costynv made it! (author)2017-04-11

Thanks, informative and easy.

IMG_1082.JPG
author
_Thats_Mee_ made it! (author)2017-03-20

Hey there,
this is an amazing tutorial to work on thank you very much for that..
but i am getting this issue...
when i work with arduino uno everything works perfectly but when i switch from uno to mega it says that "MPU CONNECTION FAILED"
Even I2C scanner is not able to find any I2C Device.
whole configuration is as described in tutorial.

MPU c

Screenshot (1).png
author

Hi, the reason for the error I would assume is because the pin mapping for the micro-controllers are different (e.g. the SCL and SDA i2c pins are located in different places on the atmega328/160 on the UNO vs the atmega2560 on the arduino Mega). Please refer to these links as a starting point for making your corrections:

https://www.arduino.cc/en/Hacking/PinMapping168

https://www.arduino.cc/en/Hacking/PinMapping2560

If you need more information to fix it, you may have to refer to the atmega datasheets on Atmel's website.

author
saitejae (author)2017-03-02

Can someone tell me how to send the values of mpu6050 which is connected to arduino uno to arduino mega using rf transmitter and receiver module?

author
HobbyTransform (author)saitejae2017-03-08

Hi, this depends on what type of rf transmitter you want to use. There are many options such as 433MHz or 315MHz modules, bluetooth (e.g. HC-05 or HC-06), WiFi (e.g. ESP8266), XBee or even Zigbee. The simplest to use are probably the bluetooth modules in my opinion.

author
saitejae (author)HobbyTransform2017-03-08

okay i'll switch to bluetooth in that case. i have two hc05 modules one is master and the other one is slave. but I failed at sending the sensor data. sir, can u help me in coding

author
HobbyTransform (author)saitejae2017-03-08

For bluetooth you have to send / receive the data through serial. Here is a great starting point to help you with the arduino code for HC-05 modules. I learnt a lot through that instructable.

author
Lavanyabjb (author)2017-03-02

I am always getting -1 for raw values for all instance of time? Can you help me..??

Thankyou

author

I'll need more information about your setup than that to be able to help you, there is probably a host of reasons why you'd get a random error like that. I can only give you general advice at this point: double check your wiring, make sure you have the latest version of arduino, try an earlier version of the library if the current one isn't working, if all else fails disconnect everything and repeat the instructable from scratch to spot any possible mistakes you've made in setting up the system.

author
josef.cita (author)2017-02-22

Hey guys, can you help? I am using MPU6050 (only the gyro) and I need to map the values, but I don't know how...

I control a robotic arm with it and the gyro gives me (-180, 180) values, but I need to get (0, 360)

Could you tell me how to do that?

Thanks for your advices :)

author

Try the arduino map function: https://www.arduino.cc/en/Reference/Map

author

But isn't this one only for analog? Or what would the program look like for a gyro?

author

The map function is simply a math function to linearly map from one range of values to another, it can be used in many different contexts, analog was just one specific example. So if you have a gyro value, say gy, mapping from (-180,180) to (0,360) would look like: gy (or some other variable like gy_new) = map(gy, -180, 180, 0, 360).

author

One more question... how do I set the gyro to some specific values? Like, I start the program and the gyro is in xyz (90, 90, 90)? How do I do this?

author

And then just Serial.print (gy_new), right?

author
zaraki11 made it! (author)2017-02-17

Hey, can someone please help me !!!! So i've uploaded a picture of the "serial monitor" and the output that its showing. Its saying that there is a connection failure. I have checked the code several times to verify if i have initialized all the variables and i have also checked my connections but there is still an error.

a.jpeg
author
HobbyTransform (author)zaraki112017-02-22

Maybe one of the pins hasn't been soldered on properly? Double check that enough solder was used to make a clean connection. Or maybe the particular usb port used with the arduino was faulty? You could try another usb port on your computer. If those aren't the issue, there might also be a solution on the ic2dev forum (the link is somewhere in the comment section).

author
gabriel.lim_2015 (author)2016-10-03

my teapot keeps sinking, why?:(:(

author

What do you mean?

author

here is a video thanks:)

author

hOW TO PROGRAM IT PLZ SND ME ITS PROGRAM AND ALSO SND LIBRARY.......

author

Hi Gabriel, based on the motion response in the simulation in your video I'd say it's working fine but with a weird offset in the pitch angle for some reason, perhaps there was a slight misalignment when the chip was being manufactured which is causing the offset. I think you should be able to compensate for that with a simple fix: find the line of code (in the arduino sketch) that says

// supply your own gyro offsets here, scaled for min sensitivity

And adjust the appropriate X, Y, Z Gyro offsets accordingly. It might take some trial and error. Let me know if that works.

author

Hey Mr Hobby, i tried to adjust the x y z gyro offsets but the same problem persists, it can sense the movement when i am doing yaw pitch and roll but each action will cause the plane to sink down, i think the problem is with the chip, what should i do? thanks:)

author

Hey Gabriel, at this point I think your chip might be broken and you may need to get a new one. But just in case it's not, you could try asking for a solution on this forum: http://www.i2cdevlib.com/forums/. If that fails, then just get a new chip.

author

thanks bro

author

No worries, if you happen to find a solution please post here for the benefit of anyone else who might be experiencing the same problem. Thanks!

author
IndraL8 (author)2017-02-07

Hi..

can you help me to fix some eror when i upload the sketch..??

where i can find the em_cmu.h library..??

author
HobbyTransform (author)IndraL82017-02-09

Hi,
Can you please copy and paste the entire error message here? I'm not sure this file is even relevant because I haven't found it on my computer or in the library folder MPU6050.

author
julianpe (author)2017-01-13

Hello everyone,


I found this thread because I use the MPU 6050 and a Arduino Nano.
Therefore I use Jeff Rowbergs I2Ccdevlib for handling data of the MPU6050.

Currently I'm using this sketch:
https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/examples/MPU6050_DMP6/MPU6050_DMP6.ino

In the first 20 seconds after startig, the sensor values are floating a little bit. After that time the sensor values are nearly constant. I want to show the values on a serial monitor only if they are constant (after the 20 seconds). Which program codes do I have to keep in the void loop function?

I want this behaviour [PSEUDOCODE]

WHILE (SensorValuesCalibrating)
{
//DO NOTHING
}

// NOW SENSOR VALUES ARE CONSTANT
// DO OTHER STUFF

author
HobbyTransform (author)julianpe2017-02-01

Hi Julian,

I agree with Waldo's idea, that's how I would start:

1. Go to the #ifdef OUTPUT_READABLE_YAWPITCHROLL section of the void loop() function

2. save the most recent values to temp variables, y_old, p_old, r_old...

3. get the next values and save them: y_new, p_new, r_new...

4. use an if statement rather than a while statement: if(y_new - y_old > y_tol || p_new - p_old > p_tol || ...){

PRINT VALUES

DO OTHER STUFF

}

5. replace the old values with the new values

I leave it to you to decide how to handle the very first values recorded.

author
Waldo31 (author)julianpe2017-01-17

I would put something in the void setup, like "while the difference between 2 values is too big, don't continue". But I'm not sure at all.

author
Athanassios76 (author)2017-01-31

Excellent tutorial!

Followed instruction and comments (thanks Waldo31!) and it
works perfectly.

One question though, in a project I’m working on it would be
helpful if I could see the values of yaw pitch roll and acceleration on the
Serial monitor (amongst other values from a GPS sensor and a BMP180 sensor) or
save then in a file in the computer (for processing them later) and at the same
time use Teapot to visualize the orientation of the MPU6050. To be more precise,
data will be transmitted from an Arduino on a moving vehicle (sensors are
connected to this Arduino) to a receiver connected to a stationary Arduino to
be printed on Serial Monitor and/or stored in a file.

One thing it came to mind is to have 2 Arduino (with
receivers) an one of them will print all data on Serial Monitor or store them
in a file while the other will be sending only yaw pitch roll data to the
serial port.

Any thoughts or suggestions will be much appreciated.

Thanks again for the informative tutorial.

author

Hi, glad you found it useful.
To print yaw pitch roll values, you'll need to add Serial.println(...) lines to the code. For saving files through arduino, I haven't done it before but it's probably a similar process as done in the C language, i.e. using fopen and fclose functions. Check here for an example: http://www.tutorialspoint.com/c_standard_library/c_function_fopen.htm. Google is your friend, that's where I would start.

author
tabrizu91 (author)2017-01-21

hello,

thank you for your good tutorial.

i did it step by step but when i run the teapot ,the palne has no movment.what do you think about this problem.

author
Waldo31 (author)tabrizu912017-01-22

I think the problem come from a bad connection. Did you try with the MPU6050_DMP6 programm, and did this show you data in the serial windows ?

author
rfmdelgado made it! (author)2017-01-10

Excelent tutorial...

20170110_231922.jpg20170110_231944.jpg20170110_231908.jpg

About This Instructable

102,750views

119favorites

License:

Bio: Hi, this page is about robotics, electronics, embedded systems, drones, RC and other cool DIY stuff. I aim to manufacture robots & electronics. To see the ... More »
More by HobbyTransform:Arduino Encoded and Modulated Laser and Infrared Serial CommunicationArduino OLED 128X64 IIC Serial Display: Printing Text and Moving ImagesMPU6050: Arduino 6 Axis Accelerometer + Gyro - GY 521 Test & 3D Simulation
Add instructable to: