Servo library does not work on ESP32: use the ESP32Servo library

The standard Arduino Servo library was written for AVR boards. On an ESP32 use the ESP32Servo library.

What the message means

The ESP32 makes PWM with its own LEDC hardware, so the timer tricks of the AVR library do not apply.

Fix it in this order

  1. Install ESP32Servo from the Library Manager and include ESP32Servo.h.
  2. Call attach(pin, minUs, maxUs) with the pulse range of your servo.
  3. Use an output-capable GPIO. GPIO 34 to 39 are input only.
  4. Power the servo from its own 5 V supply and join the grounds.
  5. Allocate timers before attaching several servos, if the library asks you to.

Why it happens

The Arduino Servo library is written for AVR boards and may fail to compile or work on the ESP32. The ESP32Servo library gives the same style of calls using the ESP32 PWM hardware.

Install ESP32Servo from the Library Manager, replace the include with ESP32Servo.h, and attach the servo to an output-capable pin.

#include <ESP32Servo.h>
Servo s;
void setup() { s.attach(18); }   // choose an output-capable pin
void loop() { s.write(90); }

Example sketch: change pins and values for your own wiring. It has not been bench-tested on your hardware.

If you see this, do this

You seeDo this
Fails to compileWrong library.
Compiles, no movementPin or power. See the servo page.

servo does not move on ESP32, input-only pins.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.

Why this happens

The standard Servo library for AVR boards uses hardware timers and interrupts that the ESP32 does not have in the same form. The ESP32 generates PWM with its LEDC peripheral, which has a fixed number of channels. ESP32Servo wraps that peripheral and keeps the familiar attach and write calls.

A hobby servo expects a pulse every 20 ms (50 Hz), with a width of roughly 1000 to 2000 microseconds, though many servos accept a wider span. A pulse of 1500 microseconds is 7.5 % duty at 50 Hz and normally means the centre position.

How to confirm the cause

  • Check whether the failure is at compile time or at run time. A compile error about timers or a missing header points to the wrong library; no movement points elsewhere.
  • Look at the pin number. GPIO 34 to 39 are input-only on the classic ESP32, and GPIO 6 to 11 are connected to the flash and must be avoided.
  • Open the serial monitor at boot. A brownout message means the servo draws more than the supply gives.
  • Move the signal wire to a different output-capable pin such as 18 and try again.
  • Test the servo from a servo tester or another board to confirm it is not faulty.

Worked example

A sweep that uses the ESP32 library and sets the pulse range explicitly.

#include <ESP32Servo.h>
Servo s;
void setup() {
  s.setPeriodHertz(50);
  s.attach(18, 500, 2400);   // pin, min us, max us
}
void loop() {
  for (int a = 0; a <= 180; a += 10) { s.write(a); delay(200); }
  for (int a = 180; a >= 0; a -= 10) { s.write(a); delay(200); }
}

Common mistakes

  • Powering the servo from the 3.3 V pin.
  • Not joining the grounds of the servo supply and the ESP32.
  • Writing to the servo before attach().
  • Using analogWrite or other PWM on the same channels, which can clash with the servo.
  • Attaching more servos than the LEDC channels allow.

If this did not fix it

  • Supply sag. Add a capacitor across the servo supply and use a supply that can deliver the servo’s stall current.
  • A servo that needs a wider or narrower pulse range than the one you set.
  • A pin that is a strapping pin and is held at a level during boot.