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
Or you can buy the following kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .
Additionally, some links direct to products from our own brand, DIYables .
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

This image is created 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

- Open Serial Monitor
- Rotate the potentiometer
- See the servo motor's rotation
- See the result on Serial Monitor
COM6
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
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.