Arduino Servo Motor Control: Wiring, Code, Power Supply and ESP32
In short: a servo is a motor with a DC motor, gearbox, potentiometer and control circuit inside that turns to a commanded angle and holds it. An Arduino controls a servo over a single signal wire with a 50 Hz pulse: about 1 ms means 0°, 1.5 ms the centre, and 2 ms 180°. With Arduino’s built-in Servo.h library it takes just a few lines of code. The thing to really watch is the power supply: feed servos from a separate 5–6 V source, not the Arduino’s 5V pin.
This guide covers how a servo works, the signal, wiring, Arduino and ESP32 code, and the most common power mistakes.
In this article 12
How does a servo work?
A standard RC servo has four main parts:
- A small DC motor: produces the movement.
- A gearbox: reduces speed and increases torque.
- A potentiometer: attached to the output shaft, it measures the shaft angle.
- A control circuit: compares the target angle in the signal with the angle the potentiometer measures, and drives the motor in the right direction until the difference is zero.
Thanks to this closed loop, the servo tries to stay at the target angle even under load. A servo is a small position control system in itself.
The servo signal: pulse width
A servo listens for a pulse every 20 ms (50 Hz). The width of the pulse sets the angle:
| Pulse width | Typical position |
|---|---|
| ~1000 µs (1 ms) | 0° (one end) |
| ~1500 µs (1.5 ms) | 90° (centre) |
| ~2000 µs (2 ms) | 180° (other end) |
These values vary by manufacturer. Some servos turn a full 180° over 500–2500 µs; others move only 90–120° over 1000–2000 µs. If the servo strains or buzzes at the ends, narrow the limits. For PWM and other RC protocols, see RC protocols and failsafe.
Servo types
| Type | Characteristics | Uses |
|---|---|---|
| Micro servo (e.g. SG90) | ~9 g, plastic gears, ~1.5–2 kg·cm | Learning projects, light mechanisms |
| Metal-gear micro (e.g. MG90S) | ~13 g, more durable | Small robot arms, RC plane surfaces |
| Standard servo (e.g. MG996R) | ~55 g, ~10 kg·cm | Robot arms, RC car steering |
| High-voltage (HV) digital servo | 7.4–8.4 V, high torque and speed | Racing RC cars, large models |
| Continuous rotation (360°) servo | Controls speed and direction instead of angle | Small wheeled robots |
On a continuous rotation servo, 1500 µs means stop, smaller values turn one way and larger values the other; there is no position feedback.
Wiring
Servo leads have three wires; colours vary by brand but the order is usually the same:
| Wire colour | Function | Connect to |
|---|---|---|
| Brown / black | GND | Supply negative and Arduino GND |
| Red | + Supply (4.8–6 V) | External 5–6 V source |
| Orange / yellow / white | Signal | Arduino D9 |
You can use the Arduino’s 5V pin to briefly test a single unloaded micro servo, but use a separate supply in real projects. Don’t forget the common ground, or the servo won’t recognise the signal and will move randomly.
First code: sweeping a servo
#include <Servo.h>
Servo servo;
void setup() {
servo.attach(9, 1000, 2000); // pin, min µs, max µs
}
void loop() {
for (int angle = 0; angle <= 180; angle++) { servo.write(angle); delay(15); }
for (int angle = 180; angle >= 0; angle--) { servo.write(angle); delay(15); }
}servo.write(angle) takes an angle from 0 to 180. For finer control, use servo.writeMicroseconds(1500) directly.
Controlling a servo with a potentiometer
#include <Servo.h>
Servo servo;
const int POT = A0;
void setup() {
servo.attach(9, 1000, 2000);
}
void loop() {
int value = analogRead(POT); // 0-1023
int angle = map(value, 0, 1023, 0, 180);
servo.write(angle);
delay(15);
}Connect the potentiometer’s middle pin to A0 and the outer pins to 5V and GND. This is the basis of manual control for a robot arm or camera mount.
Multiple servos
Servo.h supports up to 12 servos on an Arduino Uno, but because it uses Timer1, analogWrite stops working on pins 9 and 10. For many servos, the cleanest solution is a PCA9685 16-channel servo driver controlled over I²C. The Arduino sends commands over two wires and the board generates the pulses.
Power supply: the most common mistake
A micro servo draws a few tens of milliamps idle, a few hundred while moving, and over 1 A when stalled. Standard-size servos reach 2–3 A. The Arduino’s 5V pin and USB port can’t supply this; the voltage sags and you see:
- The Arduino resets.
- The servo jitters or moves randomly.
- The serial connection drops.
For proper power:
- Feed the servos from a separate 5–6 V source: a 4×AA holder, a strong 5 V adapter, or a 2S LiPo with a BEC/UBEC.
- Calculate total current: number of servos × stall current. They may all be loaded at once.
- Add a 470–1000 µF electrolytic capacitor on the servo supply to absorb current spikes.
- Connect all grounds together.
For BEC and UBEC choice, current calculation and power distribution, see BEC and UBEC explained.
Servo control with an ESP32
Arduino’s Servo.h doesn’t work directly on the ESP32; use the ESP32Servo library instead. Usage is almost identical:
#include <ESP32Servo.h>
Servo servo;
void setup() {
servo.setPeriodHertz(50);
servo.attach(18, 1000, 2000);
}
void loop() {
servo.write(0); delay(1000);
servo.write(90); delay(1000);
servo.write(180); delay(1000);
}The ESP32 outputs a 3.3 V signal. Most servos accept it without problems; for those that don’t, use a logic level shifter. You can also control a servo from a web page over Wi-Fi with an ESP32. For ESP32 basics, see what is ESP32.
Servo, DC motor or stepper?
| Need | Right choice |
|---|---|
| Move to a set angle and hold it (0–180°) | Servo |
| Continuous rotation, speed control | DC motor + driver |
| Precise position, many turns, repeatability | Stepper motor |
| High speed and efficiency | Brushless motor + ESC |
To control the speed of a continuously rotating motor, see DC motor speed control; for steppers, see the stepper motor guide.
Frequently asked questions
Why does my servo jitter?
The most common causes are a weak power supply, a missing common ground, a long thin signal wire, or pushing the servo against its mechanical limit. First move the power to a separate supply and join the grounds; if it continues, narrow the min/max µs values.
How much can an SG90 lift?
An SG90 produces about 1.5–1.8 kg·cm at 4.8 V. That means about 1.5 kg of force 1 cm from the shaft, but only around 300 g at 5 cm. The load it can carry drops quickly as the arm gets longer.
Why won’t my servo turn 180°?
Servo.h uses 544–2400 µs by default, but not every servo supports that range. Pass the min and max values from your servo’s datasheet to attach to set its range. Some servos mechanically turn only 90–120°.
Digital vs analog servo?
Digital servos run their internal control loop at a much higher frequency. This gives faster response, more holding torque and more precise positioning, but they draw more current. The signal from the Arduino is the same for both.







