How to wire a 28BYJ-48 stepper to an ESP32-C3
To wire a 28BYJ-48 stepper to an ESP32-C3, connect IN1 to GPIO 4, IN2 to GPIO 5, IN3 to GPIO 6, IN4 to GPIO 7, VCC to 5V and GND to GND. The motor runs from the 5 V pin while IN1 to IN4 come from 3.3 V pins, and the ULN2003 driver switches happily from 3.3 V, so no level shifter is needed. The sketch below uses the Stepper (Stepper.h) library. It turns the shaft a quarter turn forward and a quarter turn back.
- 3.3 V logic
- Library: Stepper.h
- 6 wires
28BYJ-48 stepper to ESP32-C3 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| 28BYJ-48 stepper pin | ESP32-C3 pin | Wire color | Why |
|---|---|---|---|
| IN1 | GPIO 4 | amber | Coil A, through the ULN2003’s first Darlington. |
| IN2 | GPIO 5 | green | Coil B. |
| IN3 | GPIO 6 | violet | Coil C. |
| IN4 | GPIO 7 | cyan | Coil D. |
| VCC (+) | 5V | red | The motor’s 5 V supply: a couple of hundred milliamps, more than USB likes to spare. |
| GND (-) | GND | blue | Common ground. |
Power and logic levels for 28BYJ-48 stepper and ESP32-C3
Logic level
The ESP32-C3’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The 28BYJ-48 stepper takes 5 V from 5V. That is the ESP32-C3’s 5 V rail, so the part shares the current the board can supply.
This pair
The motor runs from 5V, while IN1 to IN4 come from 3.3 V pins. The ULN2003 switches happily from 3.3 V, so no level shifting is needed. On a real bench, a stepper on USB power can brown out the board when it starts; a separate 5 V supply with the grounds joined is safer.
Pins to leave alone
GPIO 8 carries the onboard LED and GPIO 9 is the BOOT button, and both are also the default I2C pins. GPIO 2, 8 and 9 are strapping pins, and only GPIO 0 to 4 can read an analog voltage.
The working code for the ESP32-C3
Library: Stepper (Stepper.h). This is the sketch the circuit above runs.
#include <Stepper.h>
// The 28BYJ-48 through its ULN2003 board: 2048 full steps a turn.
const int STEPS_PER_TURN = 2048;
// Note the order: IN1, IN3, IN2, IN4. The Stepper library's sequence
// matches the motor's coils that way.
Stepper motor(STEPS_PER_TURN, 4, 6, 5, 7);
void setup() {
Serial.begin(115200);
motor.setSpeed(12); // rpm; the 28BYJ-48 tops out near 15
Serial.println("Stepper ready");
}
void loop() {
Serial.println("Quarter turn forward");
motor.step(STEPS_PER_TURN / 4);
delay(300);
Serial.println("Quarter turn back");
motor.step(-STEPS_PER_TURN / 4);
delay(300);
}
What you should see
The shaft turns a quarter turn forward and then a quarter turn back; the 28BYJ-48 needs 2048 steps for a full turn. The serial monitor, at 115200 baud, prints Stepper ready, then Quarter turn forward and Quarter turn back.
A motor that hums but does not turn has its coil order wrong: with the Stepper library the constructor lists the four pins in the order IN1, IN3, IN2, IN4, and this sketch does the same.
The serial monitor prints this, recorded from the circuit above running for three seconds:
Stepper ready
Quarter turn forward
Quarter turn backCommon mistakes wiring a 28BYJ-48 stepper to an ESP32-C3
The pin order in the constructor
Stepper(steps, IN1, IN2, IN3, IN4) makes the 28BYJ-48 vibrate without turning. The order has to be IN1, IN3, IN2, IN4, as in the sketch.
Asking for more than 15 rpm
The gearbox makes this motor slow. Above about 15 rpm it skips steps or stalls, and because nothing reports the shaft’s position, the sketch never knows. Keep setSpeed() low.
Assuming the motor knows where it is
A stepper has no sensor: the sketch counts the steps it sent. After a stall or a power cycle that count is wrong, which is why real machines home against a limit switch at start-up.
Build your own 28BYJ-48 stepper project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.