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:
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:
Your support is greatly appreciated! Happy DIY'ing!
14 People Made This Project!
We have a be nice policy.
Please be positive and constructive.
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
// The serial port
// List all the available serial ports
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 ?