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

Picture of 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:

Picture of 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

Picture of 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:

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:

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:

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

Picture of 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+.

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 affiliate links in the materials section or donating below:


Bitcoin: 1Mqe7et24Lz4DY1RUN4iAQVHkvJsdFArKR

Ethereum: 0x6d8248db1cdea6a783cb6b41ae67bb8e6144f479

Litecoin: LW6PWESqsr8xHw6EJ9WLbsQsAyTvPnwnxJ

Dash: Xemv7jud697v8tQmKfNFoMxfkd17ZayH2t

Your support is greatly appreciated! Happy DIY'ing!


AlyanS (author)2017-10-21

Can you edit the simulation so that it shows a RC car tilting and rolling? And can you run the simulation wirelessly onto a phone? I am wanting to put the sensor on an RC car and see the simulation in real time on my phone.

HobbyTransform (author)AlyanS2017-10-22

Hi Alyan, for editting the plane to show a car instead, please refer to Waldo's solution in the comments section further down.

For connecting wirelessly to a phone, I suppose bluetooth might be possible (e.g. HC-05 bluetooth module), I haven't tried that so I can't say how you would have to modify the code to do that but have a look at arduino bluetooth tutorials on instructables and maybe combine it with what you've learnt in this one?

AlyanS (author)HobbyTransform2017-10-22

How would I view the simulation on the smartphone if I can connect the devices via Bluetooth? And is it possible to use the accelerometer to measure the overall speed of the car? I suppose right now we only have a simulation of the car's tilt or direction. Big thanks for the help so far!

HobbyTransform (author)AlyanS2017-10-23

To view the simulation on an android smartphone, I would assume you'd need an android compatible arduino IDE (e.g. or the web editor ( and the processing app (, serial data would be received via bluetooth, i.e. transmitted from HC-05 and received by the phone's bluetooth. In theory this setup might work, I leave that up to you to test.

For the velocity of the car, you should be able to calculate it from acceleration using calculus (velocity is the definite integral of acceleration, initial velocity is 0 m/s, initial acceleration is 9.8m/s^2 down) or perhaps the library for the mpu6050 module has a function for calculating velocity, I leave that up to you to find.

bilgehan (author)2017-09-19

Hi, if MPU6050 has A/D converters, then why do we connect sda and scl to the "analog in" instead of "digital" on the arduino board?

HobbyTransform (author)bilgehan2017-09-22

That's a good question, what have you found out about it so far on google?

bilgehan (author)HobbyTransform2017-09-23

I've found out that a4 and a5 pins are allocated for scl and sda lines but still I have no idea why these pins are in the analog part :(

HobbyTransform (author)bilgehan2017-09-24

Oh, ok, now I understand your question. The reason is that arduino uno pins have multiple possible modes (refer to depending on which you choose when you write the code. For example digital pins 3,5,6,9,10,11 are also capable of PWM (which is a type of Digital to Analog converter). Also digital pins 0, 1 are capable of serial communication with pin 0 being Rx and pin 1 as Tx. In the same way, pins a4 and a5 can act as analog pins or they can act as i2c (SCL / SDA) depending on the mode you select when you write the code.

ljmadore (author)2017-09-10

Hi, Got it, I had to set the portName in the MPUTeapot code to COM3. Works well now.

ljmadore (author)2017-09-10

Hi, this is my first Arduino project & all was going well until the 3D Processing bit. I made the changes to '#define OUTPUT_READABLE_YAWPITCHROLL" & the "//#define OUTPUT_TEAPOT" re-loaded the Arduino code without the serial monitor. I started the Processing program * loaded the MPUTeapot & run. After about 20 seconds the Tx led on the UNO goes out & I never get motion on the plane graphic. Please help

ljmadore (author)2017-09-10

I get no motion on the 3D model

ljmadore (author)2017-09-10

Hi, this is my first Arduino project & all was going well until the 3D Processing bit. I made the changes to '#define OUTPUT_READABLE_YAWPITCHROLL" & the "//#define OUTPUT_TEAPOT" re-loaded the Arduino code without the serial monitor

VictorL88 (author)2017-09-09

Nice work. Pay attentions to the markings on the GY-521 board. It shows the orientation of the various axes. Thanks.

julianpe (author)2017-08-26

Is it possible to run the initialization phase once, and store all the offsets for further program starts?

I don't want to wait about 10 sec. for calubration each time I start up the controller.

dilisai made it! (author)2017-08-21

I made it. Very good tutorial

Thx ;)

vpsekharan made it! (author)2017-08-18

Nice project. I made it. It works very well. I waiting for a Arduino drone project in this section. Thank you very much.

mario.julio (author)2017-08-08

Thanks for sharing!!! My video:

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


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!

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.

DilpreetK3 (author)Waldo312017-04-12

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

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: ). 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 !

HobbyTransform (author)Waldo312017-02-01

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

Waldo31 (author)HobbyTransform2017-02-01

Yes you can, that's very interesting !

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

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() &&; // empty buffer

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

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

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.

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?

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?

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.

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

Thanks, informative and easy.

_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.


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:

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

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?

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.

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

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.

Lavanyabjb (author)2017-03-02

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


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.

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 :)

Try the arduino map function:

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

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).

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?

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

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.

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).

gabriel.lim_2015 (author)2016-10-03

my teapot keeps sinking, why?:(:(

What do you mean?

here is a video thanks:)

About This Instructable




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: