Control Servo with potentiometer using Arduino

Project Video
Project Details

In this video you will learn how to control a servo using potentiometer (variable resistor) and Arduino.

Project Code

 Controlling a servo position using a potentiometer (variable resistor)
 by Michal Rinott 

 modified on 8 Nov 2013
 by Scott Fitzgerald

 Update by Ahmad Shamshiri for in Ajax, Ontario, Canada
 Watch video for this code at
 This code is taken from


Servo myservo;  // create servo object to control a servo

int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  // Servo knob demo by
  Serial.begin(9600);          //  setup serial
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

void loop() {
  // Servo knob demo by
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  Serial.print(" ");
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  // Servo knob demo by
  delay(200);                           // waits for the servo to get there

Required File(s)

If you found this Arduino course helpful, please support me so I can continue creating content like this. support me via PayPal