Arduino Servo Motor Control Using a Potentiometer
In this tutorial, we will explore how to control a servo motor using a potentiometer with an Arduino. The goal is to create a simple circuit where the position of the servo is adjusted based on the rotation of the potentiometer. By the end of this project, you will have a working model that smoothly varies the servo angle from 0 to 180 degrees as you turn the potentiometer. This is a great way to understand the basics of analog input and servo control in Arduino.
To achieve this, we will use an Arduino board, a servo motor, and a potentiometer. The potentiometer acts as a variable resistor, providing an analog input to the Arduino, which will then determine the position of the servo motor accordingly. This project is not only fun but also educational, as it demonstrates key concepts in electronics and programming. For a more in-depth explanation, be sure to check out the video (in video at 00:00).
Hardware Explained
In this project, the main components are the Arduino board, the servo motor, and the potentiometer. The Arduino is the brain of the operation, processing inputs and controlling outputs. The servo motor is what will physically move based on the commands it receives from the Arduino. The potentiometer can be 1k ohm to 1M ohm allows users to provide a variable input that translates into servo movement.
The servo motor operates by receiving a PWM (Pulse Width Modulation) signal that indicates the desired position. The potentiometer, when turned, changes its resistance, which is read by the Arduino as an analog voltage. Servo Motor operate with 5V. Dote not work with 3.3V. This voltage is then mapped to the range of motion of the servo motor, allowing for smooth movement from 0 degrees to 180 degrees.
Datasheet Details for SG90 Servo Motor
| Manufacturer | Parallax |
|---|---|
| Part number | SG90 |
| Logic/IO voltage | 5 V |
| Supply voltage | 4.8–6 V |
| Output current (per channel) | ~500 mA |
| Peak current (per channel) | 1 A |
| PWM frequency guidance | 50 Hz |
| Input logic thresholds | 0.5 V (low), 2.5 V (high) |
| Voltage drop / RDS(on) / saturation | 0.5 V at 500 mA |
| Thermal limits | 0°C to 60°C |
| Package | Standard 9g Servo |
| Notes / variants | Commonly used in RC applications |
- Ensure the servo does not exceed its maximum current rating.
- Use a heat sink if the servo operates continuously at high loads.
- Connect the potentiometer to an analog pin for accurate readings.
- Be cautious of the PWM signal timing for the servo control.
- Check the servo's range of motion to avoid physical obstructions.
Wiring Instructions


For this setup, you will need to connect the components as follows:
Connect the potentiometer’s middle pin (wiper) to the Arduino's analog pin A0. The other two pins of the potentiometer should be connected to 5V and GND on the Arduino. For the servo motor, connect the red wire to 5V, the black wire to GND, and the yellow or white wire to digital pin 9 on the Arduino. This arrangement allows the potentiometer to provide an analog input to the Arduino, which will control the servo motor’s position based on that input.
Make sure to verify all connections and ensure that the servo is not obstructed in its range of motion. If using a different Arduino model, confirm that the pin assignments for the analog input and servo output are compatible.
Code Examples & Walkthrough
Below is an excerpt of the code that initializes the servo and reads the potentiometer value:
#include
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
In this code, we include the Servo library and create a servo object called myservo. The servo is attached to pin 9, which allows us to control it using this pin.
void loop() {
int sensorValue = analogRead(A0); // read the potentiometer
pos = map(sensorValue, 0, 1023, 0, 180); // map to servo position
myservo.write(pos); // move servo to position
delay(15); // waits 15ms for the servo to reach the position
}
In the loop function, we read the potentiometer value from pin A0 and map it from its range (0-1023) to the servo's range (0-180 degrees). The myservo.write(pos) command then moves the servo to the desired position based on the potentiometer's rotation.
Demonstration / What to Expect
When you run this program, you should see the servo motor smoothly move from 0 to 180 degrees as you turn the potentiometer. If the servo does not move as expected, check the wiring and ensure the potentiometer is functioning correctly. Additionally, ensure the Arduino is properly powered and the code is uploaded without errors. You can expect to see the servo reflecting the potentiometer's position in real-time (in video at 02:00).
Video Timestamps
- 00:00 - Introduction to the project
- 01:30 - Hardware setup explanation
- 03:45 - Wiring instructions
- 05:00 - Code walkthrough
- 07:30 - Demonstration of the completed project
Resources & references
No resources yet.
Files📁
No files available.