Servo Motor Control
Using a Potentiometer
Turn a knob, move a motor. Learn how analog input maps directly to physical rotation — the foundation of every robotic arm ever built.
What Are We Building?
A potentiometer is an analog knob that changes resistance as you turn it. The Arduino reads this as a value from 0 to 1023, then uses the map() function to convert that range into a servo angle from 0° to 180°. Turn the knob left, the servo turns left. Turn it right, the servo follows. Simple, powerful, and the basis for robotic arm control.
Analog Input
Read a 0–1023 value from a rotating knob using analogRead().
Value Mapping
Use the map() function to convert any range to another instantly.
Servo Control
Drive precise angular position using the built-in Servo library.
Robotics Basics
Foundation skill for robotic arms, camera gimbals, and steering systems.
What You Need
| # | Component | Qty | Role |
|---|---|---|---|
| 1 | Arduino UNO | ×1 | Reads knob value & sends angle to servo |
| 2 | Servo Motor (SG90) | ×1 | Rotates to the angle commanded by Arduino |
| 3 | 10 kΩ Potentiometer | ×1 | Analog input — produces 0–5V as you turn it |
| 4 | Jumper Wires | ×~6 | Connections between all components |
| 5 | Breadboard | ×1 | Optional prototyping platform |
⚡ Power tip: For high-torque servos (like MG995), power the servo from an external 5V supply and share the GND with Arduino. Powering from the Arduino's 5V pin can cause resets due to current draw.
Wiring It All Together
🎙 Potentiometer
⚙️ Servo Motor (SG90)
💡 Why Pin 9? Servo motors are controlled by PWM (Pulse Width Modulation) signals. On Arduino UNO, pins 3, 5, 6, 9, 10, and 11 support PWM — marked with a ~ symbol on the board.
How The Knob Controls The Motor
Potentiometer Produces Voltage
As you rotate the knob, the wiper pin outputs a voltage between 0V (fully left) and 5V (fully right), acting as a voltage divider.
Arduino Reads Analog Value
analogRead(A0) converts that voltage into an integer from 0 to 1023 using the 10-bit ADC built into the Arduino.
map() Converts the Range
The map(potValue, 0, 1023, 0, 180) function linearly scales the 0–1023 range into a 0°–180° angle. A reading of 512 becomes approximately 90°.
Servo Receives the Angle
myServo.write(angle) sends the command via PWM. The servo's internal feedback circuit holds the shaft precisely at that angle.
Loop Repeats Every 15ms
A delay(15) gives the servo time to physically reach its position before the next command is issued — preventing stuttering.
🔁 Visualizing the map() Function — Try It
Drag the slider to see the mapping live
The Program
Paste this into the Arduino IDE or Tinkercad's code editor. The Servo.h library is built into Arduino — no extra installation needed.
#include <Servo.h> // Built-in Arduino servo library // ── Objects ─────────────────────────────────── Servo myServo; // ── Pin Definitions ─────────────────────────── int potPin = A0; // Potentiometer wiper pin int servoPin = 9; // Servo signal wire (PWM pin) // ── Variables ───────────────────────────────── int potValue; int angle; void setup() { myServo.attach(servoPin); // Attach servo to pin 9 } void loop() { potValue = analogRead(potPin); // Read 0–1023 angle = map(potValue, 0, 1023, 0, 180); // Convert to 0°–180° myServo.write(angle); // Send angle to servo delay(15); // Wait for servo to reach position }
🧮 The map() Formula Explained
In our code:
map(potValue, 0, 1023, 0, 180)
// potValue=512 → angle ≈ 90°
// potValue=256 → angle ≈ 45°
// potValue=768 → angle ≈ 135°
Simulate Before You Build
The full circuit is pre-wired in Tinkercad. Hit Start Simulation, then click and drag the potentiometer knob to watch the servo rotate in real time.
Open in Tinkercad Simulator
No components needed — run the entire project in your browser. The potentiometer knob is interactive and the servo animates live.
- Start simulation and turn the potentiometer knob — observe the servo angle changing.
- Change
map(..., 0, 180)tomap(..., 0, 90)— what changes? - Add
Serial.println(angle)and open the Serial Monitor to see live angle data. - Challenge: Can you control TWO servos with ONE potentiometer moving in opposite directions?
Quick Quiz
Tap an option to check your answer instantly.
Q1. What does the map() function do in this project?
Q2. If the potentiometer reads 512, what angle will the servo be at?
Q3. Why is the servo signal wire connected to Pin 9 and not Pin 4?
Q4. What is the purpose of delay(15) at the end of the loop?
Where Is This Used?
Servo jitters or vibrates
Use an external 5V power source for the servo and share GND with the Arduino. The Arduino's onboard 5V may not supply enough current.
Compilation fails with "Servo.h not found"
Go to Sketch → Include Library → Manage Libraries and search for "Servo". In Tinkercad, it is already included automatically.
Servo moves only in one direction
Check that the potentiometer's outer pins are connected to 5V and GND (not reversed). Reversed polarity gives a very limited voltage swing.
- Add a second potentiometer + servo to control 2 axes (like a pan-tilt camera mount)
- Display the live angle on a 16×2 LCD screen
- Replace the potentiometer with an ultrasonic sensor to control angle by distance
- Chain 3+ servos to build a simple robotic arm with manual control
- Add limit switches to prevent the servo going past safe angles
Comments
Post a Comment