Using Multiple Servos with Arduino

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?

Any idea what is going on here?

Without knowing how you powered the servos? No. I'd guess, though, that you are expecting the Arduino to power and control the servos. That is not going to happen. It can CONTROL the servos. It can NOT power the servos.

Hi all, new to the forum here, 1st post and all that....

Being a noobie and all that, I was just messing with this last week.

redbird:
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.

Any idea what is going on here?

Try disconnecting the ground of your power supply from the Uno. It really shouldn't matter, but I've had the same problems you're having.

My servos are connected to a power supply that's not connected to the arduino, the only connection with the arduino from the servos is the PWM wire. In my case, I'm using 4xAA batteries to power the servos, and a 9v battery to power the arduino.

When I powered one servo from an arduino, it worked fine. When I tried two, it became erratic. The main green LED on the arduino would dim, then the arduino would reboot.

Now that I have 2 power supplies, not connected in any way, it works.

Randy

When you set servos to go directly to certain positions the shaft is moving very quickly and so there can be a lot of skip ups (I know this because I've been messing with this myself recently). A better way of doing it without putting a lot of wear and tear on the servos is using a for loop to gradually get the servo to the position you want. In this way you don't get sudden jerks but a smooth transition to the angle you want.