Wiring guide

How to wire a 28BYJ-48 stepper to an ESP32

To wire a 28BYJ-48 stepper to an ESP32, connect IN1 to GPIO 25 (D25), IN2 to GPIO 26 (D26), IN3 to GPIO 27 (D27), IN4 to GPIO 14 (D14), VCC to VIN (5 V from USB) 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
ESP32 · 28BYJ-48 stepperlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

28BYJ-48 stepper to ESP32 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 pinESP32 pinWire colorWhy
IN1GPIO 25 (D25)amberCoil A, through the ULN2003’s first Darlington.
IN2GPIO 26 (D26)greenCoil B.
IN3GPIO 27 (D27)violetCoil C.
IN4GPIO 14 (D14)cyanCoil D.
VCC (+)VIN (5 V from USB)redThe motor’s 5 V supply: a couple of hundred milliamps, more than USB likes to spare.
GND (-)GNDblueCommon ground.
Voltage

Power and logic levels for 28BYJ-48 stepper and ESP32

Logic level

The ESP32’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 VIN (5 V from USB). That is the ESP32’s 5 V rail, so the part shares the current the board can supply.

This pair

The motor runs from VIN (5 V from USB), 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 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.

The sketch

The working code for the ESP32

Library: Stepper (Stepper.h). This is the sketch the circuit above runs.

28BYJ-48 stepper on ESP32
#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, 25, 27, 26, 14);

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);
}
Output

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 back
Pitfalls

Common mistakes wiring a 28BYJ-48 stepper to an ESP32

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.