This is how I managed to use a MPU6050, an Arduino Nano, and two Servos to perform basic planar stabilization, as would be found on camera gimbals and so on. This is a work in progress but the guide provide hardware connections and basic code to implement my initial prototype.

My List of Parts:

1 x 6 DOF MPU-6050 module (http://goo.gl/0uxWkR) from Banggood.com

1 x Arduino Nano

2 x 9g Servos (http://goo.gl/N81jsj) from Banggood.com

2 x 10k Resistors

1 x Breadboard

Jumper wires

USB Battery pack (for independent power) or use USB power from a PC

IMPORTANT: I pay Banggood.com only w/ PayPal. I read that you shouldn't use credit cards there.

Step 1: Step 1: Push Things in and Roll Tape.

1. It took me a long time to convince me that I need 10k Pull-Up Resistors on SCL and SDA pins off MPU-6050 module. But they are vital for the MPU to function properly. Before I added those resistors, my MPU would cause lock-up w/in a minute of power-up. Also pay attention to the #2 and #3 below for complete solution.

(Updated on 1/17/16)

2. Jeff Rowberg @ I2CDevlib.com points out ADO pin must be connected to GND (I2CDevlib.com post).

3. ArsenioDev also points out that baud rate for MPU should be kept at maximum 115200.

4. Thank you for the tips! The tips above completely solved my lock-ups!

5. I'm now running on a DIY Uno w/ ATmega32P-PU w/ clock and two caps and it's running equally great! ATmega by itself only takes about 3ma right now, aside from the MPU and Servo powers.

For wire connections, use the diagram or pictures as a reference. (Also explained in the code)

MPU -> Arduino

VCC -> 5V (this powers the rail and hence the whole system including the servos)


SCL -> A5

SDA -> A4

INT -> D2 (I have it but not used in current software. I'm polling, not interrupting in this set up)

IMPORTANT: In addition to the wires above, two 10k resistors:

SCL -> 10K resistor (BROWN-BLK-ORANGE-Whatever) -> 5V Rail

SDA -> 10K resistor -> 5V Rail

ADO -> GND (Per Jeff Rowberg @ I2CDevlib.com - See Above)

SERVO 1 -> Arduino

This is the first servo taped (I know it's totally a hack job) on the breadboard, and this is oriented so that it would rotate around the long (or longitudinal or roll) axis. My servos had Brown-Orange-Yellow wires.


ORANGE -> 5V Rail


SERVO 2 -> Arduino

This is the second servo that is mounted to the servo 1 which controls the pitch axis. Again, it's wrapped even worse than the first one so it's barely visible in the picture.


ORANGE -> 5V Rail


Step 2: Step 2: Check the Servo Orientation

For the code to work correctly w/o any adjustment, it would help to make sure the servos are oriented the same way as mine. I know the tapes make it terrible to discern. Sorry... If you tape up, or screw everything together and find out that something's turned around, it's no big deal. We'll fix it in the next step.

Step 3: Step 3: Send 1's and 0's to the Arduino Nano

Of course any Arduino should work, but I've been using these Nanos a lot for their affordable price.

Upload the code using the USB cable from your PC. If you have a long cable, you may just leave it alone and let it power the unit. I taped (I know. This is the last one.) a USB power bank underneath the breadboard so I can hold it and feel cool about my sticky mess.

BTW, I have been using the USB brick underneath a breadboard and use USB output to power an Arduino or even connect to a USB-Serial interface (Like an FTDI interface) to power the breadboard and it's been very handy.

It should fire up, and should start it's wake-up dance. It's designed to sweep on both axis to clear the area. If it hits the chassis or things like that, it's possible the mount is too restrictive or servo is mounted backwards.

If a servo seems mounted backwards, you can adjust the code to reverse it's command:

<p>Servo1.write(-mpuPitch + 90);<br>Servo2.write(mpuRoll + 90);</p>

If the command seems to be adding to a change and not correcting it, you may try to reverse the sign of the mpuPitch or mpuRoll. Also, +90 is there because I oriented my servos so that 'normal' horizontal position would be in the middle of the servo range. You may play w/ that value as well depending on your installation.

Step 4: Step 4: Help Me Out Please

I learned a tremendous amount already from everyone w/ their awesome comments on Reddit. (https://goo.gl/lI0ir3)

So please help me make it better and learn lots of other things. I put Kalman Filter and PID on the list of things to read up to improve the command logic. Thank you for reading my first instructable.

P.S. If it does lock up on you, a reset on arduino should get it going again. Sorry.

I made it. Thanks. I have faced some difficulties. But overcome all and finally made it. However need to improve the smooth operation of servo movements. Also need to replace 4 solenoids for 2 servos. Any idea guys?
<p>I haven't used solenoids but there are some cool additional logic you could add to the current setup, such as Kalman filter (https://github.com/TKJElectronics/KalmanFilter) and PID. I was suggested to try to add kalman filter but I haven't gotten that far. However, it could help w/ smoother operation of the movements. </p>
<p>I eventually want to do this with steppers but for today I'm trying to figure this out as practice. I used the same modules, identical wiring (I think) and the code verbatim, and this is the result. https://youtu.be/vVdlZcyRsDw Any suggestions? I did have some trouble installing the libraries and had to do figure all that out manually, so that's my 1st suspicions, but it compiles and uploads now... so I dunno.</p>
<p>This is an awesome tutorial. Question:<br>Which libraries are needed to run these?<br>I see the following:<br>Servo.h<br>I2Cdev.h<br>MPU6050_6Axis_MotionApps20.h<br>MPU6050.h (if relevant)</p><p>I know, though, that different people write similar, but not necessarily compatible libraries. Which are the ones, specifically, I should get?</p>
<p>Hello! First of all, great tutorial!</p><p>We are using the same code in our project as you do in yours. We would like to implement Kalman filter and PID in our code. Would that be possible? Could you give us any tips of how to do it? </p><p>Cheers</p>
<p>Are you looking to make a self-balancing robot by chance? Along the time I made this, I built a 2 wheel robot w/ PID. I have been away from arduino for a bit but if you google &quot;self balancing PID robot&quot; you should run into a few different examples to get you going. I don't have the code any more but I adapted one of those online codes and attached 3 potentiometers for individual PID inputs. That's as far as I got. I would have loved to play w/ Kalman filters but didn't make it that far. Hope that gets you going in the right direction. Oh, there are a few youtube videos I watched also.</p>
<p>MPU6050 connection failed</p>
<p>Hi there, great tutorial!</p><p>I seem to be having trouble with the initial setup. Keeps telling me FIFO overflow in the serial monitor.. What do?!</p>
<p>Sorry, I am not familiar w/ that particular error message. Can you show me a screen shot of it and your code?</p>
<p>My servos didn't moved (my servos are fine, indeed i see the dance)</p><p>in serial monitor it only said <strong>&quot;</strong><strong>Initializing I2C devices...&quot;</strong> the <strong>&quot;</strong><strong>Testing device connections...&quot;</strong> won't appear and so the next message :(</p><p>did i do something wrong ?(i followed <strong>exactly</strong> your instructable, except for the power source)</p><p>oo yeah i almost forget to mention, i used uno w/ usb power source. At this point i started to think i dont have enough power, is that true?</p><p>many thanks btw, i can use ur code as refence :)</p>
<p>I FOUND THE PROBLEM</p><p>so i got rid of the pull-up resistor and foila.</p><p>my question is, BUT WHY :/</p>
<p>Hello there,</p><p>Can you help me in that I encountered errors in software code?</p><p>Thank you from now.</p>
<p>So I'm not sure how you downloaded your code, but the files you have do not seem to have the correct content. It almost looks like the HTML file saved straight from a Github repository. If you look at a sample arduino code, such as this; https://www.arduino.cc/en/Tutorial/Button you will see that typical arduino/c code wouldn't have all those tags w/ brackets. </p>
<p>This could not figure out his life.</p>
<p>try to use notepad++, it's a good program for these thing.</p>
<p>At this github repo; https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/I2Cdev there are two files, .cpp and .h. Both of these files need to be in a separate folder inside the library folder of arduino program. For example, if I had my arduino files in /arduino then you need to make a folder called I2CDev at /arduino/library. Then places those two files in /arduino/library/I2CDev/ Restart the arduino and it should include those two files on compiling your code.</p>
<p>I'm using an Arduino Leonardo and I am having some issue regarding the servos. One axis doesn't move at all while the other axis is trying to turn the servo past its threshold and is breaking my servo. When I switch the same two servos it the same axis, not the servo, that doesn't work while the other axis breaks my other servo. I am also using an external 5v 1 amp power supply for the servos having the same ground as the Arduino.</p>
<p>Hmmm, would i be able to program an attiny to work with this instead of the arduino? that would really cut down on space required. is the attiny compatable with the code? I dont have much experience with the attiny, but i do know that it doesnt work with everything. </p>
Ok so I got some tips for those who had the same problem than me concerning the servos:<br><br>- You need a specific power supply for the servos, the one from the board isn't enough<br>I used a 9v supply and it worked well<br>The ground of the 9v supply has to be the same than the board<br><br>- For the supply of the MPU, you shoukd connect it on the 3v3<br>The MPU-6050 is a 3v3 device, some have integrated regulators and some don't<br>If you connect to 5v and it doesnt have a regulator you will damage it
<p>Thank you the extra info. I will add those on the next update.</p>
<p>Thank you: the extra 9v supply made a huge difference !</p>
<p>I have just built this and it works perfectly. Thank you so much.</p><p>Some things I had to do to get it working for me:</p><p>A. Run the MPU calibration sketch from here: </p><p> <a href="http://www.i2cdevlib.com/forums/topic/96-arduino-sketch-to-automatically-calculate-mpu6050-offsets/" rel="nofollow">http://www.i2cdevlib.com/forums/topic/96-arduino-s...</a></p><p>\\ I had to Disconnect the ADO line for this calibration sketch to work for me on a NANO. </p><p>B. Insert those offsets into the original sketch on the instructable.</p><p>C. Run the updated sketch and enjoy the stabilisation :)</p>
<p>Forgot to add those steps. Thanks for sharing. I have to mpu6050s and the values were very different for each.</p>
<p>help, It appears a mistake, it says &quot;fatal error: I2Cdev.h: No such file or directory&quot;</p><p>do I need to preinstall some files or so? </p>
<p>Try this http://www.i2cdevlib.com/usage for the missing file(s).</p>
<p>my servos do not rotate properly , what do you think is the problem?</p>
<p>Hi, I would try a basic servo setup first, and see if the servos are indeed working. Try something like https://www.arduino.cc/en/Tutorial/Sweep and see if the servos will do the sweeping movement first.</p>
hlw!! <br>i am facing some problem with gyroscope mpu6050 gy521 and servo mg996r!!<br>everything is ok!! the code is running....! there is no error but servo isnot moving!! i just dnt understand why servo not moving!!<br>i have used arduino uno and for the servo i used external power and i' ve connected all gnd together.....but still servo remains the same.....!!<br>could u help me out of this????<br>is this because of the high torque servo?? or the resistor which i didnt add yet???
<p>Hi, I would try a basic servo setup first, and see if the servos are indeed working. Try something like https://www.arduino.cc/en/Tutorial/Sweep and see if the servos will do the sweeping movement first.</p>
<p>Hello,</p><p>I'm a huge beginner with Arduino, and I have a few questions for you</p><p>- First of all, will this software work if I'm using only one servo ? (I'm trying to stabilize only one axis for my hardware)</p><p>- I followed all the steps from the beginning, the .ino file is stocked in a folder on my desktop. When I first tried to compile, it asked for several libraries, which I put in the same folder than the .ino file (as mentionned in the error message &quot;</p><p>C:\Users\Kathleen Legrand\Desktop\SERVO_w_GYRO_2\SERVO_w_GYRO_2.ino:48:20: fatal error: I2Cdev.h: No such file or directory&quot;</p><p>After putting all the libraries in the folder, when I compile the code I have a long list of error messages (see attached image)</p><p>Could you please help me with that ?</p><p>Thank you very much :)</p>
<p>Hi, you can certainly use one Servo, as long as you align the axis of the gyro correctly w/ the servo. Let's say we'll use X-axis of the gyro and use that to command the servo. Then we also have to make sure the value of the x-axis of the gyro is sent to the servo motor to counteract</p><p>Also the I2CDev library should be installed inside the library folder inside the Arduino program folder. Try what's shown here; <a href="http://www.i2cdevlib.com/usage" rel="nofollow">http://www.i2cdevlib.com/usage</a></p><p>w.</p>
<p>Thank you for your quick answer</p><p>I've managed to compile the code sucessfully, and it shows no error when sending it to the arduino chip</p><p>However, once transfered, it looks like nothing happens, the servo doesn't even rotate to calibrate, and when I'm moving the gyro it doesn't move either</p><p>As I said, I'm using only one servo, could it be the problem ?</p><p>I attach you a picture of my mounting</p><p>Thank you very much</p>
<p>Actually what you have is not a 'servo' in a traditional sense but probably a gimbal motor. To use those, you need a separate motor controller, like something like here; <a href="https://www.instructables.com/id/Brushless-Gimbal-with-Arduino/" rel="nofollow">https://www.instructables.com/id/Brushless-Gimbal-w...</a> </p><p>It's my intend to update my set up to use gimbal motor to stabilize a drone camera and such but have not had the time to yet. 8-( </p><p>Those motors actually work more like a hard drive controller and requires additional 'brain.' Traditional servo actually has this 'brain' in them already, which is why they are in a box w/ additional gears, like this;</p><p><a href="https://www.adafruit.com/product/155" rel="nofollow">https://www.adafruit.com/product/155</a></p><p>Sorry probably not what you were hoping for... But hope that clears things up for you.</p>
<p>Hello,</p><p>I've ordered servos in order to replace my gimbal</p><p>So now I have exactly the same mounting than described in your tutorial, with two 9g servos</p><p>However, I have a problem that apparently other people have : the servos are not moving</p><p>Do you have an idea of how I can manage to have it working ?</p><p>Thanks a lot</p>
<p>Hi, I would try a basic servo setup first, and see if the servos are indeed working. Try something like https://www.arduino.cc/en/Tutorial/Sweep and see if the servos will do the sweeping movement first.</p>
Oh I see, I didn't know there was such a difference, your link was very interesting thank you<br>I'll try with a real servo and keep you informed<br>I'll also try to learn and apply how to use a gimbal if possiblr, at least I know what to search now<br>Thanks :)
<p>Hi! Servo are working correctly few seconds. After that not working. This is my serial monitor's screenshot.</p>
<p>Could you verify that pull up resisters and in place, as well as ADO pin is connected to the ground? That solved all of my problems.</p>
<p>I removed the int line with no reset, take a try.</p>
<p>Thank you for the tip!</p>
<p>Hello, thank you so much for this information it is very helpful!</p><p>I have tried this with my arduino but my sensor keeps resetting my offsets after a few seconds of inactivity. Do you know how to prevent this from occurring? </p>
<p>Hi, a few things I would suggest to try,</p><p>1. If the MPU is resetting, I would make sure there's enough power. I power my arduino w/ a portable USB.</p><p>2. I would double check and make sure there are pull-up resistors on the I2C connections SCL and SDA. Let me know if any of these help you.</p>
<p>Thanks for your quick reply! I am powering through USB and I am using the resistors. I am not sure if the MPU is resetting exactly... if I keep the MPU moving, everything is fine. However, if it is still for about five seconds, my offsets behave as if they were all zero. When I reset the arduino, everything goes back to normal. I think the MPU is entering &quot;sleep mode&quot; but I can not find out how to disable it. Thanks again for your help.</p>
<p>Let's try a few more!</p><p>1. I would assume that you have tried all the troubleshooting steps above, particularly #2 and #3 in part 1. If that's the case, then I would make sure your MPU is calibrated. You can find a arduino code to run such as this <a href="http://wired.chillibasket.com/2015/01/calibrating-mpu6050/" rel="nofollow">http://wired.chillibasket.com/2015/01/calibrating-...</a> which will give you the values to initialize the mpu in your own program. This needs to be done for each MPU in each separate installation.</p><p>2. Also, and more likely, that my logic in my code is too simple for your application. My program does not employ any secondary filtering/logic beyond reading the values from the MPU. It works pretty well for me but it may not for you. If you know how to add PID or Kalman filtering, it would greatly reduce the erratic behavior. Or any other simple filtering logic would help you ignore weird values, such as running a moving average and ignore spiky values.</p><p>Hope that helps you.</p>
Psst, the freeze is a side effect of the library FIFO desync. Max the baudrate to 115200 and it won't reappear.
<p>and thats why This is our cool place to be, where we help eagother. Well done ArsenioDev. </p><p>And also woojay</p>
<p>Thanks for the tip! I will be updating it shortly.</p>
<p>I'm currently working on my own quad (starting from scratch) and this looks like a nice way to tackle the stabilizing without the need to buy a new set of propellers each time :-)</p>
<p>Awesome stablizer.</p>
<p>Thank you!</p>

About This Instructable




More by woojay:Gyro Stabilizer w/ Arduino and Servo 
Add instructable to: