Servo motor misbehaving when potentiometer is at zero

Hey everybody :slight_smile:
I have created the attached circuit and written the following code:

#include <Servo.h>

const int SERVO=9; //Servo on Pin 9
const int POT=0;    //POT on Analog Pin 0

Servo myServo;
int val = 0;     //for storing the reading from the POT

void setup()
{
    myServo.attach(SERVO);
}

void loop()
{
    val = analogRead(POT);           //Read Pot
    val = map(val, 0, 1023, 0, 179); //scale it to servo range 
    myServo.write(val);              // sets the servo 
    delay(15);                       // waits for the servo
}

It works really ok apart from when the potentiometer is at zero. Then, the servo starts spinning and stopping on its own. Any ideas?

Add a Serial.print() line so you can see what the value of the variable val actually is.

And. temporarily increase the delay(15) to delay(200);

...R

Hi,
How big is your "9V battery"?

Thanks..Tom.. :slight_smile:

Yes, you may be hitting the endstop and pulling lots of current, and your supply is inadequate
(people always underestimate the current demand of servos and motors - they use lots of current,
small servos typically require 1A at 6V, and if you are using a small 9V battery it cannot possibly
supply that - they are completely unsuitable for motors)