[beginner] how to control 2 servos!

hi, i just brought my first arduino duel, and 2 servos for testing.

I plug the devices as the pic shown in "sweep" example and modify something, but how come my BOTH servos won't work together? but if i stay with one servo it works just fine, so what is wrong?

Servo myservo; // create servo object to control a servo
Servo myservo2; // a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9);
myservo2.attach(10); // 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);
myservo2.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);
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

Insufficient power on the +5v line to drive more than one servo at a time?

dc42:
Insufficient power on the +5v line to drive more than one servo at a time?

That's my guess also. Try powering the two servos with a seperate source of +5vdc rated at least to 1 amp (for 2 servos), say fresh alkiline 4 AA cells wired in series. Red servo wires wire only to positive terminal of AA cells, servo ground wires to negative of AA cells, and you also need a wire from AA negative to arduino ground pin.

Lefty

Thank you both of you! It finally works !
but how come my arduino can't handle 2 micro servos (towerpro 9g sg90) without external power supply? Because i saw some videos whose servoes work with onboard usb power supply without problem...

None of the sites I looked at that sell that servo specifies the stall current, so there's no telling how much current it needs.

[EDIT: just found a page where someone measured the stall current at 0.6A. So 2 of them is well above the current limit for USB.]

ttttkk:
Thank you both of you! It finally works !
but how come my arduino can't handle 2 micro servos (towerpro 9g sg90) without external power supply? Because i saw some videos whose servoes work with onboard usb power supply without problem...

Servos have a motor inside them (duh) and how much current it may require depends on several factors like how much mechanical load is on the servo, how large is the servo, if more then one servo are they both going to be turning at the same time in the program, etc. Most servo manufactures are very poor a listing min and max current requirements for their servos. Using USB power there is a fixed 500ma current maximum allowed before either the on-board thermofuse opens up or the USB port shuts down or both.

Bottom line, it is never a good idea to power servos (even one) from the arduino +5vdc pin, no matter if using usb power or power from the external power connector. An independent voltage source rated at 4.5-6vdc and budgeting one amp of current capacity for each servo being powered is the most conservative and reliable method.

Lefty