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+.
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:
PayPal: https://www.paypal.me/HobbyTransform
Bitcoin: 1Mqe7et24Lz4DY1RUN4iAQVHkvJsdFArKR
Ethereum: 0x6d8248db1cdea6a783cb6b41ae67bb8e6144f479
Litecoin: LW6PWESqsr8xHw6EJ9WLbsQsAyTvPnwnxJ
Dash: Xemv7jud697v8tQmKfNFoMxfkd17ZayH2t
Your support is greatly appreciated! Happy DIY'ing!

Participated in the
Arduino All The Things! Contest
131 Comments
Question 2 years ago
hi, how i can build 3 axis stabilizer gyro , for the thing with 1kg weight in 1 meter far from stabilizer ?
Question 3 years ago on Introduction
Hey, how i can use another 3D Model? I want to have a car instead of the plane. Is it possible with a .obj model?
Question 3 years ago
Hey , I get the whole time a fifo overflow. I copy your code entirely and connected like on your picture . Do you have any idea?
Thank you
Question 3 years ago
I have a problem with MPU6050 gyro sensors, I am making school project with Vehicle Accident GPS tracking. When the normal condition, eventhing is ok. Gyro is working his axis,but when over limit gyro's x,y,z values, LCD and Serial monitor is stop their process. how can i do that?
link:http://bigbelectronics.in/product.php?product=gy-521-mpu6050-module-3-accelerometer-arduino
Question 3 years ago
I am trying to interface the mpu 650 module to node mcu to calibrating the values for robot i am
trying to useed the display the calibrating values (X-Y axis) please provide the code for node mcu.
thankyou.
http://bigbelectronics.in/product.php?product=gy-521-mpu6050-module-3-accelerometer-arduino
Question 3 years ago on Step 1
Is this used on a quadcopter or any other UAV????
If so how would you install it on a flight controller (kk 2.1.5) for instance????
4 years ago
I would like to control an output based on a value of roll. Example would be turn on output 10 at 90 degrees and turn it off at 70 degrees.
I am using the code Jeff Rowberg wrote back in 2012 and looking at yaw, pitch and roll on the serial monitor.
Any help would be great!
Thanks, Doug
Question 4 years ago
when i put the toxi for the processing it was error open gl. "The field PConstants.OPENGL is deprecated". how to fix it.. and what it is mean?
4 years ago
It goes like
DMP ready! Waiting for first interrupt, but then nothing happens
4 years ago
it works fine, but i also get some garbage value, kindly help me out.
Question 5 years ago
where is the code i cant find it?
5 years ago
port = new Serial(this, portName, 115200);
WHAT WILL BE THE CORRECT SYNTAX TO WRITE IT IT IS SHOWING ERROR ABOUT COM PORT ...Error oppening serial port
Tip 5 years ago
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
/Documents/Arduino/libraries/MPU6050/examples/MPU6050_DMP6/Processing/MPUTeapot
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:
5 years ago
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
5 years ago
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?
Question 5 years ago on Introduction
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 ?
5 years ago
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.
Reply 5 years ago
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:
https://www.arduino.cc/en/Hacking/PinMapping168
https://www.arduino.cc/en/Hacking/PinMapping2560
5 years ago
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.
Reply 5 years ago
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.