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




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

Hi, this page is about robotics, electronics, embedded systems, drones, RC and other cool DIY stu...

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:

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

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!

14 People Made This Project!


  • Metalworking Contest

    Metalworking Contest
  • Fix It! Contest

    Fix It! Contest
  • Creative Misuse Contest

    Creative Misuse Contest

122 Discussions

it works fine, but i also get some garbage value, kindly help me out.


where is the code i cant find it?

port = new Serial(this, portName, 115200);


Works just as well on Mac with a little tweak. First of all, do everything as you would on your Mac. Install Arduino libraries through Sketch -> Include Library -> Add .ZIP Library. Download and install Processing 3. Download and unzip ToxicLibs. You should see a toxiclibs-complete-0020 folder. Go to your Applications folder, right click on Processing and select Show Package Contents. Now navigate to Contents -> Java -> modes -> java -> libraries. This is where you should drag 'n drop the toxiclibs-complete-0020 folder.

Good. Now fire up Processing and follow


Click Run. Most probably you will get an error in line 77. To solve this you must replace the portName parameter with the actual portname your Mac assigns to your Arduino board. There are 2 ways to get that. The easiest would be to fire up the Arduino IDE and read the bottom right corner, it shows the assigned portname for the connected board. In my case that would be "dev/cu.wchusbserial1a160". Now go to line 77 on Processing MPUTeapot code and in my case, replace portName with "/dev/cu.wchusbserial1a160"So line 77 now becomes:

port = new Serial(this, "/dev/cu.wchusbserial1a160", 115200);

Please pay attention to both the leading slash / and the double " "

Now press Play. If everything is correct, you should see the little plane, following your gyroscope movements.

The second way to acquire that portname would be to run the following sketch within Processing:

// Example by Tom Igoe

import processing.serial.*;

// The serial port
Serial myPort;

// List all the available serial ports

hi itried the same on mpu6050
i didnot changed anything in jeff's code but i got error which is something like

Initializing I2C devices...
Testing device connections...
MPU6050 connection failed
Send any character to begin DMP programming and demo:
Initializing DMP...
DMP Initialization failed (code 1)

please help me out of this

I tried this tutorial in a Wemos D1 and I got this error in several parts of the code (In Arduino it works fine):

C:\Program Files (x86)\Arduino\libraries\I2Cdev\I2Cdev.cpp:276:75: error: no matching function for call to 'min(uint8_t&, int)'

for (uint8_t k = 0; k < length; k += min(length, BUFFER_LENGTH))

Any idea on how to solve this?

can i Switch this Code to an spi tft Display ? or is the work flow with nother sensors to much for and anruino mega Control board ?

Thanks a lot . I made it successfully. I was wondering if we could display 3D model on 3.2 inch TFT LCD using Arduino which has exactly same output as that of in processing IDE.

1 reply

You might be able to, I've never tried it though so I won't be much help. I do know there is a 3.2 inch TFT LCD shield for Arduino Mega, so you can try that but be careful to change the MPU6050 connections accordingly (to SDA and SCL pins) as this instructable was focused on the Uno and not the Mega. Check these links for the Uno and Mega pin mappings:

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.

5 replies

Hi Julian, stationary calibration is important because it is the only way for the system to know in what orientation, position and speed it is at any given point later. If you run the initialization phase once and store the offsets for later and skip calibration on each start up, you need to guarantee your setup always begins in that exact same state of motion and orientation for the IMU motion data to be at least approximately accurate.

It might be possible to the skip calibration part in the code, have you taken a look at the code (i.e. both the included arduino sketch and the MPU6050 library header files)? Can you tell me what part of the code relates to calibration? Then I can help you out.

It is possible to setup the code and the offset for the DMP in this way, that after the controller is start up, the DMP generates valid sensor values?

Now its the following behaviour: After controller starting the DMP generates unstable values, just after ca. 10 sec. the values becoming stable.

I can't help you until you answer my question: "have you taken a look at the code (i.e. both the included arduino sketch and the MPU6050 library header files)? Can you tell me what part of the code relates to calibration?" Then I can help you out. Please stop repeating your question, I already understand what you are asking.

Sorry for disturbing.
I already had a look in the necessary MPU6050 *.cpp and *.h files.
The ino sketch receives the actual sensor values from this functions:
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

Hi Julian, I think what you are looking for could be the line:

delay(30); // wait after reset

in the function MPU6050::dmpInitialize() in one of the following files: MPU6050_6Axis_MotionApps20.h and MPU6050_6Axis_MotionApps41.h, which file you have to edit depends on which one is #included in your MPU6050_DMP6.ino file. Maybe you can set the delay to something smaller or even 0 and see what happens. I'm only speculating from looking at the code, I haven't tested any of this, so I'll leave that up to you.

Hi, i followed your tutorial but my mpu6050 is not connecting successfully. I have this result:

IniInitializing I2C devices...

Testing device connections...

MPU6050 connection failed

Send any character to begin DMP programming and demo:

Does anyone here knows how to solve this?

2 replies

i have the same problem,anyone plz recommend the solution

Hey guys, this question has already been asked a couple of times before. Please scroll through the comment section to find the possible solutions (there may be a number causes for your problem, so I gave a number of possible solutions).

thanks a lot

the tutorial is very helpful

Thanks for this nice tutorial! It's great to get started with the MPU6050 module and it's fun to see how precise this sensor works.