Let's Make Robots!

Driving up to 48 servos with a Spider controller

Controls more servos than you can fit on one robot :p
_48_servos_array.zip850 bytes

This tip/walkthrough will work with all Arduino Mega PCBs and their clones. I have found the instructions for the servo command to be a little vague so in this tip I will share my research.

The Spider controller is an Arduino Mega compatible PCB. Unlike the Arduino Mega the Spider controller was designed specifically to drive a large number servos. This made it ideal for this tip/walkthrough.

The Arduino servo library is capable of driving up to 48 servos. After a bit of experimenting I found that the servos can be assigned to any of the digital pins from D0 to D63. For those thinking that the digital pins only go to D53, analog pins A0 - A9 are digital pins D54 - D63.

For those who have never used the servo command before I will start at the beginning using the attached code as my example. The first line of the code "#include <Servo.h>" means that the instructions needed to generate the servo control signals will be included in the code to be uploaded to the microcontroller.

Before the setup() function you must define your servos same as you define your global variables. I've simply called my servos s00 to s47. You should name your servos with descriptive names to make your program easier to read.

The Servo command uses a timer for each 12 servos used starting with Timer 5. As these timers are also used for commands like PWM you need to plan your pin assignments to avoid conflicts.

1   to 12 servos use timer   5          disabling PWM on pins 44,45 and 46.
13 to 24 servos use timers 1&5       disabling PWM on pins 11,12,44,45 and 46.
25 to 36 servos use timers 1,4&5    disabling PWM on pins  6,7,8,11,12,44,45 and 46.
37 to 48 servos use timers 1,3,4&5 disabling PWM on pins 2,3,5,6,7,8,11,12,44,45 and 46.

Once you have defined your servos you need to attach them to the physical pins of the controller. In the setup() function you can attach your servos to any of your digital outputs. In my sample I have avoided digital pins 0 and 1 as they are used for serial communications. At this point even thought the servos have been attached to a pin they are not receiving any signals.

The first time you write to a servo using either the servo write() or writeMicrosecond() command the pin will be initialised and the servo will receive a control signal. depending on your application you may need to initialise the servos in the setup() function. In my example I initialise the servos in the loop() function.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

here's a link to a video of 8 tentacle robots (32 servos), with your code as a jumping point, cheers mate.

They worked fine until i added sensors, now one of them doesn't work, works with simpler code so must be a conflict with my crap amateur coding.

But anyways, thanks for the platform to begin my exploration in this area!

the vids by a friend of mine, not paricularly thorough but you get the idea.


That looks really cool. Could you do a post with some behind the scenes photos/video? I would be great to see how you mounted and powered your servos.


It's on display in another city at the moment but I'll pick it up again in 4 weeks, I'll post some detail shots/vids for you then. I'll also do some investigation then on why one arm seems to be just twitching then too. Works fine with your code but when i add the sensors goes a bit buggy.

Without knowing what sensors you are refering too here are some possible solutions to your problem. If it is a code problem then maybe your code is trying to update the servo positions too slowly or even too quickly. I find updating the servo positions every 15mS seems to provide the smoothest movement.

If you are using an IR sensor then you must remember they usually use high current pulses. Add an electrolytic across the power connections of each sensor.

Make sure you use plenty of noise filtering capacitors like these:

Try adding one to each servo connector like this:

The electrolytic is optional. Depends on you power supply and cable lengths.

If your servo voltage is too high then servos can become twitchy as well.

hope this helps.

cheers for this, plenty to think about there.

I'm using 4 ultrasonic sensors, and i have some pretty lengthy cables to both servos and sensors. Just found it odd that all servo's go sweetly with your code but not with the sensors.

I'm updating the servos every 25mS, but I have got an odd loop within the timer function for that which is just crunching variables repeatedly between updates. This could well be the issue as it's pretty clunky (I'm still a bit random and lazy with my code, just a codebaby really).

thanks for taking an interest in this! As I said I'm at a distance from the actual beasts themselves right now so it's still all a bit theoretical.

I purchased a spider controller for an art installation I am doing in Chicago- it is beautiful. I have 40 servos running off the code provided here- thank you, it looks very cool.

Any hints on how I would go about applying the Servo Sweep that comes as an example in Arduino? To all 40 servos?    I am new to all of this, however I know the basics of arduino/processing and mid-level in python.  I am a skilled in rapid prototyping, so hopefully I can offer up help to someone in that area to return the favor.  Not just a newbie without any skills to share!  I'm sure this is easy, just thought I would check here first.  thanks.


// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
int pos = 0;    // variable to store the servo position
void setup()
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
void loop()
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position

I have updated the attached sample code. The new code is similar to the servo sweep example except that you can select how many servos from 1-48. Each servo can move at a different speed and / or direction.

That is very cool.  I tried it this morning and it works great.  Thank you very much... I will post pictures once it's finished.  It's a 1ft x 30 ft, undulating strip of turf grass that hangs from the ceiling using an aluminum armature, 40 servos, piano wire and few other things.  Setup could be applied to a crawling worm bot for sure.


Please post videos or at least photos. I love to see what people create with my inventions!


Here's my Hexadroid looks like. All it can do right now is stand and march in place. 

I'm doing the program for walk but the kinematics is killing me. Is there any calculations or sample program for 3DOF mentioned here? That would really help me a ton. Thank you!