DEV Community

Cover image for ESP32 MicroPython Servo Motor
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

ESP32 MicroPython Servo Motor

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

Wiring

ESP32 MicroPython Servo Motor wiring diagram

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)
Enter fullscreen mode Exit fullscreen mode

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)