Problem using multiple servos with servo.h

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
luv2solder
 
Posts: 42
Joined: Tue Jun 21, 2011 4:15 pm

Problem using multiple servos with servo.h

Post by luv2solder »

I can use Servo.h (from the servo library) to control one or two servos on pins 9 and 10 of the Mega 2560 (v2). However, I can't get it to correctly run three or more servos.

Here is the sketch:
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
int angle1 = 0;
int angle2 = 0;
int angle3 = 0;
void setup(){
servo1.attach(9);
servo2.attach(10);
servo3.attach(8);
}
void loop()
{
angle1 = 130;
servo1.write(angle1);
angle2 = 130;
servo2.write(angle2);
angle3 = 130;
servo3.write(angle3);
delay(5000);
angle1 = 161;
servo1.write(angle1);
angle2 = 161;
servo2.write(angle2);
angle3 = 161;
servo3.write(angle3);
delay(5000);
}

All three servos should simultaneously sweep from 130 degrees, wait 5 seconds, go to 161 degrees, wait 5 seconds, and repeat. It does this fine if I use two servos (on pins 9 and 10), but if I add another from pin 8, that servo is WAY out of synch with the other two. Besides that, it usually sweeps from 130 to 161 degrees but often (perhaps once per minute) barely moves per cycle, as if it were moving only a couple of degrees. Any idea why the pin 8 servo is so flaky? BTW, I am using a 9V 1500 mA external power supply to power the Arduino board and a separate 5V 3A power supply for powering the 3 servos, all of which have no load.

Does servo.h require more code to correctly operate > 2 servos?

Is there an alternative to servo.h?

Is there a servo shield that operates multiple (preferably at least eight) servos?

Thank you!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Problem using multiple servos with servo.h

Post by adafruit_support_rick »

I'm no expert on this, but I'm wondering how you managed to get servos working on pins 9 and 10? Those are Timer 2 PWM outputs, and the Servo library looks as if it only uses Timers 1, 3, 4, and 5 on the Mega2560.

Try putting your servos on Pins 44, 45, and 46, which are Timer 5 outputs. The servo library seems to prefer Timer 5.

User avatar
luv2solder
 
Posts: 42
Joined: Tue Jun 21, 2011 4:15 pm

Re: Problem using multiple servos with servo.h

Post by luv2solder »

I used pins 9 and 10 because all examples I saw online, as well as in the Arduino Cookbook (second edition), said to use those pins.

I used the pins you mentioned: same problem. Then I used a new servo (exact same type, just a different one) and THEN the servos operated in synch. Bizarre.

Thank you for your help.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”