Pages: [1] 2   Go Down
Author Topic: How to control 3 servo motors with the UNO  (Read 8255 times)
0 Members and 1 Guest are viewing this topic.
Tucson, Arizona
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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: http://www.arduino.cc/playground/Learning/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.
« Last Edit: December 12, 2011, 04:43:51 pm by ardanino » Logged

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hope it will help you smiley-wink

Code:
#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
}
 
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

simple two servo code for use with the serial monitor.

Code:
// 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="";
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Tucson, Arizona
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.






Logged

Canton, Connecticut, United States, Planet Earth, SOL Galaxy, 3rd star on the left.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
To Infinity, And BEYOND!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
/*
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.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using an arduino to power six fine
Logged

Tucson, Arizona
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sweet, thank you guys.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Tucson, Arizona
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Tucson, Arizona
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you PaulS.

Quote
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?

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Typical servo power setup.

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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??
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1] 2   Go Up
Jump to: