How to wire a servo to an ESP32
To wire a servo to an ESP32, connect GND to GND, VCC to VIN (5 V from USB) and PWM to GPIO 13 (D13). The servo takes 5 V on its power wire, while the signal wire comes from a 3.3 V pin, which an SG90 reads as high without a level shifter. The sketch below uses the ESP32Servo (ESP32Servo.h) library. It sweeps the horn from 0 to 180 degrees and back, printing a line at each end.
- 3.3 V logic
- Library: ESP32Servo.h
- 3 wires
Servo to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| Servo pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| Brown (GND) | GND | blue | The signal is measured against this, so it must be shared with the board. |
| Red (V+) | VIN (5 V from USB) | red | An SG90 wants 4.8 to 6 V. At 3.3 V it barely moves, so it takes the 5 V rail, not 3V3. |
| Orange (signal) | GPIO 13 (D13) | amber | Any GPIO with an output driver; the ESP32 makes the pulse in its LEDC hardware. |
Power and logic levels for servo and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The servo takes 5 V from VIN (5 V from USB). That is the ESP32’s 5 V rail, so the part shares the current the board can supply.
This pair
The servo runs from VIN (5 V from USB) while its signal comes from a 3.3 V pin. That works: an SG90 reads anything above about 2 V as high, so no level shifter is needed. On a real bench, a servo under load can pull more current than USB gives; then power it from its own 5 V supply and join the grounds.
Pins to leave alone
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
The working code for the ESP32
Library: ESP32Servo (ESP32Servo.h). This is the sketch the circuit above runs.
#include <ESP32Servo.h>
const int SERVO_PIN = 13; // signal, GPIO 13 (D13)
Servo servo;
void setup() {
Serial.begin(115200);
servo.attach(SERVO_PIN); // 544 to 2400 us, 50 times a second
Serial.println("Sweeping");
}
void loop() {
for (int angle = 0; angle <= 180; angle += 2) {
servo.write(angle);
delay(15);
}
Serial.println("at 180");
for (int angle = 180; angle >= 0; angle -= 2) {
servo.write(angle);
delay(15);
}
Serial.println("back at 0");
}
What you should see
The servo horn turns from 0 to 180 degrees in about 1.4 seconds and then back. The serial monitor, at 115200 baud, prints Sweeping once, then at 180 at the top of each sweep and back at 0 at the bottom.
A servo that buzzes or does not move is almost always a missing common ground or a supply that sags under load.
The serial monitor prints this, recorded from the circuit above running for two and a half seconds:
Sweeping
at 180Common mistakes wiring a servo to an ESP32
Powering the servo from 3V3
The ESP32’s 3.3 V regulator is sized for the chip, not a motor, and an SG90 at 3.3 V jitters or stalls. The red wire goes to VIN (5 V from USB).
Forgetting the common ground
With the servo on a separate supply and only the signal wire to the board, the servo has nothing to measure the pulse against and twitches at random. Connect the supply’s ground to the board’s GND.
Putting the servo signal on an input-only pin
GPIO 34, 35, 36 and 39 on the ESP32 have no output driver at all. pinMode() to OUTPUT on one of them does nothing and the line never moves, which looks exactly like a dead part. GPIO 4 (D4) is a safe choice; so are GPIO 13, 14, 16 to 19, 23, 25 to 27 and 32 to 33.
Build your own servo project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.