move servo slowly from original, to start position

Hey guys.

Ive been using the code below to move servos. When i upload the code to the arduino, it sends an initial value (currently 1500) to the motor, which causes the motor to move to that value as quickly as possible.

Is it possible to create a method that instead causes the motor to move slowly from the position that its in before uploading, to the predefined start position?


// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually DOES NOT WORK.

//1200 or 1500 is neutral (seen 2 places)

String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo

void setup() {
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println(“servo-test-22-dual-input”); // so I can keep track of what is loaded

void loop() {
while (Serial.available()) {
char c =; //gets one byte from serial buffer
readString += c; //makes the string readString
delay(20); //slow looping to allow buffer to fill with next character

if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
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.print("writing Angle: ");

readString=""; //empty for next input

This is always a problem with R/C servos. Some of them also move when simply being powered up and not even being sent pulses [GWS servos are notorious on this account].

To control servo velocity, you can send pulses that are interpolated between starting and ending positions, but this implies you know the starting position.

You might try the following, which I've never gotten to trying myself:

  • on startup, send 1500-usec pulses to the servo using digitalWrites in a simple delay loop, instead of using the regular servo library commands.

  • also rather than using 20-msec updates, send the 1500-usec pulses at a much slower rate, say 50 or 100-msec updates [experiment with this value].

The slow update rate should lower the servo torque and cause it to move much more slowly rather than locking in the normal servo loop and ramming over to the end position at full speed.

If you try this, please report back. :-)

Not easily. If you know what position the servo is at, you can use a for loop to move a degree at a time with suitable delays, but I assume you don’t know. That means that you need some kind of sensor or encoder to tell you that. Alternatively, you can write the servo’s position to EEPROM every time you move it and reload that info at startup. Even then, you have to assume that no manual intervention took place while the system was without power.

I assume you're talking about a standard PWM controlled angular servo.

There's no position feedback from this, so unless you have some way to predict what position the servo is in I don't see any obvious way to do what you want. I suppose you could connect up an external pot to whatever the servo was connected to and measure its position like that?

The only other approach I can see (without hacking into the servo wiring) would be to current limit the servo power supply so that it was only capable of moving slowly. So, although you might command an instantaneous move, it would actually move there feebly and slowly. Probably not what you want.

If you replace the servo with a stepper motor you could do what you want, but you'd still need some way to measure to measure the initial position so you're back to adding a pot or other sensor.

Hi Rich,

It seems to be dificult if you don't know what is the starting position of the servo. When I say servo position, I mean the pulse value needed to be in this position. Even if this value is stored in EEPROM you could not be not sure that the position will be kept by the servo when power supply is lost. The only way is to have a feedback of the servo position. Do you have it?


IN RC Vehicles we apply to power to the servo while we are attaching the steering so that then self centering on power up is not a problem.

So the simple tried and tested approach is to detach whatever is attached to the servo, power up the servo and then reattach whatever it was so that its pointing where you want whenever you next apply power.

Duane B