Instructables

Serial Servo Controller w/Arduino - Control Up To 12 Servos At Once Using the Arduino and a USB Connection

Featured
I'm going to show you how to use your Arduino to control up to 12 servos at once with minimal jitter.  Using a simple serial interface you can control the position of up to 12 servo channels.  Up to 10 snapshot positions can be saved and played back at any time.  Start up values for each servo can be saved as well.

There are two major parts to the application.  The first part I will discuss is the firmware and hardware.  This includes the Arduino and another board we will use to supply power to the servos.  It's important to isolate the servo power supply from the microprocessor power in case the servos need more current than the battery can supply (imagine all 12 servos stalling at once).  If the power supplies are the same you could have a brownout condition on your microprocessor depending what type of power supply you are using.  I will also show you the firmware required to run this application on the Arduino.

In the second part I will discuss some simple software that will allow you to control the firmware through serial commands.  This software provides an interface to the Arduino firmware to control the servos, save servo start up positions, even record a series of positions, or frames, (of all channels saved at once) to playback in sequence when desired.  The software is .Net based so make sure you have the latest .Net framework installed.  I will be supplying an installer and source code.  

Here is a video demo of the project:
 
Remove these adsRemove these ads by Signing Up

Step 1: Parts List

IMG_20120103_150535.jpg
The following is a list of material and items I used for this instructable:

1x Arduino Board (Any variant that has a 328p processor)
1x BreadBoard
1x Battery 6.0-8.0Vdc (I used an 800maH LiPoly battery)
1x 1N4004 Diode
2x 220uF 35V Electrolytic Capacitors
4x 3 Pin Headers  (Use as many as you have servos for, I used 4 for this example but the firmware supports up to 12, just repeat)
4x 100 ohm resistors (1 for each servo.  These are used for current limiting.  At 3.3v the resistor limits the amount of current through the microprocessor at 3.3ma if a low impedance load is seen)
1x Small LED
1x 470 ohm resistor
1x Switch (I used a DIP switch for simplicity)
 4x Hobby Servos
Various Jumper Wires

Step 2: Servo Power Supply

Picture of Servo Power Supply
IMG_20120103_150535.jpg
SSCPowserSupply.Page0.PNG
The servo power supply is a simple circuit I created to provide a simple and safe way to power your servos.  The supply consists of a couple of capacitors to help supply extra current during high current demands on the battery, a diode to prevent reverse polarization, a convenient switch for power, and a small LED for a simple power indicator.

You will need the following in this step:
1x Switch
2x 220uF
35V Electrolytic Capacitors
1x Terminal Header Connector
1x 1N4004 General Rectifier
1x Small LED
1x 470 ohm Resistor
Various Jumpers / Wires


First connect the switch, diode and terminal for the battery.  
Next add the filter capacitors.
Finally add the power indicator led and current limiting resistor.

Next we will build the servo interface circuit.

Step 3: Servo / Microprocessor Interface

Picture of Servo / Microprocessor Interface
The servo / microprocessor interface is a simple interface designed to make it easy to hook up servos to the Arduino and apply the proper power and grounding circuitry for the servos.  In this example I show a bank of 4 servos.  It is easy enough to add more servos by duplicating this bank.  For example, duplicate it once for 8 servos or twice for 12 servos total.  

The interface to the Arduino is quite simple and consists of just a current limiting resistor.  The resistor really  isn't necessary but I like to play it safe and protect my Arduino whenever I can.  Adding the resistor will prevent your Arduino from sourcing too much current to the control wire of the servo.

You will need the following in this step:
4x 100 ohm Resistors
4x Three Pin Terminal Headers
Various Jumpers / Wires


First add the resistors to the breadboard.
Next add the three pin terminal headers
Finally connect everything to proper buses (Right most pin is ground, middle is servo power)

Depending on how permanent you want it you might want to apply a liberal coat of hot glue to all the headers on the board.  That will save you from having to pull the terminal headers from the connectors which they will inevitably stick to when you pull them off.

Next we upload the firmware.

Step 4: Upload Firmware

Picture of Upload Firmware
The first step before we can upload upload the firmware is to install the Arduino environment.  If you haven't already installed the Arduino environment or you have an older version than v1.0 you will need to download and install the latest version.  The Arduino software can be found here [http://arduino.cc/en/Main/Software].

Before you can compile the firmware, it's necessary to install the i2cEEPROM library.  This library gives us access to the 24lc256 family of serial EEPROM chips.  The chip is used if you want to support data storage.

Data storage is used to hold saved positions for playback, initial positions, and user custom data.  I'll show a custom step at the end of this instructable that illustrates how to easily add this feature.

Note: It's not necessary to have the serial eeprom connected to your Arduino for this application to work, but it is necessary to install the library in order for you to properly upload the firmware.  The firmware supports the features if the hardware is present.

Download the library as a zip here:
https://docs.google.com/open?id=0B-GOV0jkq9cROTk0ODg2ZTYtMzBkMi00ODY2LTgxZTItNTRiYWE5ODIzYTdi

The source can be found here:
https://github.com/Soshimo/I2C-Serial-EEPROM-Arduino-Library

Open an explorer window and open the folder at <Documents>\Ardunio.

Create a directory called libraries (if there isn't one already).

Unzip the contents into the libraries directory that you just created.  (Note this library only works for Arduino v1.0 and later versions).

Your directory structure should look like this:
<Documents>/Arduino/libraries/i2cEEPROM

Close all Arduino windows and restart the Arduino software.  

Check that the library was loaded correctly.  
Navigate to the library import menu: Sketch -> Import Library.  

On the list of available libraries to import you should see i2cEEPROM.  If you don't see it, make sure you have the path correct as above and shut down all instances of Arduino before restarting.

Finally download the SerialServoControl sketch:
https://github.com/Soshimo/Serial-Servo-Controller-Firmware

The easiest way to get the sketch into your Arduino environment is to create a new sketch called SerialServoControl.  Open the .ino file  downloaded above in notepad (or your editor of choice).  Copy the entire file's contents and paste into the window of the new sketch you created.  

Save the sketch.

Upload the sketch to your Arduino.

Next we will hook up the Arduino to the servo controller board we built in the last two steps.

Step 5: Connecting Arduino To Servo Control Board

The next step is to connect the Arduino to the servo control board that we built in the last couple of steps.  I am using an Arduino Duemilanove but you can use any compatible Arduino.  Note that older Arduinos built on chips before the ATMega328p will not work for this demo.  The ATMega168 does not have enough memory to store the program and you will not be able to run it.  Basically make sure you have at least 32k memory on your device available for programming.

The electrical interface is simple.  Ground must be connected between the breadboard and the Arduino   The servo control pins start at digital pin 2 for channel 0, up to digital pin 13 for channel 11.  Bring each pin out to the breadboard.  Tie them to the current limit resistor (100 ohm resistor).  Connect the servos to the terminals.  Make sure you pay attention to polarity.  The black wire, or ground, is always on the right.

The next step is to install the serial control software.

Step 6: Installing and Running the Software

Picture of Installing and Running the Software
I agonized over this step for awhile.  On the one hand I didn't want to have to provide an installer to bloat your system but I also didn't want to have the complexity of installing Visual Studio 2010 Express, compiling the source code, and building the executables.

In the end I went with simplicity and created an installer for the Serial Servo Control software.  The software requires .Net Framework 3.5sp1 or better (the latest version of the framework will be fine).  I'll also provide source code for modification.

The program is simple and the interface is crude and dated but it works.  You can control one of 12 different channels (0-11) with a slider, numeric up/down, or directly entering the value you want.  The values are in degrees and represent the servo position to move to.  In this version the servo traverses at it's fastest speed but I'll add ramping in a later version.

Here is the link to the installer (Admin Rights Required):
https://docs.google.com/open?id=0B-GOV0jkq9cRMDY4YWE3ZjAtNGU1YS00MDRiLTg3NzAtZjA2YzQ0ZDMyZWYx

Here is the link to the source code:
https://github.com/Soshimo/Serial-Servo-Controller


I'm not going to support modifying or building the source code so you are on your own for that.  One caveat, it requires the Windows XML Installer SDK to be installed on your machine.  If you don't have that installed you can just remove the installer project from the solution.  It's built for Visual Studio 2008 but can be upgraded to 2010 with no problem.  The project requires .Net framework version 3.5sp1.

Troubleshooting:
Do you see the Arduino COM port in the list of COM ports to connect to?
If no, make sure you have the correct Arduino drivers installed and that the board is powered on.

When you send commands to the firmware do you see any errors in the terminal window?
Sometimes the errors can help you pin point the problem.

Do some of the servos move but not others?
Check polarity on the servo cables, make sure the connection is secure - sometimes they can work loose if you haven't hot glued the terminals down (or otherwise secured them).

Can you reset the terminal from the Terminal -> Reset menu?
Make sure the USB cable is connected to the Arduino USB port.  The firmware uses the hardware UART.

Finally, if nothing is working and no feedback is given from the terminal window check all your electrical connections.  Make sure the servo cables are connected correctly.  Invest in a DMM if you don't already have one - you can find usable ones for under $50.  Check voltages with power applied.  Make sure you have at least 5V going to the servos or they won't move properly.  Check ground connections on the board.  Make sure all voltages are equal across all ground points.  Finally, remove power from the circuit and check continuity.  IMPORTANT! Make sure you remove power before checking continuity.  You could damage your DMM, board, Arduino, or yourself otherwise.  

I take no respsonbility, niether expressed nor implied for anything you may do while constructing or using this project.  This project is MIT licensed [http://www.opensource.org/licenses/mit-license.php] so you are free to use it and modify it at will.  I only request that you keep any attributes.  Have fun!



Step 7: [Optional] Add Serial EEPROM For Saving Values

This step I'll show you how to quickly add some storage capacity to our application.  We will use the Microchip 24lc256 serial EEPROM to give us 256k bytes of user data storage.  This includes servo set positions as well as frame playback (the software supports up to 10 frames).  It also includes about 200kb of user data storage for the application do do anything you want.  You can store a history of positions for a histogram later or whatever your imagination can think of!


The parts list is simple:
2 x 4.7k ohm resistors. [The 328p does have built in pullups on the SDA/SCL lines but if you want to chain multiple devices pull ups are recommended.  The Microchip datasheet recommends 4.7k ohm and the Atmel recommends not more that 10k ohm resistance, so I went with the datasheet]
1x Microchip 24LC256 IC - Low power serial (I2C) EEPROM chip.
Various jumpers

First add the IC to the board.  Pay attention to the orientation of the chip.  The front of the chip has a slight indentation.
Connect Vss to the breadboard ground. 
Connect A0-A2 of the Serial EEPROM to ground.
Connect Vcc to the Vcc tie point.  DO NOT connect to the breadboard power rail.  You will most likely damage your chip.
Connect the pull up resistors (4.7k ohm) from SDA/SCL to the tie point.
Connect Arduino 5V (3.3V may work as well, check the datasheet for the IC you are using) to the tie point.
Connect Arduino ground to the breadboard ground (if you have not already done so in a previous step).
Connect Arduino Analog In 4 (SDA) to the Serial EEPROM SDA (pin 5)
Connect Arduino Analog In 5 (SCL) to the Serial EEPROM SCL (pin 6)
Cool project.
It would be nice to have a web interface so I could control it with any wifi enabled tablet. I think it can be done with php.
soshimo (author)  rocketman2212 years ago
Thanks for the feedback!

I wish I had made the command handling more modular, maybe that's something I can work on. That way you could abstract the handling out and use any transport mechanism you want. In this example it's RS232, for your idea it would TCP/IP. Heck, you could even go wireless with Zigbee or something and really have some power.
I was actually thinking of using some wireless serial transmitters from sparkfun to connect the arduino to a serial port on my server.
But I was considering making pan/tilt mounts with servos for a couple of small security cameras.
Alexisa93 months ago

How many amps can this supply?

Hi! Thank you for this great and useful instructable! It helped me a lot with my robotic project. The only difference is that I used an Adafruit 16 channel pwm servo controller so I modified the arduino sketch and now everything works fine! I would try to hack the control program to add 4 control channels but unfortunately I don't know what to do :( I'm wondering when the new version of the software with ramping control will come! I have another question: I would try to use a 2kb EEPROM, is it enough space? I'm sorry for all those questions and I hope you will have the patience to answer me!! Thank you again for the useful work!!!
Very cool project. Thanks a bunch for the lesson on Arduino controlled servos.
jtlol1 year ago
Hello again,
We are making progress. I unplugged the second servo and tested with just one connected to channel 0. I was able to control the servo position with the slider. When I connected the second servo again, I experienced the same problem as before with both servos just humming and neither responding to commands. EEPROM is on the way so I will be able to test the full solution once the multiple servo issue is resolved. Thank you again for being so responsive.
soshimo (author)  jtlol1 year ago
Hmm, sounds like you might have a connection problem with the second servo (or both). Double check that the servos are not sharing the same data line (that's the yellow wire) and that both lines go to separate ports on the Ardunio. Also, make sure that the grounds and servo power wires are connected correctly on both. I suspect that you might be getting crosstalk.
jtlol1 year ago
It starts to twitch by the way I don`t have an eeprom :(
soshimo (author)  jtlol1 year ago
Okay, the EEPROM is necessary to store the frames. You can still control the servos with the sliders though. When you say it starts to twitch, does that mean the servos are moving the entire travel? When you slide the sliders from 0 to 180 degrees do you see the servo moving appropriately?

On a side note, you can take the code from the C# software - it's open source - and just use the communication DLL from that in your own software, but in order to store and recall frames for later animation you will need an eeprom.
jtlol1 year ago
Thanks for the reply. Yes, I am using the Rev 3 Arduino. Software is 1.0.4. Servos are EXI S1123 9G Micro servo. I am getting entries in the terminal server window: Frame 0 stored, Frame 1 stored, etc. when I hit the Write Frame button and Finished Sequence. 10 Frames Played when I press the Playback button. The terminal reset menu item does seem to put the servo back into startup position, but that is all that seems to work. Am I using the software correctly?
soshimo (author)  jtlol1 year ago
Okay, that's all good information. So it thinks it's talking to the eeprom. That may or may not be the case :). Let's see if the software is actually talking to the hardware correctly and work back from there. When you move the vertical slider of a channel that is attached to a servo do you see the servo move? What does the terminal window say after you move the slider?
jtlol1 year ago
(removed by author or community request)
soshimo (author)  jtlol1 year ago
Okay, what kind of servos are you using? Do you know the make/model? Also, did you see anything in the terminal window? You said you were using the Uno, was it Rev 3? I have an Uno Rev 3 that I can test with - something might have changed there. Finally, what version of the Arduino software are you using?
jtlol1 year ago
Hi Soshimo-san,
I have followed your instructions and can get power to the servos. Unfortunately, when I launch the sketch on my Arduino Uno, the servos move for a split second, then just hum with no movement. Can you suggest something that I can check or test?
soshimo (author)  jtlol1 year ago
Can you reset the terminal? Go to Terminal -> Reset menu.
Also, check the terminal window. Do you see any errors there? Is there any output in the terminal window?
What is the connection status (in lower right corner of window)?
megaduty2 years ago
Thanks Soshimo! This could help me with my 4 servo and 6 servo biped project.
Picture 5.pngPicture 6.png
soshimo (author)  megaduty2 years ago
Glad to help. Sweet looking 3d printed parts there. Maybe you can do an instructable when it's done. There is a new contest coming up that this would be a perfect fit for - grand prize is a $50k 3D printer. Feel free to use whatever you need from my instructable and happy hacking!
cupolo2 years ago
is what I was looking for a long time thanks so much ....... very nice ;)
bqbowden2 years ago
I have a prject for an ardiuno to control groups of selenoids on a schedule. Can you suggest a resurce where I can build this kind of circuit?
soshimo (author)  bqbowden2 years ago
I would start with the Arduino Playground [http://arduino.cc/playground/]. There are plenty of examples of using an Arduino to control a solenoid. Just extend one of those examples into a scheduler which shouldn't be too hard to do - plenty of examples of writing a generic scheduler on the web.
njkl442 years ago
take a look at my project:
http://www.instructables.com/id/Arduino-Wireless-Animatronic-Hand/
i control 5 servos just by power from arduino and no resistors or anything. I am currently powering 7 right now but i have a lipo im using
soshimo (author)  njkl442 years ago
I had brownout problems early on with a PTZ camera I was using to control from the Arduino so I've always been wary of using the power from the board. If you are powering from USB the maximum current you can source is 500-900ma. If from a walwart usually around 750-1000ma. I guess you can power your arduino from a lipo as well, but coming from the RC world (fixed and rotor wing craft) it's always a good idea to separate your command/logic supply from your servo/motor supply. As the motor battery drains, which they all do quickly during flight, you may lose control to your craft and suffer a catastrophic crash.
alexw21502 years ago
cool!
soshimo (author)  alexw21502 years ago
Thank you sir! It was my first instructable so I was a little nervous about it. I almost had as much fun documenting it as I had creating it, plus it forced me to fix a lot of little nagging things.