Servo Library Seems Unreliable

I’m not sure why, but it seems like the Arduino servo library has some sort of periodic twitch to it. Every few seconds or so it gives a pulse that’s too long, which makes the servo try to get all the way over to one side for a split second. Not a problem with cheap servos that have a slow response, but the fancy-pants ones that we’re using are giving an annoying flicking movement to everything. So I decided to just use a simple loop instead:

#define MINIMUMPULSE 550    // Minimum servo position
#define MAXIMUMPULSE 2400   // Maximum servo position

void soft_servo(char pin, int angle){
  angle=constrain(angle, MINIMUMPULSE, MAXIMUMPULSE); //keep it within a safe range
  digitalWrite(pin, HIGH); //start the pulse
  delayMicroseconds(angle);       // Length of the pulse sets the motor position
  digitalWrite(pin, LOW);  //end the pulse
}

void loop(void){
  do this;
  do that;
  soft_servo(whichservo,whatangle);
  delay(10); //give things a bit of time to settle. Something between 1ms and 40ms
}

Advantage: no more funny pulses. Interrupts† and timers are free for other uses.

Disadvantage: has to be called every few milliseconds or else the servo goes slack. Maybe there’s a way to program a modern digital servo to hold a position indefinitely with one command pulse? That would be handy!

In practice I haven’t had any trouble running a servo refresh in my main loop. 10mS is an eternity in which to calculate the math and gather sensor data.

†Disable interrupts during the pulse or else it may get elongated and send the servo into unknown territory!

One Response to “Servo Library Seems Unreliable”

  1. I intend to use a LynxMotion SSC-32 for my projects. Its relatively cheap but has several important features

    Firstly is one is doing sequencing you can send a group of comands and then say ‘Start These’
    Secondly you can tell the unit how long to take from start position to end position on each servo
    Lastly you can turn servo’s off after use (very handy if you getting low on batteries)

    Plus you chat to it using simple serial. Easy for a PicAxe or Arduino

Leave a reply to Marcwolf Cancel reply