The code I used was this

int servo = 5;
int angle;

void setup() {

  pinMode(servo, OUTPUT);


void loop() {
  for (angle = 0;angle<180;angle++){
    digitalWrite(servo, HIGH);

  for (angle = 179;angle>0;angle--){
    digitalWrite(servo, HIGH);

as u can see I have set the delay after a particular step to be 65 milliseconds but it won't work, any delay greater than 64 milliseconds do not work and the motor just stops rotating.

what is the reason for this??


A servo motor doesn't just require a single pulse to send it to a specific angle. It requires a constant stream of pulses at the right frequency for it to track what the angle should be.

By sending one pulse you're just hinting where it might like to go to, then stopping. It's never going to get there, unless you keep sending those pulses fast enough.

Also if you don't send pulses then the servo will go into a "low power sleep mode" where it's not actively driving the output and it will go "floppy" where you can move the servo by hand.

Typically you need to send pulses at about 50Hz, which is one pulse every 20ms. Many servos will be more forgiving, but if you send too slowly they just won't know what to do.

If you want to be handling the pulses yourself then you need to do it in the right way. That is:

  1. Send a pulse of your desired length
  2. Delay for 20ms minus your desired length
  3. Go to 1 until your desired elapsed time has been reached.

For example:

for (angle = 0; angle<180; angle++){
  uint32_t ts = millis();
  while ((millis() - ts) < 100) { // 100ms delay
    digitalWrite(servo, HIGH);
    delayMicroseconds((angle * 11) + 650);
    delayMicroseconds(20000 - ((angle * 11) + 650));

That won't be exact, owing to the latency of the loop instructions, but it will be close enough.

| improve this answer | |

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.