MicroPython servo sweep on GPIO 15, on an ESP32-DevKitC V4
MicroPython on an ESP32: 50 Hz PWM on GPIO 15 sweeps an SG90 servo from 0 to 180 degrees and back, a degree every 15 ms. The whole build, an ESP32-DevKitC V4 and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $12.50. On Mokxi they're free.
What you'll learn
- How a hobby servo reads a pulse width
- Making 50 Hz PWM with machine.PWM
- Turning degrees into a duty value
- Powering a servo from 5 V and sharing ground
- Difficulty: Beginner
- Time: About 15 minutes
- Board: ESP32-DevKitC V4
- Parts: 3
How it works
A hobby servo turns to an angle set by the width of a pulse it receives every 20 milliseconds, which is 50 Hz. A 0.5 ms pulse means 0 degrees and a 2.5 ms pulse means 180 degrees. GPIO 15 sends that signal, and the servo is powered from 5 V.
The loop moves the servo one degree at a time from 0 up to 180, waiting 15 milliseconds per degree. It prints at 180, then sweeps back down and prints at 0.
Parts list
3 parts, plus the jumper wires. They retail for about $12.50; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 1 × Hobby servoIn Halloween Prop Kit, Christmas Lights Kit, Starter Projects Kit.
- 1 × Power, 5 V
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Hobby servo pin PWM | ESP32-DevKitC V4 pin 15 |
| Hobby servo pin GND | Ground |
| Hobby servo pin VCC | 5 V |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Servo sweep: the servo on GPIO 15 turns from 0 to 180 degrees and
# back, a degree every 15 ms, driven by 50 Hz PWM.
from machine import Pin, PWM
import time
servo = PWM(Pin(15), freq=50)
def angle(deg):
# 50 Hz is 20 ms a period: 0.5 ms (duty 26) is 0 degrees and
# 2.5 ms (duty 128) is 180 degrees
servo.duty(26 + deg * 102 // 180)
while True:
for deg in range(0, 181):
angle(deg)
time.sleep_ms(15)
print("at 180")
for deg in range(180, -1, -1):
angle(deg)
time.sleep_ms(15)
print("at 0")
Code walkthrough
The key functions of the sketch, and what each one does.
angle main.py
Turns an angle in degrees into a PWM duty. At 50 Hz a period is 20 ms, so 0.5 ms is duty 26 and 2.5 ms is duty 128. The line servo.duty(26 + deg * 102 // 180) spreads the 102 steps between those over 180 degrees.
def angle(deg):
# 50 Hz is 20 ms a period: 0.5 ms (duty 26) is 0 degrees and
# 2.5 ms (duty 128) is 180 degrees
servo.duty(26 + deg * 102 // 180)Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The servo jitters or does not move This project
Check that the servo's PWM wire goes to GPIO 15, it is powered from 5 V, and its ground connects to the board's ground. - The sweep is too fast or slow This project
Change thetime.sleep_ms(15)pause. It sets how long the servo gets per degree. - The servo jitters, twitches or resets the board when it moves. Hobby servo
Power it from its own 5 V supply, not the Arduino 5 V pin, and join the grounds. A hobby servo can pull several hundred milliamps. Add a 100 uF to 470 uF capacitor across its supply. - The servo hums at the end of its travel or will not reach full angle. Hobby servo
Stay inside the pulse range, about 1000 to 2000 us for most hobby servos. Pushing past the end stop makes it strain. Calibrate the min and max pulse for your servo. - The board is not found, or upload fails with a connecting message. ESP32-DevKitC V4
Use a USB cable that carries data, not charge only. Pick the ESP32 Dev Module board. If it still fails, hold BOOT while uploading starts, then release. - Some pins never read right, or the board will not boot with a wire attached. ESP32-DevKitC V4
GPIO34 to 39 are input only and have no pull-ups. GPIO6 to 11 run the flash, so leave them alone. GPIO0, 2 and 12 are boot pins; avoid pulling them the wrong way at reset.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.