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
 http://www.arduino.cc/en/Tutorial/Knob

 Update by Ahmad Shamshiri for Robojax.com in Ajax, Ontario, Canada
 Watch video for this code at https://youtu.be/X_drE5tLbz0
 This code is taken from http://robojax.com/learn/arduino
*/

#include 

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 Robojax.com
  Serial.begin(9600);          //  setup serial
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  // Servo knob demo by Robojax.com
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  Serial.print(val);
  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 Robojax.com
  Serial.print(val);
  Serial.println();
  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