loading

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.
Here's what you need:

  • 1 x Arduino UNO + USB cable
  • 1 x mini prototyping breadboard
  • 1 x GY-521 breakout board (few bucks on eBay)
  • Some male-to-male jumper cables
  • Soldering iron + solder

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:
https://www.facebook.com/HobbyTransform

https://twitter.com/HobbyTransform

Your support is greatly appreciated! Happy DIY'ing!

<p>Thanksssss</p>
<p>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!</p>
<p>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):</p><p>You have to comment the line: &quot;String portName = Serial.list()[0];&quot; (by adding two &quot;/&quot;), and uncomment the line: &quot;//String portName = &quot;COM4&quot;;&quot; and change the &quot;COM4&quot; by the COM port on wich your arduino is connected (you can see it on the arduino IDE --&gt; Tools --&gt; serial ports). And It works perfectly then.</p><p>However, I have a question: Do you have a simple solution to change the plane in something else, something of my choice ? Thank you.</p>
<p>Thanks for this edit. My device was recognizing the earlier configuration.</p>
<p>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).</p><p>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 !</p>
<p>Cool, it sounds easier than I thought. I might try it out some time.</p>
<p>Yes you can, that's very interesting !</p>
<p>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</p>
<p>Just delete/comment these 4 lines from the MPU6050 DMP sketch:</p><p> Serial.println(F(&quot;\nSend any character to begin DMP programming and demo: &quot;));</p><p> while (Serial.available() &amp;&amp; Serial.read()); // empty buffer</p><p> while (!Serial.available()); // wait for data</p><p> while (Serial.available() &amp;&amp; Serial.read()); // empty buffer again</p>
<p>Hi Ainel,</p><p>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.</p>
<p>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 &quot;FIFO overflow!&quot; every 7th line. Do you have any ideas for how to fix this?</p>
<p>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?</p>
<p>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.</p>
<p>Thanks, informative and easy.</p>
<p>Hey there,<br>this is an amazing tutorial to work on thank you very much for that..<br>but i am getting this issue...<br>when i work with arduino uno everything works perfectly but when i switch from uno to mega it says that &quot;MPU CONNECTION FAILED&quot;<br>Even I2C scanner is not able to find any I2C Device. <br>whole configuration is as described in tutorial.</p><p>MPU c</p>
<p>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:</p><p><a href="https://www.arduino.cc/en/Hacking/PinMapping168" rel="nofollow">https://www.arduino.cc/en/Hacking/PinMapping168</a></p><p><a href="https://www.arduino.cc/en/Hacking/PinMapping2560" rel="nofollow">https://www.arduino.cc/en/Hacking/PinMapping2560</a></p><p>If you need more information to fix it, you may have to refer to the atmega datasheets on Atmel's website.</p>
<p>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?</p>
<p>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.</p>
<p>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</p>
<p>For bluetooth you have to send / receive the data through serial. Here is a great <a href="https://www.instructables.com/id/Arduino-AND-Bluetooth-HC-05-Connecting-easily/?ALLSTEPS" rel="nofollow">starting point</a> to help you with the arduino code for HC-05 modules. I learnt a lot through that instructable.</p>
<p>I am always getting -1 for raw values for all instance of time? Can you help me..??</p><p>Thankyou</p>
<p>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.</p>
<p>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... </p><p>I control a robotic arm with it and the gyro gives me (-180, 180) values, but I need to get (0, 360)</p><p>Could you tell me how to do that?</p><p>Thanks for your advices :) </p>
<p>Try the arduino map function: <a href="https://www.arduino.cc/en/Reference/Map" rel="nofollow">https://www.arduino.cc/en/Reference/Map</a></p>
But isn't this one only for analog? Or what would the program look like for a gyro?
<p>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).</p>
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?
<p>Hey, can someone please help me !!!! So i've uploaded a picture of the &quot;serial monitor&quot; 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. </p>
<p>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).</p>
<p>my teapot keeps sinking, why?:(:(</p>
<p>What do you mean?</p>
<p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/71yew05jLPU\" width="500"></iframe></p><p>here is a video thanks:)</p>
<p>hOW TO PROGRAM IT PLZ SND ME ITS PROGRAM AND ALSO SND LIBRARY.......</p>
<p>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</p><p>// supply your own gyro offsets here, scaled for min sensitivity</p><p>And adjust the appropriate X, Y, Z Gyro offsets accordingly. It might take some trial and error. Let me know if that works.</p>
<p>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:)</p>
<p>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.</p>
<p>thanks bro</p>
<p>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!</p>
<p>Hi.. </p><p>can you help me to fix some eror when i upload the sketch..??</p><p>where i can find the em_cmu.h library..??</p>
Hi,<br>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.
<p>Hello everyone,</p><p><br>I found this thread because I use the MPU 6050 and a Arduino Nano.<br>Therefore I use Jeff Rowbergs I2Ccdevlib for handling data of the MPU6050.<br><br>Currently I'm using this sketch:<br>https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/examples/MPU6050_DMP6/MPU6050_DMP6.ino<br><br>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?<br><br>I want this behaviour [PSEUDOCODE]<br><br>WHILE (SensorValuesCalibrating)<br>{<br> //DO NOTHING<br>}<br><br>// NOW SENSOR VALUES ARE CONSTANT<br>// DO OTHER STUFF</p>
<p>Hi Julian,</p><p>I agree with Waldo's idea, that's how I would start:</p><p>1. Go to the #ifdef OUTPUT_READABLE_YAWPITCHROLL section of the void loop() function</p><p>2. save the most recent values to temp variables, y_old, p_old, r_old...</p><p>3. get the next values and save them: y_new, p_new, r_new...</p><p>4. use an if statement rather than a while statement: if(y_new - y_old &gt; y_tol || p_new - p_old &gt; p_tol || ...){</p><p>PRINT VALUES</p><p>DO OTHER STUFF</p><p>}</p><p>5. replace the old values with the new values</p><p>I leave it to you to decide how to handle the very first values recorded.</p>
<p>I would put something in the void setup, like &quot;while the difference between 2 values is too big, don't continue&quot;. But I'm not sure at all.</p>
<p>Excellent tutorial!</p><p>Followed instruction and comments (thanks Waldo31!) and it<br>works perfectly.</p><p>One question though, in a project I&rsquo;m working on it would be<br>helpful if I could see the values of yaw pitch roll and acceleration on the<br>Serial monitor (amongst other values from a GPS sensor and a BMP180 sensor) or<br>save then in a file in the computer (for processing them later) and at the same<br>time use Teapot to visualize the orientation of the MPU6050. To be more precise,<br>data will be transmitted from an Arduino on a moving vehicle (sensors are<br>connected to this Arduino) to a receiver connected to a stationary Arduino to<br>be printed on Serial Monitor and/or stored in a file.</p><p>One thing it came to mind is to have 2 Arduino (with<br>receivers) an one of them will print all data on Serial Monitor or store them<br>in a file while the other will be sending only yaw pitch roll data to the<br>serial port.</p><p>Any thoughts or suggestions will be much appreciated.</p><p>Thanks again for the informative tutorial.</p>
Hi, glad you found it useful.<br>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.
<p>hello,</p><p>thank you for your good tutorial.</p><p>i did it step by step but when i run the teapot ,the palne has no movment.what do you think about this problem.</p>
<p>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 ?</p>
<p>Excelent tutorial...</p>
<p>The link that this site posts about the ToxicLibs is not right.</p><p>Here is the right <a href="https://github.com/technicolorenvy/Processing-Libraries" rel="nofollow">link</a>.</p><p>Download it all and use the <a href="https://github.com/technicolorenvy/Processing-Libraries/tree/master/toxiclibs_p5" rel="nofollow">toxiclibs_p5</a> and the <a href="https://github.com/technicolorenvy/Processing-Libraries/tree/master/toxiclibscore" rel="nofollow">toxiclibscore</a> and extract them to the same path that the post says...</p><p>Good Luck :)</p>

About This Instructable

86,137views

105favorites

License:

More by HobbyTransform:Arduino OLED 128X64 IIC Serial Display: printing text and moving images MPU6050: Arduino 6 Axis Accelerometer + Gyro - GY 521 Test & 3D Simulation RC Multi Channel Arduino Proportional Transmitter / Receiver with Button Trimmers 
Add instructable to: