Arduino - Servo Motor controlled by Potentiometer

In a previous tutorial , We have learned how a potentiometer triggers a servo motor. In this tutorial, We are going to learn how to rotate a servo motor according to the potentiometer's output value

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Servo Motor
1×Potentiometer
1×Breadboard
6×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.
Or you can order electronic components from utsource.net

About Servo Motor and Potentiometer

If you do not know about servo motor and potentiometer (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

Arduino Servo Motor Potentiometer Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program

  • Reads the value of the potentiometer (value between 0 and 1023)
int analogValue = analogRead(A0);

  • Scales it to angle (value between 0 and 180)
int angle = map(analogValue, 0, 1023, 0, 180);

  • Sets the servo position according to the angle
myServo.write(angle);

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-servo-motor-controlled-by-potentiometer */ #include <Servo.h> Servo myServo; // create servo object to control a servo void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); myServo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { // reads the value of the potentiometer (value between 0 and 1023) int analogValue = analogRead(A0); // scales it to use it with the servo (value between 0 and 180) int angle = map(analogValue, 0, 1023, 0, 180); // sets the servo position according to the scaled value myServo.write(angle); // print out the value Serial.print("Analog: "); Serial.print(analogValue); Serial.print(", Angle: "); Serial.println(angle); delay(100); }

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Arduino IDE Upload Code
  • Open Serial Monitor
  • Rotate the potentiometer
  • See the servo motor's rotation
  • See the result on Serial Monitor
  • COM6
    Send
    Analog: 0, Angle: 0 Analog: 85, Angle: 14 Analog: 201, Angle: 35 Analog: 286, Angle: 50 Analog: 370, Angle: 65 Analog: 444, Angle: 78 Analog: 521, Angle: 91 Analog: 608, Angle: 106 Analog: 690, Angle: 121 Analog: 793, Angle: 139 Analog: 907, Angle: 159 Analog: 1023, Angle: 180 Analog: 1023, Angle: 180
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Code Explanation

Read the line-by-line explanation in comment lines of source code!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Alternative Servo Motors

The above code also works with the following servo motors:

Alternative Potentiometers

The above code also works with the following rotary angle sensors:

Servo Motor on Commercial Products

  • Robotic Arm Kit for Arduino
  • Otto DIY Robot Builder Kit

Potentiometer on Commercial Products

  • Digital Oscilloscope

Comments

If you have any questions or suggestions, please comment on our YouTube channel

If this tutorial is useful for you, please give us motivation to make more tutorials
We are available for hire. See how to hire us to code for you