Blog
ESP32 PWM and Servo: LEDC, Frequency, Control
Servo control on the ESP32 is PWM with a pulse in the right place: 16 LEDC channels, any GPIO, 50Hz, 1-2ms.
How to Connect Servo to ESP32?
Three wires: signal to any GPIO (13 works), power to 5V, ground to ground — plus one rule: servo ground and ESP32 ground must be common. A 9g SG90 runs off the board’s 5V pin; anything bigger needs an external supply. I stalled a project with a MG996R on the 3.3V rail — brown-outs and a twitching servo; the power FAQ covers it.
How to Control Servo with ESP32?
Use the ESP32Servo library; the classic Arduino Servo.h doesn’t drive the ESP32’s timers properly. attach(13), then write(90), done — it generates 50Hz with 500-2500µs pulses. Raw LEDC: ledcSetup(0, 50, 16), ledcAttachPin(13, 0), ledcWrite(0, duty). Search “how to control a servo with an esp32” and every tutorial is one of these two. First flash? Getting-started FAQ.
How Many Channels PWM ESP32?
16 LEDC channels on the classic ESP32, driven by 8 timers — 4 high-speed, 4 low-speed. Channels sharing a timer must share frequency, so you get 16 duty slots but only 8 independent frequencies — plenty for servos. “How many pwm esp32” in short: 16 channels, 8 timers; newer chips trim that — check your family’s datasheet.
Which ESP32 Pins Are PWM?
All of them except input-only GPIO34-39. PWM isn’t locked to fixed pins — LEDC routes through the GPIO matrix, so any output-capable pin carries PWM and channels move in software. Asking which C3 pins can control a servo gets the same answer: any GPIO. A “dead” PWM pin is usually the 34-39 wall or a wiring slip.
How to Measure ESP32 PWM with Oscilloscope?
Probe the signal pin, ground clip to GND, 1V/div, and for servo PWM set 10ms/div — a 50Hz train of 1-2ms pulses. Period with cursors, frequency is 1/T; on-time gives duty. A $30 USB scope is enough. I chased a jittering servo with a bench scope for an hour before noticing the supply was sagging, not the pulse — measure the 5V rail too.
What frequency should I use for servos, LEDs, and motors?
Servos: 50Hz with 500-2500µs pulses; some digital servos prefer 333Hz — check the servo’s spec sheet; it varies by model. LEDs: 1-5kHz for dimming. Motors and coils: 20-25kHz to escape audible whine. Under a few MHz, frequency doesn’t cost duty resolution. Official docs are vague on per-timer ceilings; the ranges above are what I’ve used.
Why does my servo jitter or sit dead?
Power first, library second, pin third — 90% of cases. Jitter under load means a supply sagging below 4.8V; a dead servo usually means the wrong library; a pin that never moves is often 34-39. Brown-out resets make servos twitch in a loop — chip restarts, servo re-attaches. Bench-fine, project-broken? Suspect ground loops. Debugging FAQ has the checklist.
Can the ESP32 tell where a servo is?
No — analog hobby servos don’t report position; read() returns the last commanded angle, so a pushed arm is invisible to the chip. Searching “how to read servo position esp32” mostly shows that command echo, which trips people up. Real feedback needs a feedback servo or an encoder. Saving is simpler: Preferences stores the angle in NVS and setup() re-applies it — the whole “how to save servo position esp32” answer.
How to control servos from a web page?
WiFi.softAP(), AsyncWebServer, one endpoint — the “how to use ap webserver esp32 servo” pattern: serve a slider, post to /set?angle=90, write to the servo. WebSocket for live updates. The same code runs in the simulator first.
Do I need extra hardware for several servos?
For two or three small servos, no — 16 channels cover it; add a 470-1000µF cap near the servos for spikes. High-torque or many servos: external 5V BEC or PSU with common ground; a shield is convenience, not a requirement.
Drive a Servo From a Pot — Right Now, In Your Browser
PWM makes sense the second you watch a knob move a horn. Both of these run free in our simulator:
- Servo + potentiometer on ESP32 — ADC in, PWM out, one line of feedback:
ESP32 Servo + Pot control
Pot: 512 Angle: 90 deg
Pot: 1023 Angle: 179 deg
Turn the virtual pot and both numbers track it — raw ADC counts on the left, the angle the LEDC peripheral is generating pulses for on the right.
- Stepper via A4988 driver — for when you need full steps, not just an angle. It announces itself and spins:
A4988 stepper running
Two motors, two drive styles, same lesson: the ESP32 generates the pulses, the driver handles the current.