Smaller change servo movement

I was having many problems with servo motor continually hunting for locking position.

I came across this excellent code from Michal Rinott which was designed around a potentiometer. I am now trying it with pushbuttons. The servo moves to position and quickly detaches on arrival.

Is there any way to make smaller servo movement, as wanting for camera panning. I moves quickly and with large gaps of movement. Thanks

// by Michal Rinott <>

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
const int servoPin = A3;
#define button3 6 // Pan - 
#define button4 5 // Pan - 
int val = 20;    // variable to start value position for servo
int lastVal = 20;    // variable to kick off value from the analog pin
unsigned long timeOfLastChange = 0;

void setup()
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);


void loop()

  if (digitalRead(button3) == LOW)// Right Pan
    if (val < 179)

  else if (digitalRead(button4) == LOW)// Left Pan
    if (val > 50)

  if (val < (lastVal - 5) || val > (lastVal + 5))

    myservo.attach(A3);  // attaches the servo on pin A3 to the servo object
    myservo.write(val);  // sets the servo position
    delay(15);           // waits for the servo to get there
    timeOfLastChange = millis();
    lastVal = val;
  else if (millis() - timeOfLastChange > 500)


You explicitly coded the servo to move in 5 degree steps. If you want smaller steps, explain why you did that.

Thanks Paul. Thats always the danger of cutting/pasting other people's code and not knowing what every line means. Now I know from you what that line means so I will trim it. I also sinse put a delay after incrementing my val value. That changed the increment movement of the servo but might not be the best answer now I can change the degree steps change. Thanks again

For fine control,use writeMicroseconds

Thanks AWOL, I will give that a try.

I am curious. Why do you detach the servo?

Why do you detach the servo?

It is a sledgehammer way of ensuring that it stops moving. Unless, of course, some force is applied to the servo output arm.

Thanks for answering why I did attach/detatch on the servo. The servo can be detached without movement worry as it has hardly any loading. Can you suggest a non sledgehammer way to fix servo regular buzzing. It's annoying as the servo even with barely any load keeps on updating its position. As it's on a security camera, I don't want it permanantly twitching. The servos are only wanted to occasionally change pan/tilt modes.

I had tried to log it's set to position and prohibited changes unless it was outside it's position where told to stay, but it was unreliable. The code I followed totally fixes my problem.

Can you suggest a non sledgehammer way to fix servo regular buzzing.

Actually I don't think I can, but detaching the servo seems wrong unless you don't mind it moving under the influence of an external load. It is possible that the servo is buzzing because a load is being placed on it and it is compensating by attempting to hold its position.

Digital servos are often more prone to buzzing whilst holding their position. What type of servo are you using ?