Other Arduino Codes and Videos by Robojax

Control Servo with potentiometer using Arduino

دروس آردوینو به فارسی

This is the Arduino code for Control Servo with potentiometer

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

Resources for this sketch

 Controlling a servo position using a potentiometer (variable resistor)
 by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

 modified on 8 Nov 2013
 by Scott Fitzgerald

 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.h>

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 = 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
  delay(200);                           // waits for the servo to get there

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