13. Servo Motors
Command and safely power a closed-loop position actuator.
Learning outcomes
- Describe a hobby servos motor, gears, sensor and controller
- Relate pulse width to requested angle
- Use Servo.h
- Provide a suitable external supply
Parts and preparation
Uno, small hobby servo, 10 k ohm potentiometer and regulated 5-6 V supply.
Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.
Libraries for this lesson
In Arduino IDE 2 open Tools → Manage Libraries…. Search by the Library Manager name and install the package by the exact author below. Similar names from other authors can use different APIs and will break the example.
| Include | Library Manager name | Author | Install note |
|---|---|---|---|
Servo.h | Servo | Arduino | Built-in with Arduino AVR Boards. No Library Manager install required. |
Closed-loop actuator
A servo compares the requested position with feedback from an internal potentiometer and drives its geared motor to reduce the error.
Control signal
Typical position pulses repeat about every 20 ms. Around 1 ms, 1.5 ms and 2 ms usually represent one end, centre and the other end, but exact limits vary.
Power
Servo current rises while moving and can be very high when stalled. Use a regulated external supply sized for the servo and always join grounds.
Wiring and safe build sequence

- Servo signal -> D9
- Servo red -> external regulated +5 V
- Servo brown/black -> external GND
- External GND -> Uno GND
- Pot wiper -> A0; outer legs -> 5 V/GND
Worked sketch
Download .ino sketch#include <Servo.h>
Servo arm;
void setup() {
arm.attach(9);
}
void loop() {
int raw = analogRead(A0);
int angle = map(raw, 0, 1023, 10, 170);
arm.write(angle);
delay(15);
}How the code works
- The 10-170 degree limits reduce the risk of driving against mechanical end stops.
- write requests a position; it does not confirm the shaft reached it.
Test and record evidence
Practical evidence checklist
Common faults and checks
- Resetting or jitter usually indicates weak power or missing common GND.
- Disconnect the mechanical load while testing limits.
Check your understanding
Q1. Why is a servo closed-loop?
Show answer
It uses internal position feedback to correct its shaft position.
Q2. Why use external power?
Show answer
Movement and stall current can exceed the Uno regulator/USB capability.