How to control 3 servo motors with the UNO

I have recently just finished building a hexapod robot from a book and I want to control the 3 servo motors on the robot. I have read the tutorial on this site: Arduino Playground - SingleServoExample and http://www.arduino.cc/en/Tutorial/Sweep , but I want to learn how to control 3. From what I understand I need another power source besides the 5v that is on the UNO. I have also seen some tutorial videos that show the use of a Proto Board. Any help would be greatly appreciated.

Hope it will help you :wink:

#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
 
void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
 
 
void loop()
{
  
    myservo.write(90);              // tell servo to go to position 90 digrees
    delay(1000);                       // waits 1000ms for the servo to reach the position
    myservo.write(0);              // tell servo to go to position 0 digrees
    delay(1000);                       // waits 1000ms for the servo to reach the position
    myservo.write(180);              // tell servo to go to position 180 digrees
    delay(1000);                       // waits 1000ms for the servo to reach the position
}

simple two servo code for use with the serial monitor.

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();
      
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}

Thank you Zoomkat, I will try that code...so if I want to add another servo motor to that 2 servo code, I should just add another:

#include <Servo.h>
String readString, servo1, servo2, servo3;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
Servo myservo3;

Here are some photos of my 3 servo hexapod. The design is from a book by Karl Williams called Insectronics.

Yes, you've hit it exactly! I use similar code with a simple ping-bot, which controls 3 servos for the 2 drive wheels and the ping))) turret.

/*
PingBot-II  By Stephen Griswold (gelfling6@hotmail.com)

The code is from a project on Instructables, called the PingBot, using
the Adafruit Motor Controller.  I, however, use a pair of modified Futaba
S3004 servos, converted to constant rotation servos by redirecting the
position potentiometer out the side, and removing the stop tab from the
main drive gear.  I removed the code for the motor control board, and added
the two servo values to the left & right wheels.  With this, I had to divide
the wait time for the servos, because the servos were actually running a little
faster than the motors would.

The body, is a knock-off of a Lock-N-Lock food container, power is provided by a
7.2V RC battery, and power is regulated down to +5V for the servos & PING)), through
a 7805 voltage regulator. The placement of the board & battery are loose inside the
container, but slow movement doesn't jostle it around. The pivot wheel on the back, is
a movable model aircraft wheel, and yes, it is oversized. This provided a little extra
angle for detecting obstacles that are lower. It's not perfect, but it's working!
*/

#include <Servo.h> // Enables the Servo library


Servo PingServo;  // Servo for the PING)) sensor position
Servo LeftWheel;  // Servo for the Left Wheel
Servo RightWheel; // Servo for the Right Wheel

int minSafeDist = 11 ; // Minimum distance for ping sensor to know when to turn, 11-Inches
int pingPin = 7; // Ping sensor is connected to port 7
int centerDist, leftDist, rightDist; // Define variables center, left, right distance
long duration, inches; // Define variable for Ping sensor


void setup() {
PingServo.attach(10); // Servo is attached to pin 10 in the motor shield
PingServo.write(90); // Center the Ping sensor (puts it at 90 degrees)
LeftWheel.attach(9); //  Left Wheel Servo
RightWheel.attach(8); // Right Wheel Servo

Serial.begin(9600); // Enables Serial monitor for debugging purposes
Serial.println("Serial test!"); // Test the Serial communication

}

void AllStop() {
LeftWheel.write(90); // Stop Left Servo
RightWheel.write(90); // Stop Left Servo

}
void AllForward() { // Makes the robot go forward
LeftWheel.write(180); // Motor 1 goes forward
RightWheel.write(0); // Motor 2 goes forward
Serial.println("Going forward"); // Prints a line in the serial monitor
}
void turnRight() { // Makes the robot go right
RightWheel.write(180); // Turns motor 2 backward
LeftWheel.write(180); // Motor 1 goes forward
delay(800); // Time required to turn right (0.8 seconds)
Serial.println("Motors going Right"); // Prints a line in the serial monitor
}
void GoBack(){ // Makes the robot go back
RightWheel.write(180); // Motor 2 goes back
LeftWheel.write(0); // Motor 1 goes back
delay(800); // Time Required to go back (0.8 seconds)
Serial.println("Backward"); // Prints a line in the serial monitor
}
void turnLeft() { // Makes the robot go Left
RightWheel.write(0); // Motor 2 goes forward
LeftWheel.write(0); //  Motor 1 goes backward
delay(800); //Time Required to turn left (0.8)Seconds
Serial.println("Motors going Left");// Prints a line in the serial monitor
}

// Starts the loop to decide what to do
void loop()
{
LookAhead();
Serial.print(inches);
Serial.println(" inches"); // Prints a line in the serial monitor
if(inches >= minSafeDist) /* If the inches in front of an object is greater than or equal to the minimum safe distance (11 inches), react*/
{
AllForward(); // All wheels forward
delay(110); // Wait 0.11 seconds
}else // If not:

{
AllStop(); // Stop all motors
LookAround(); // Check your surroundings for best route
if(rightDist > leftDist) // If the right distance is greater than the left distance , turn right
{
turnRight();
}else if (leftDist > rightDist) // If the left distance is greater than the right distance , turn left
{
turnLeft();
}else if (leftDist&&rightDist<minSafeDist) // If the left and right distance is smaller than the min safe distance (11 inch) go back
{
GoBack();
}
}
}

unsigned long ping() {
pinMode(pingPin, OUTPUT); // Make the Pingpin to output
digitalWrite(pingPin, LOW); //Send a low pulse
delayMicroseconds(2); // wait for two microseconds
digitalWrite(pingPin, HIGH); // Send a high pulse
delayMicroseconds(5); // wait for 5 micro seconds
digitalWrite(pingPin, LOW); // send a low pulse
pinMode(pingPin,INPUT); // switch the Pingpin to input
duration = pulseIn(pingPin, HIGH); //listen for echo

/*Convert micro seconds to Inches
-------------------------------------*/

inches = microsecondsToInches(duration);
}

long microsecondsToInches(long microseconds) // converts time to a distance
{
return microseconds / 74 / 2;
}

void LookAhead() {
PingServo.write(90);// angle to look forward
delay(175); // wait 0.175 seconds
ping();
}

void LookAround(){
PingServo.write(0); // turn ping)) right
delay(320); // wait 0.32 seconds
ping();
rightDist = inches; //get the right distance
PingServo.write(180); // look to the left
delay(620); // wait 0.62 seconds
ping();
leftDist = inches; // get the left distance
PingServo.write(90); // face straight ahead
delay(275); // wait 0.275 seconds

// Prints a line in the serial monitor
Serial.print("RightDist: ");
Serial.println(rightDist);
Serial.print("LeftDist: ");
Serial.println(leftDist);
Serial.print("CenterDist: ");
Serial.println(centerDist);
}

I've been able to control up to 5 servos with a nunchuck controller (I was bored) similar, simply make sure the servo output is linked to a PWM output.

I am using an arduino to power six fine

Sweet, thank you guys.

@njkl44, I hope you mean "control" and not "power"

Okay, I think I have the code down, but now there is another question. How do I power all 3 servos? I have read on the forums here and through searching around on the web that you cannot power more than one servo using the Uno alone. Does anyone know a good tutorial explaining how to hook up a external power source?

I have read on the forums here and through searching around on the web that you cannot power more than one servo using the Uno alone.

You misread stuff, then. You can't power more than 0 servos from an Arduino.

Connect the positive terminal of a battery to the positive leads of the servos.
Connect the negative terminal of a battery to the negative/ground leads of the servos and the ground of the Arduino.

Connect the control wires to 3 Arduino digital pins.

Thank you PaulS.

Connect the positive terminal of a battery to the positive leads of the servos.
Connect the negative terminal of a battery to the negative/ground leads of the servos and the ground of the Arduino.

Connect the control wires to 3 Arduino digital pins.

I'm really new to Arduino and wiring things, can you explain this a little better for me?

can you explain this a little better for me?

No, not really. A battery has a positive terminal and a negative terminal (or ground). Each servo has a positive wire, a ground wire, and a control wire. The positive wire and positive terminal need to be connected. Ditto for the negative/ground terminal and wire, which also needs to be connected to the Arduino ground.

Typical servo power setup.

Hi, i am also uisng servo motors to do my project. As what author mentioned i need 3 servo motors also. Can anyone suggest about the external power of the servo motors? Using AA battery??

Can anyone suggest about the external power of the servo motors?

I have a hose. Can you tell me how long it will take to fill my bucket. I won't tell you what size hose, what the water pressure is, or how big my bucket is, but I want you tell me how long it will take to fill the bucket.

Pretty hard, isn't it. You've told us nothing about your servo. How much current does it draw? What voltage is it? What duty cycle will you be expecting? What load is the servo moving? How fast?

There are so many things that you haven't told us that even a guess as to the external power requirements would be impossible. All we can say now is that you need some.

slt,
je viens de faire un programme arduino pour un servo de type ACE R/C DS1015 mais je trouve pas son largeur d'impulsion pour commancer le programme...
px vous m'aider et merci

Et, maintenant en anglais, peut-etre?

Hi guys,
This is my first time that I read about Arduino as a controller of servomotor, thus I am fully new on this world.
My inquiry is "Can I control a group of servomotors respectively (one after another)?"
Thank you.

Yes. You should read the Servo webpage in the Learning section.