Arduino Uno - which pin can control servos?

Hi there, been away for a while but started pikcing up a project that I left off for over a year. I haven't worked on this for a while so please forgive me if I am a bit rusty. I'm at a point where I would like to have my Arduino UNO control a servo. I've been reading a lot of posts out there, but there seems to be a couple of libraries out there to choose from (Arduino Servo library included in the IDE, and a Software Servo library, etc.).

Most people seem to recommend to use pin 9 and 10 to control the servo. However, I already had these two pins controlling two motor drivers so they are not an option. I've seeon other examples that seem to suggest that any output pin (PWM or not) can be used to control a servo, but I'm not sure if that's specific to just a particular servo library or not. I've also seen comments on Arduino 1.0.17 or newer having different capabilitites than the older boards when it comes to servo control. I'm also interested in finding out whether using a pin to control a servo will change the PWM characteristics of other pins.

I guess I am hoping to find out the pros and cons of the different servo librarys, and which other pins I can use to control the servo. Currently, digital pins 0-2 and 9-13 are all assigned to specific functions, but the rest of the digital pins and all the analog pins are still open to assignment.

Any help will be greatly appreciated!

Thanks,

  • K.

Oh by the way, is there a library that provides better resolution than 0-180 degrees? I'm trying a control a servo that has 6 turns lock-to-lock (i.e. 2160 degrees of travel from one end to the other) so a higher resolution than 180 positions would be quite useful.

Thanks again!

  • K.

I've seeon other examples that seem to suggest that any output pin (PWM or not) can be used to control a servo

Any pin can be used to command a servo.

Oh by the way, is there a library that provides better resolution than 0-180 degrees?

Can you direct this servo to any spot between 0 and 2,160 degrees?

PaulS:

I've seeon other examples that seem to suggest that any output pin (PWM or not) can be used to control a servo

Any pin can be used to command a servo.

Oh by the way, is there a library that provides better resolution than 0-180 degrees?

Can you direct this servo to any spot between 0 and 2,160 degrees?

Don't know about the OP's servo as he didn't post a link to it, but yes, using servo.writeMicroseconds() one can command a servo directly to anywhere within it's control range. There are servos that can turn some full or multiple turns (not to be confused with ex-servos that have been modified for continuous rotation service) over there command range.

Lefty

Thanks both, this is very helpful. I am using a GWS S125 6TD "Sail winch" servo. I read some documentation that claims it has a resolution of 4,096 positions (12-bit ADC?) across a motion range of 6 full turns, but I can't find that link anymore. Honestly I don't need that kind of precision. In fact, 0-180 (approx 12 degrees per step) is probably more than enough for my application, but I'm always interested in the boundary conditions of maximum control just in case the need comes up.

Folks, just hooked up my servo on pin 16 (Analog pin 3 on my UNO), hooked up the servo library with a few lines of code and it just worked! Thanks for the advice.

PaulS:

I've seeon other examples that seem to suggest that any output pin (PWM or not) can be used to control a servo

Any pin can be used to command a servo.

Can analog pins be used for servos?

Also, the SainSmart Sensor Shield v4 says it has 13 ports that can be used, but the digital ports are numbered from 0 to 13, for a total of 14. Is pin 0 an input-only port?

At the risk of taking over this thread, I have three more questions:

  1. Do digital servos need to be handled differently than analog servos?
  2. Does an electronic speed controller need to be handled differently than servos?
  3. And lastly, is there better documentation somewhere that I can reference so that I don't need to bother you fine people?

Can analog pins be used for servos?

Yes

  1. Do digital servos need to be handled differently than analog servos?
  2. Does an electronic speed controller need to be handled differently than servos?
  3. And lastly, is there better documentation somewhere that I can reference so that I don't need to bother you fine people?

No,
No,
Don't know

Thank you, AWOL.

I am starting on a new project to create a walking robot using 3 servos, i read Zoomcat's code and changed it is this corect im sort of new to coding arduino

#include <Servo.h>
String readString, servo1, servo2, servo3;
Servo myservo1;
Servo myservo2;
Servo myservo3;

void setup() {
Serial.begin(9600);
myservo1.attach(6);
myservo2.attach(7);
myservo3.attach(8);
Serial.println("three-servo-test-1.0");
}

void loop() {

while (Serial.available()) {
delay(3);
if (Serial.available() >0) {
char c = Serial.read();
readString += c;
}
}

if (readString.length() >0) {
Serial.println(readString);

servo1 = readString.substring(0, 4);
servo2 = readString.substring(4, 8);
servo3 = readString.substring(8, 12);

Serial.println(servo1);
Serial.println(servo2);
Serial.println(servo3);

int n1 = servo1.toInt();
int n2 = servo2.toInt();
int n2 = servo3.toInt();

myservo1.writeMicroseconds(n1);
myservo2.writeMicroseconds(n2);
myservo3.writeMicroseconds(n2);
readString="";
}
}

Lots of info on Servos and controlling them from Arduino HERE:

You could try something like below to send individual commands to each servo.

//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
    }
  }
}

BugZapp, if you enclose your code in tags, you don't get spurious smilies....

myservo2.attach(7);
myservo3.attach(8);
Serial.println("three-servo-test-1.0");

Will come out correctly as:

myservo2.attach(7);
  myservo3.attach(8);
  Serial.println("three-servo-test-1.0");

Use the # icon just above the :sweat_smile: