Introduction: Gyro Stabilizer W/ Arduino and Servo
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
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)
GND -> GND
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.
BROWN -> GND
ORANGE -> 5V Rail
YELLOW -> D10
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.
BROWN -> GND
ORANGE -> 5V Rail
YELLOW -> D11
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.
Participated in the
Arduino All The Things! Contest