6
$\begingroup$

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]) ;
  }
}
$\endgroup$
2
  • 2
    $\begingroup$ In the second code snippet, is the line for int pin_assignments meant to be commented out? $\endgroup$
    – Ian
    Commented Feb 20, 2013 at 14:34
  • $\begingroup$ haha no, it's not, but that's certainly not the problem :) $\endgroup$ Commented Feb 20, 2013 at 15:59

5 Answers 5

3
$\begingroup$

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!

$\endgroup$
0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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

$\endgroup$
0
$\begingroup$

You only need to chage the value of:

#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer

Change 12 for your value.

$\endgroup$
0
$\begingroup$

Instead of driving the servos with the Teensy, you could use a servo driver. The one in the link has an I2C servodriver that handles 16 servos and can be chained together to address more servos. This will take the load off the microcontroller and leave processing power for other tasks.

$\endgroup$
1
  • $\begingroup$ Didn't see the vintage of this question before answering. Will leave my answer in case someone drops by $\endgroup$
    – jkp
    Commented Jan 26, 2021 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.