Using Multiple Servos with Arduino

So, I have 8 servos that I want to drive with my Arduino. I know I shouldn't connect all the servos to the GND and +5v of Arduino. Somebody told me that servos when working pull about 2 amps each Is this correct? How should I hook an external battery with Arduino to my servos? I am told that I can hook up DC jack into Arduino, an external batteries GND to Arduino's GND, and the external batteries + to +5v on Arduino. Is this correct? Please help me out here.

Yep that's right. Have a look at my pic below, and go and read DuaneB's work, starting here. He has some test results for current draw.

Basic servo setup.

Thanks Zimbo for the tremendous help. I'm enjoying the website you linked to your post, it's very helpful. What I'm making is a 4 legged spider! (I really hope it works :slight_smile:
Zoomkat, makes perfect sense. Thank you as well.

Thanks Zimbo

That's Jimbo 8), but you're welcome all the same.

Yep DuaneB seems to be the go-to-guy for servo know-how.

[zen]Is a 4-legged spider really a spider?[/zen]

oh it's so help me actually. Now, I use 5 servos in my project. Can anybody help me for give a little sketch ? I am so confuse to start it. :cold_sweat:

aziz_wicaksono:
oh it's so help me actually. Now, I use 5 servos in my project. Can anybody help me for give a little sketch ? I am so confuse to start it. :cold_sweat:

Several servo control code.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

hi! how to program it if i'll be needing more than 2 servo motors? thanks

i'll be making snake robot and there is 14 servo motors included.

just like this Nagaina The Snake Robot - YouTube

jmichael26:
hi! how to program it if i'll be needing more than 2 servo motors? thanks

Zoomcat's code in reply #6 shows 4 servos already, on pins 6, 7, 8 and 9.

ahh but what if i'll be using 14 servos ?

Well you'll need an Arduino with 14 spare digital pins. Uno has 20 pins if you count the analogs, but lose 2 for Tx Rx leaves you a theoretical 18. Mega has dozens more .

I'm not sure if there's an absolute maximum that the servo library can handle though, sorry.

my friend told me that i need to use sevo drivers to accomodate those servos. but i'm just worried about its program . please help me

JimboZA:
Well you'll need an Arduino with 14 spare digital pins. Uno has 20 pins if you count the analogs, but lose 2 for Tx Rx leaves you a theoretical 18. Mega has dozens more .

I'm not sure if there's an absolute maximum that the servo library can handle though, sorry.

but how is the program if i'll be using 14 servos? just copy paste your #6 program? thanks

zoomkat:
Several servo control code.

//zoomkat 11-22-12 simple delimited ',' string parse 

//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added

String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);

//myservoa.writeMicroseconds(1500); //set initial servo position if desired

myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

//expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

int n = readString.toInt();  //convert readString into a number

// auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        { 
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
        readString=""; //clears variable for new input
      }
    } 
    else {   
      readString += c; //makes the string readString
    }
  }
}

Sorry, i have an question for this code.
How these servo run, what is control it ? I found pins 6,7,8,9 in arduino connect to servo but I didn't find pin that receive order control servo from other part like varistor...
Thanks !

How these servo run, what is control it ? I found pins 6,7,8,9 in arduino connect to servo but I didn't find pin that receive order control servo from other part like varistor...

You send data to the Arduino, from the Serial Monitor application, to control the servos:

          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);

like varistor..

Can you explain just what the "varistor" is?

Hello to everyone.I have 6 servos and Arduino Uno.The connection that i make is 3,5,6,9,10,11(all the PWM of Arduino).It will be a problem?Thanks in advance.

jack1992:
Hello to everyone.I have 6 servos and Arduino Uno.The connection that i make is 3,5,6,9,10,11(all the PWM of Arduino).It will be a problem?Thanks in advance.

Servos are NOT driven using PWM (the way Arduino does it), so there is no need to use just PWM pins.

PaulS:
Servos are NOT driven using PWM (the way Arduino does it), so there is no need to use just PWM pins.

ok thank you sir

I am trying to use 2 servos but it is not working. When I connect only one servo, it works properly. When I connect the second servo, both servos get very erratic and after a few movements they drive to a limit and stay there. If I disconnect one servo control wire, the remaining servo acts normally again.

I initially tried this on a Diecemilia and thought maybe it was caused by an old board. I swapped out for an Uno but get the same result.

Servos are powered by an external 5V power supply; ground of power supply connected to Uno and servos.

The code is as simple as I could get, just trying to get the system to work.

#include <Servo.h>

Servo sideservo, forwardservo;  // create servo object to control a servo

void setup() {
  sideservo.attach(9);
  forwardservo.attach(10);  
}

void loop() { // minimal movement to avoid damage to system
  sideservo.write(60);
  delay(1000);
  forwardservo.write(80);
  delay(1000);
  sideservo.write(90);
  delay(1000);
  forwardservo.write(100);
  delay(1000);
}

Any idea what is going on here?