# Controlling more than 12 Servos with the Arduino Servo library

I'm using Teensy hardware specifically. I have a Teensy 2.0 and a Teensy 3.0, and from the documentation it seems like there are two 16 bit timers available, and each should be able to control 12 servos. However, I've attached a logic analyzer and have confirmed that only the first 12 servos attached ever function.

Is there anything special I have to do with my sketch in order to convince the Servo library to allocate the second timer for servos attached beyond number 12?

This works:

#define NUM_SERVOS 12

Servo servos[NUM_SERVOS];

// teensy 2.0 pins
int pin_assignments[NUM_SERVOS] = {0, 1, 2, 3, 4, 5, 20, 19, 18, 17, 16, 15};

void setup() {
for(int i = 0; i < NUM_SERVOS; i++) {
servos[i].attach(pin_assignments[i]) ;
}
}


But this, below, only ever shows activity on the first twelve pins attached:

#define NUM_SERVOS 24

Servo servos[NUM_SERVOS];

// teensy 3.0 pins
int pin_assignments[NUM_SERVOS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};

void setup() {
for(int i = 0; i < NUM_SERVOS; i++) {
servos[i].attach(pin_assignments[i]) ;
}
}

• In the second code snippet, is the line for int pin_assignments meant to be commented out? – Ian Feb 20 '13 at 14:34
• haha no, it's not, but that's certainly not the problem :) – Ian McMahon Feb 20 '13 at 15:59

I'm guessing you're using the arduino libraries (haven't used the Teensy), in which case, if you read through the documentation (http://arduino.cc/en/Reference/Servo), you'll see that the Arduino servo library

Supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega

You can manually bitbang the pwm signals for your servo if you don't have too many other time-critical things going on, or make/get a separate serial servo controller, or do any of a number of other things. This is good reading to understand what is going on: http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM (I would click through to the original document for a visual view of the different timer modes).

p.s. what are you doing with 24 servos and one microcontroller? sounds exciting!

The problem lies in the Servo.h file in Arduino\libraries\Servo. The Teensy 3.0 parameters are not defined in this file and therefore it is assumed to have only one timer. Changing the default action to copy the second last action was sufficient for my purposes, I changed it the file as follows:

From:

#else  // everything else
#define _useTimer1
typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t ;
#endif


To:

#else  // everything else
#define _useTimer3
#define _useTimer1
typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ;
#endif


This only managed to get me 16 servos though. I also tried adding other timers with no success in increasing the number of available servos. Hopefully this proves useful to someone.

There is another Servo library that can drive as many servo's as you want :Link to Arduino Software Servo. Good luck