A servo motor is a small actuator that rotates its shaft to a specific position, usually somewhere between 0 and 180 degrees, instead of spinning continuously like a regular motor. That makes it useful whenever you need to move something to an exact angle, like a camera mount, a robot arm, or a gate latch. This tutorial covers wiring a servo to an ESP32 and controlling its angle with MicroPython.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 MicroPython Servo Motor
What you need
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- ESP32 Uno-form board
- ESP32 S3 Uno-form board
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Servo Motor
- Jumper Wires
- Breadboard
- Screw Terminal Expansion Board for ESP32
- Breakout Expansion Board for ESP32
- Power Splitter for ESP32
- DIYables ESP32 Starter Kit (ESP32 included)
- DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The servo has three wires. The red wire goes to VCC, which needs 5 volts, the black or brown wire goes to GND, and the yellow or orange wire is the signal wire that receives the PWM control pulses from an ESP32 pin. In the example code this signal wire connects to GPIO26.
Do not power the servo from the ESP32's VBUS pin, since this can damage the board. Instead use a separate power supply for the servo, and make sure that supply's GND is connected to the ESP32's GND so both share a common reference. If you need to control several servos at once, a PCA9685 16-channel PWM driver module saves pins and simplifies the wiring.
How it works
The ESP32 sends a PWM signal on the signal pin, and the width of each pulse tells the servo what angle to move its shaft to. The servo motor itself contains the small motor, gearbox, and control circuitry needed to read that signal and hold the shaft at the commanded position.
Code
The MicroPython code uses the DIYables_MicroPython_Servo library, creating a Servo object tied to the signal pin, here GPIO26. The main loop then calls move_to_angle() to sweep the servo between 0, 90, and 180 degrees, pausing two seconds at each position with utime.sleep().
There's also a non-blocking version that uses utime to update the servo's position gradually over time, so the servo moves smoothly between angles without freezing the rest of your program while it waits.
esp32-micropython-servo-motor.py
"""
This ESP32 MicroPython code was developed by newbiely.com
This ESP32 MicroPython code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-servo-motor
"""
from DIYables_MicroPython_Servo import Servo
import utime
# Create a Servo object
servo = Servo(26) # The ESP32 pin GPIO26 connected to the servo motor
while True:
# Move servo to different angles
servo.move_to_angle(0)
utime.sleep(2)
servo.move_to_angle(90)
utime.sleep(2)
servo.move_to_angle(180)
utime.sleep(2)
Things that go wrong
If the servo doesn't move or jitters erratically, check that its GND is tied to the ESP32's GND even when using a separate power supply, since a missing common ground is a common cause of unpredictable behavior. Also confirm the servo is getting a proper 5 volt supply rather than being powered from the ESP32 itself.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-servo-motor

Top comments (0)