I don't like how my servo quickly jumps to the default middle position, so I commented out the line where DEFAULT_PULSE_WIDTH is set in Servo.cpp:

//servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH);

Unfortunately while this stopped the servo from moving on execution of attach, the position of the servo is now showing up as -52!

Just running the following in setup() is enough to generate the bug:

int servo_pin = 53;
Serial.print("Initial position: ");
Serial.println(servo.read());  //returns -52

This last line returns -52 no matter what position the servo is actually in, which has bad consequences (its initial movement is way off).

Note I get the same behavior when I set the DEFAULT_PULSE_WIDTH to 0 in Servo.h.

I am using a Mega -- an Elegoo clone -- on Windows 10.

Note I got the ideas for stopping the servo from moving on initialization from these threads:

  • you are using Windows 10 to control the servo somehow? – jsotola Mar 26 '18 at 23:16
  • Yes, mentioned at the end of my question. – neuronet Mar 26 '18 at 23:40
  • how are you using Windows to to control the servo? – jsotola Mar 27 '18 at 0:00
  • I'm using the IDE in windows just using a standard sweep algorithm. You can see code here: onlinegdb.com/HJIKaZv9z But really all you need to reproduce it is what I showed above in setup(). – neuronet Mar 27 '18 at 0:18

You seem to be starting from a fundamental misunderstanding of RC hobby type PWM servos. To put it simply, they do not electronically output any position information.

Until you have commanded a position and the servo loop has had a fair chance to seek to that, the position of the servo is utterly and entirely unknowable.

This is stated in the documentation of the Arduino servo system:



Read the current angle of the servo (the value passed to the last call to write()).

As this explains, the returned value is not the physical position, but rather the last commanded position.

Until you have commanded a position (something your code does not do) the return value of this call is physically meaningless.

| improve this answer | |
  • For those curious, what you would need is a servo with feedback. Something like this: adafruit.com/product/1404 I thought I was accessing that information, but no. – neuronet Mar 27 '18 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.