Servo motors: position control with pulses
A hobby servo has a small DC motor, a gearbox, a potentiometer and a control chip in one case. The servo drives its own motor. Your code only sends a pulse every 20 ms (50 Hz). The width of that pulse tells the servo which angle to hold. The servo compares the pulse with its potentiometer and drives the motor until the two agree. This is a closed-loop system.

Photo: Suyash Dwivedi, CC BY-SA 4.0
Top: two 20 ms frames. Bottom: a magnified view of the first 3 ms. The usable range is only about 0.5–2.5 ms, so for more than 85% of each frame, the servo only waits.
How the pulse becomes an angle
The pulse width sets the angle
The servo only measures the time that the pulse is HIGH. 1.5 ms is the center, and the nominal range is 1.0–2.0 ms. Many hobby servos move through their full ~180° with about 0.5–2.5 ms. Each model is slightly different. To find the real limits, do tests carefully. If the servo makes a buzz against its end stop, stop.
Frame rate
The standard frame is 20 ms (50 Hz). Analog servos need about 50 Hz. Digital servos often accept up to 300+ Hz for a faster response. If you are not sure, use 50 Hz. The frame period must always be longer than the longest pulse.
Continuous rotation
A “360°” or continuous servo has no position feedback, so the same pulse sets the speed and direction. 1.5 ms = stop, shorter = one direction, longer = the opposite direction. Most of these servos have a small trim pot to adjust the stop point.
Wiring & power
Most servo problems are power problems. A servo that moves uses hundreds of mA. A stalled servo uses much more current. These surges decrease the supply voltage and reset the ESP32 (a “brownout”), so give the servos their own 5–6 V supply.
Servo wire colors
| Wire | Function | Connect to |
|---|---|---|
| Brown / black | Ground | Servo supply − and ESP32 GND |
| Red (middle) | Power (4.8–6 V) | Servo supply + |
| Orange / yellow / white | Signal | Any output-capable GPIO |
General rules
- One small SG90 with no load can operate from the 5 V (VIN/USB) pin of the DevKit for a quick test. For more servos or a load, use a separate supply.
- Do not supply a servo from the 3V3 pin.
- Always connect the supply GND to the ESP32 GND. If you do not, the signal has no reference.
- A 3.3 V signal operates almost all 5 V servos. If a servo is not reliable, add a 74AHCT125 level shifter.
- For more than ~4 servos, use a PCA9685 16-channel I²C PWM board. This board has its own power terminal.
Power supply size
- Typical draw
- 0.5 A
- Maximum (all stalled)
- 1.4 A
- Recommended supply
- 5–6 V, at least 2 A
- Bulk capacitor
- 470 µF, 10 V or higher
The estimate sets the supply to 1.5× the typical current. It is also a minimum of 70% of the maximum current, when all servos are stalled. The reason is that each time a servo starts to move, it uses stall current for a short time.
Code
With the ESP32Servo library
Arduino IDE → Library Manager → search for ESP32Servo (by Kevin Harrington / madhephaestus). With Arduino-ESP32 3.x, use version 3.x or newer.
servo_sweep.ino
#include <ESP32Servo.h>
const int SERVO_PIN = 18;
Servo myServo;
void setup() {
myServo.setPeriodHertz(50); // standard 20 ms frame
myServo.attach(SERVO_PIN, 500, 2400); // min/max pulse in µs for 0° and 180°
}
void loop() {
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle); // the library changes the angle → pulse width
delay(10);
}
for (int angle = 180; angle >= 0; angle--) {
myServo.write(angle);
delay(10);
}
}
Without a library (raw LEDC)
This code shows exactly what the library does. It makes a 50 Hz PWM signal. The high time of the signal is equal to the pulse width.
servo_ledc.ino
const int SERVO_PIN = 18;
const int FREQ = 50; // Hz → 20 000 µs period
const int BITS = 14; // 0..16383 duty steps (≈ 1.2 µs each)
void writeMicros(int us) {
uint32_t duty = (uint32_t)us * ((1 << BITS) - 1) / 20000;
ledcWrite(SERVO_PIN, duty);
}
void setup() {
ledcAttach(SERVO_PIN, FREQ, BITS);
writeMicros(1500); // center
}
void loop() {
writeMicros(1000); delay(1000);
writeMicros(2000); delay(1000);
}
Troubleshooting
ESP32 resets when the servo moves
This is a brownout. The start-up current of the servo decreases the supply voltage. Use a separate supply. Add a 470–1000 µF capacitor near the servo. Use short, thick power wires.
The servo jitters or twitches
First, examine the common ground. Then look for a supply that is noisy or weak. Also look for long signal wires adjacent to motor wires, or pulses
that change in each loop. Call write() only when the value changes.
The servo makes a buzz and becomes hot
The servo pushes against its end stop or against a load that it cannot move. This is a stall. Make the pulse range smaller in
attach(), or use a stronger servo. Long stalls damage plastic gears and burn the motors.
Resources
- ESP32Servo library (GitHub)The servo library used above, with examples.
- Adafruit: 16-channel PCA9685 servo driverControl up to 16 servos over I²C, with its own power input.
- Wokwi ESP32 simulatorTry servo code in the browser before you connect wires.
- PWM & LEDsThe same LEDC peripheral, with an explanation of duty cycle and frequency.
- Power suppliesHow to select supplies, UBECs and batteries, and the common-ground rule.