Built-in project · ESP32-DevKitC V4

MicroPython stepper motor on a ULN2003 board, on an ESP32-DevKitC V4

MicroPython on an ESP32: a 28BYJ-48 stepper on its ULN2003 board, IN1 to IN4 on GPIO 14, 27, 26 and 25, turns once one way and once back. 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.

Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $13. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • How a stepper motor moves in steps
  • Driving a motor through a ULN2003 board
  • Reversing direction by reversing the coil order
  • Using a list of pins in MicroPython
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: ESP32-DevKitC V4
  • Parts: 3

How it works

A stepper motor turns in small steps when its coils are switched on in order. The ULN2003 board handles the power. Four wires from the ESP32 (GPIO 14, 27, 26 and 25) go to IN1 through IN4, and the board gets 5 V and ground.

The program lights one coil at a time, cycling through all four, with 2 ms between steps. 2048 steps make one full turn of the output shaft. It turns one way, prints a message, pauses a second, turns back the other way, and repeats.

Parts list

3 parts, plus the jumper wires. They retail for about $13; on Mokxi they're free.

Kits that can build it

These 2 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 pinConnects to
Stepper motor, 28BYJ-48 with ULN2003 pin IN4ESP32-DevKitC V4 pin 25
Stepper motor, 28BYJ-48 with ULN2003 pin IN3ESP32-DevKitC V4 pin 26
Stepper motor, 28BYJ-48 with ULN2003 pin IN2ESP32-DevKitC V4 pin 27
Stepper motor, 28BYJ-48 with ULN2003 pin IN1ESP32-DevKitC V4 pin 14
Stepper motor, 28BYJ-48 with ULN2003 pin VCC5 V
Stepper motor, 28BYJ-48 with ULN2003 pin GNDGround

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.

main.py · ESP32-DevKitC V4
# Stepper motor: a 28BYJ-48 on its ULN2003 board, IN1 to IN4 on GPIO
# 14, 27, 26 and 25. Energize the coils one after another, four steps a
# cycle: 2048 steps are one turn of the output shaft. One turn each way.
from machine import Pin
import time

coils = [Pin(n, Pin.OUT, value=0) for n in (14, 27, 26, 25)]

def step(n, direction):
    for i in range(n):
        k = i % 4 if direction > 0 else 3 - i % 4
        for j, coil in enumerate(coils):
            coil.value(1 if j == k else 0)
        time.sleep_ms(2)

while True:
    step(2048, 1)
    print("one turn clockwise")
    time.sleep(1)
    step(2048, -1)
    print("one turn back")
    time.sleep(1)

Code walkthrough

The key functions of the sketch, and what each one does.

step main.py

Moves the motor n steps. For each step it picks which coil should be on: i % 4 going forward, or 3 - i % 4 going backward. It sets that coil to 1 and the other three to 0, then waits 2 ms. The wait sets the speed.

def step(n, direction):
    for i in range(n):
        k = i % 4 if direction > 0 else 3 - i % 4
        for j, coil in enumerate(coils):
            coil.value(1 if j == k else 0)
        time.sleep_ms(2)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The motor buzzes or shakes without turning This project
    The wire order matters. Check IN1 to IN4 go to GPIO 14, 27, 26 and 25 in that order.
  • It does not move at all This project
    Check the ULN2003 board has 5 V and ground connected.
  • The motor buzzes or shakes and does not turn. The coil pins IN1 to IN4 are in the wrong order for the library. Stepper motor
    For Arduino's Stepper library on a 28BYJ-48, create it with pins 8, 10, 9, 11 (IN1, IN3, IN2, IN4). Or use AccelStepper with HALF4WIRE and the same pin order.
  • It stalls or skips when spun fast or powered from the Arduino 5 V pin. It also draws more than a USB port may give. Stepper motor
    Power the ULN2003 board from a separate 5 V supply and join the grounds. Run it slowly, about 10 to 15 RPM, and ramp up speed gradually.
  • 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.