Source: https://mokxi.com/libraries/esp32servo
Updated: 2026-11-17

Libraries / Motion

Arduino library · Motion

# ESP32Servo: drive a hobby servo from an ESP32

Yes. You can use the ESP32Servo library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Hobby servo). The example below, "Sweep a servo with ESP32Servo", runs on the ESP32-C3-DevKitM-1.

Run the example in Mokxi See the calls

Built in on Mokxi: #include <ESP32Servo.h> and it builds, with nothing to install. Free, and no account needed.

## What ESP32Servo does

ESP32Servo is the Servo library for ESP32 boards. It keeps the Arduino Servo calls you already know, attach(), write() and read(), and adds the ESP32 set-up that tutorials show: ESP32PWM::allocateTimer() to claim hardware timers and setPeriodHertz(50) to set the 50 Hz frame a standard servo expects.

A hobby servo has three wires. The signal wire goes to a GPIO pin of your choice, the red wire to 5 V and the brown or black wire to ground. The pin you name in attach() is the only thing the library needs to know about the wiring.

The pulse widths are the same as the Arduino library’s: attach(pin) sends 544 microseconds for 0 degrees and 2400 for 180. attach(pin, 544, 2400) spells those limits out, and some servos want a different window, so those two numbers are the ones to adjust if yours stops short of its ends.

Library Manager name | ESP32Servo

Author | Kevin Harrington, John K. Bennett

Include | #include <ESP32Servo.h>
#include <ESP32PWM.h>

Boards | ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Hobby servo

## Install it

It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32.

In Mokxi it is built in: #include <ESP32Servo.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "ESP32Servo" by Kevin Harrington, John K. Bennett from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add ESP32Servo to lib_deps in PlatformIO. Mokxi follows the calls of release 3.2.1, so a sketch written here builds there too.

- ESP32-C3
- ESP32-C6
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32 DevKit
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- ESP32-S3-DevKitC-1
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32

## The calls you will use

In the order a sketch meets them.

### Servo myservo

The servo object. ESP32Servo uses the same class name as the Arduino Servo library.

### ESP32PWM::allocateTimer(0)

Claims one of the four hardware timers for servo pulses. Tutorials call it for timers 0 to 3 at the start of setup().

### myservo.setPeriodHertz(50)

Sets the frame rate of the signal. A standard analog servo wants 50 Hz.

### myservo.attach(servoPin, 544, 2400)

Starts sending pulses on that pin, with the pulse widths in microseconds for 0 and 180 degrees. Leave the two widths out to use the defaults.

### myservo.write(90)

Moves the servo to an angle from 0 to 180 degrees. A value of 500 or more is read as a pulse width in microseconds instead.

### myservo.read()

The angle last written, not a measurement of where the horn really is.

### myservo.attached()

True once attach() has given the servo a pin, which is handy to check before a first write.

## Example: Sweep a servo with ESP32Servo

On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.

sketch.ino · ESP32-C3-DevKitM-1

### Wiring

- ESP32-C3-DevKitM-1 2 to Hobby servo PWM
- ESP32-C3-DevKitM-1 5V to Hobby servo VCC
- ESP32-C3-DevKitM-1 GND to Hobby servo GND

Open it in the editor, wired and ready to run

## Common errors and how to fix them

### The servo twitches and resets the board

A servo can draw an amp or more when it starts moving, more than a USB port gives a board to spare. Power it from its own 5 V supply and join that supply’s ground to the board’s ground, with only the signal wire going to the GPIO pin.

### The servo does not reach its ends, or buzzes at them

Its pulse window is not 544 to 2400 microseconds. Change the two numbers in attach(pin, min, max) a little at a time until it travels fully without humming at either end.

### Nothing moves on the pin you chose

Check the pin is a GPIO your board can use for output. On the classic ESP32, pins 34 to 39 are input only, and some other pins are tied to the flash or to boot, so pick a free one such as 13, 18 or 19.

### Servo.h works on an Uno but not on the ESP32 here

Include ESP32Servo.h on the ESP32 family instead of Servo.h, and keep one of the two in a sketch, not both. The class and its calls stay the same, so the rest of an Arduino servo sketch needs no change.

## Keep going

### Parts

- Hobby servo

### More motion libraries

- Servo
- Stepper
- AccelStepper
- L298N
- Adafruit PWM Servo Driver Library
- Adafruit Motor Shield V2 Library
- Adafruit Motor Shield library (AFMotor, v1 shield and L293D clones)
- TMCStepper
- GyverStepper
- VarSpeedServo

Every built-in library · Open the editor

## Try ESP32Servo without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
