Skip to content

Motor · ESP32 guide

Controlling NEMA17 Stepper Motor with ESP32

The standard 42 mm stepper behind most 3D printers and small CNC machines: 200 steps per turn, four wires, and a driver that sets the current.

Updated November 16, 2026 Arduino C++AccelStepperAlso forArduino Uno

What is the NEMA17?

A NEMA 17 is a stepper motor with a 42.3 mm (1.7 inch) square faceplate. It turns in exact steps, usually 1.8° per step (200 steps per revolution), with no encoder needed, which is why it sits in almost every desktop 3D printer, small CNC router, plotter and pick-and-place machine. Most are bipolar: two coils, four wires. The name describes the frame, not the performance: two NEMA 17 motors can differ widely in torque, current and length, so the numbers on your motor's label matter more than the name.

You cannot connect one directly to an Arduino or ESP32. Each coil needs roughly 1 to 2 A, a GPIO gives tens of milliamps, so the motor needs a stepper driver (A4988, DRV8825 or TMC2209) and its own 12 to 24 V supply. The driver turns two cheap signals, STEP and DIR, into the right current in the coils. This guide covers the specifications, how to pick the body length, how to wire and identify the coils, how to set the driver current (Vref) without burning anything, microstepping, why torque falls with speed, and the faults you will meet. When you are ready to build, the Arduino Uno guide and the ESP32 guide take you from wiring to homing and speed control.

Specifications

ParameterTypical value
Frame size42.3 mm (1.7 in) square
Step angle1.8° (200 steps per revolution)
Winding4-wire bipolar, two coils
Rated current1 to 2 A per coil (set by the model)
Mounting holes4 × M3, 31 mm apart
Centring boss22 mm diameter
Shaft5 mm diameter, often with a flat (D-shaft)
Holding torqueAbout 40 to 85 N·cm, depends on body length
Body lengthAbout 34 to 63 mm (see the table below)
Step accuracy±5 % of a full step (±0.09°) per the PBC datasheet
Supply (via driver)12 to 24 V typical; up to 36 V with a suitable driver
Case temperatureDatasheets allow up to about 80 °C rise at rated current

Wires and coil pairs

WireNameDescription
1A+Coil A, one end. Pair it with A− using the datasheet or a multimeter
2A−Coil A, other end
3B+Coil B, one end
4B−Coil B, other end

Wiring & code

Setting up NEMA17 with ESP32

Library

AccelStepper by Mike McCauley

Arduino IDE > Tools > Manage Libraries > search "AccelStepper" > install "AccelStepper" by Mike McCauley

This guide drives a NEMA 17 from an ESP32 through an A4988 or DRV8825 driver. The ESP32's 3.3 V logic works with both. Three sketches build up from a basic move to Serial command control and smooth high-speed moves with FastAccelStepper, which uses the ESP32's hardware to generate pulses. For the motor, torque, microstepping and the Vref formula, read the NEMA 17 overview first.

What you need

  • 1ESP32 DevKit (WROOM-32)

    Notes
    3.3 V logic
  • 1NEMA 17 stepper motor

    Notes
    Rated about 1 to 2 A per coil
  • 1A4988 or DRV8825 driver module

    Notes
    Check the sense-resistor marking for the Vref formula
  • 112 to 24 V DC power supply

    Notes
    2 A or more; separate from the ESP32
  • 1100 µF electrolytic capacitor

    Notes
    Across VMOT and GND at the driver
  • 1Breadboard and jumper wires

Wiring (A4988)

A4988 pinESP32 pinNotes
STEPGPIO25One pulse = one (micro)step
DIRGPIO26Sets the direction
ENABLEGPIO27Active LOW; optional
VDD3V3Logic supply (A4988 only; the DRV8825 has no VDD pin)
GND (logic)GNDCommon ground with the supply and the motor ground
RESET and SLEEPJoin them togetherTie RESET to SLEEP with a jumper wire
MS1, MS2, MS33V3 (all three)Selects 1/16 microstepping. Leave all low for full steps
VMOT and GND (motor)12 to 24 V supplyAdd the 100 µF capacitor across these pins. Never from the ESP32
1A, 1B, 2A, 2BMotor coil leadsCoil 1 on 1A/1B, coil 2 on 2A/2B

Warning

Set Vref before connecting the motor, and switch everything off before connecting or disconnecting it. The ESP32 pins are 3.3 V logic: the A4988 and DRV8825 read 3.3 V as HIGH, but confirm that your exact carrier does. Power the driver's VDD from 3.3 V so the ESP32 never sees 5 V on a pin. Never power the motor from the ESP32.

  1. 1Identify the coil pairs with a multimeter and mark them.
  2. 2Wire the driver as in the table, with the motor disconnected.
  3. 3Power the 12 to 24 V supply, and adjust the trimmer until Vref matches your calculated value.
  4. 4Power off, then connect the motor coils.
  5. 5In the Arduino IDE install the ESP32 board package, then in Tools > Manage Libraries install AccelStepper by Mike McCauley (and FastAccelStepper by gin66 for Sketch 3).
  6. 6Select your ESP32 board and port, upload Sketch 1, then power the motor supply.

Sketch 1 - Back and forth, one revolution each way

sketch.ino
#include <AccelStepper.h>

const int STEP_PIN = 25;
const int DIR_PIN = 26;
const int ENABLE_PIN = 27;

const int MICROSTEPS = 16;                       // MS1-MS3 high on an A4988
const long STEPS_PER_REV = 200L * MICROSTEPS;    // 3200

AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);

void setup() {
  pinMode(ENABLE_PIN, OUTPUT);
  digitalWrite(ENABLE_PIN, LOW);                 // LOW enables the driver outputs

  stepper.setMaxSpeed(STEPS_PER_REV);            // 1 revolution per second
  stepper.setAcceleration(STEPS_PER_REV);
  stepper.moveTo(STEPS_PER_REV);
}

void loop() {
  if (stepper.distanceToGo() == 0) {
    delay(500);
    stepper.moveTo(-stepper.currentPosition());
  }
  stepper.run();
}

Sketch 2 - Control from the Serial Monitor

Type commands in the Serial Monitor at 115200 baud (set it to Newline line ending): move 3200 moves 3,200 steps from where it is, to 0 goes to absolute position 0, speed 2000 sets the top speed in steps per second, stop decelerates to a halt, and pos prints the position.

sketch.ino
#include <AccelStepper.h>

const int STEP_PIN = 25;
const int DIR_PIN = 26;
const int ENABLE_PIN = 27;

AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
String line;

void handle(const String& cmd) {
  if (cmd.startsWith("move ")) {
    stepper.move(cmd.substring(5).toInt());
  } else if (cmd.startsWith("to ")) {
    stepper.moveTo(cmd.substring(3).toInt());
  } else if (cmd.startsWith("speed ")) {
    stepper.setMaxSpeed(cmd.substring(6).toFloat());
  } else if (cmd == "stop") {
    stepper.stop();
  } else if (cmd == "pos") {
    Serial.println(stepper.currentPosition());
  } else if (cmd.length() > 0) {
    Serial.println("Commands: move N, to N, speed N, stop, pos");
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(ENABLE_PIN, OUTPUT);
  digitalWrite(ENABLE_PIN, LOW);
  stepper.setMaxSpeed(2000);
  stepper.setAcceleration(2000);
  Serial.println("Ready. Commands: move N, to N, speed N, stop, pos");
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\n' || c == '\r') { handle(line); line = ""; }
    else line += c;
  }
  stepper.run();
}

Sketch 3 - Fast, smooth moves with FastAccelStepper

AccelStepper times every pulse in software, which limits the rate and can jitter when Wi-Fi is busy. FastAccelStepper generates the pulses in ESP32 hardware, so it reaches much higher rates and keeps running while your code does other work. Whether the motor can follow is a torque question: at 12 V it will stall at high speed, so use 24 V for the faster moves.

sketch.ino
#include <FastAccelStepper.h>

const int STEP_PIN = 25;
const int DIR_PIN = 26;
const int ENABLE_PIN = 27;

FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper = NULL;

void setup() {
  Serial.begin(115200);
  engine.init();
  stepper = engine.stepperConnectToPin(STEP_PIN);
  if (stepper) {
    stepper->setDirectionPin(DIR_PIN);
    stepper->setEnablePin(ENABLE_PIN);           // active LOW by default
    stepper->setAutoEnable(true);                // driver off when idle
    stepper->setSpeedInHz(8000);                 // 8000 steps/s = 150 RPM at 1/16
    stepper->setAcceleration(20000);
  }
}

void loop() {
  stepper->move(3200 * 5);                       // five revolutions forward
  while (stepper->isRunning()) delay(10);
  delay(500);
  stepper->move(-3200 * 5);                      // and back
  while (stepper->isRunning()) delay(10);
  delay(500);
}

Tip

If the motor stalls or squeals, lower setSpeedInHz and setAcceleration first. Raise them in steps of about 20 % until it just starts to miss, then back off by 20 % for a safe margin. A 24 V supply raises that limit noticeably.

ESP32 notes

  • Pins. GPIO25, 26 and 27 are safe general-purpose pins. Avoid the strapping pins (0, 2, 5, 12, 15) and the flash pins (6 to 11) for STEP and DIR. See the ESP32 pinout guide.
  • Wi-Fi and timing. With AccelStepper, heavy Wi-Fi work can delay the loop and make the motor stutter. Keep the loop light, or use FastAccelStepper for hardware timing.
  • Idle heat. Disable the driver (ENABLE high) when the motor does not need to hold position.
  • Power budget. The ESP32 and the motor share only ground. Keep the motor supply separate to avoid resets.

ESP32-specific problems

  • Nothing moves

    Likely cause
    RESET and SLEEP not joined, no VMOT, or ENABLE high
    Fix
    Jumper RESET to SLEEP; check the motor supply; drive ENABLE low
  • Motor buzzes but does not turn

    Likely cause
    Coil wires crossed or Vref too low
    Fix
    Re-check the coil pairs; raise Vref slightly
  • Stutter when Wi-Fi is active

    Likely cause
    Software pulse timing delayed
    Fix
    Use FastAccelStepper, or keep the loop light
  • Driver does not respond to 3.3 V

    Likely cause
    Some carrier boards need more than 3.3 V on STEP or DIR
    Fix
    Check the carrier datasheet; try the same code with a level shifter
  • Motor stalls at high speed

    Likely cause
    Torque falls with speed
    Fix
    Raise the supply to 24 V, lower speed and acceleration
  • ESP32 resets when the motor starts

    Likely cause
    Supply sag or noise
    Fix
    Separate supplies, common ground, 100 µF at VMOT
  • Distance does not match the code

    Likely cause
    Microstep pins and constants differ
    Fix
    Match MS1 to MS3 with 200 × MICROSTEPS
  • Compile error for FastAccelStepper

    Likely cause
    Library not installed, or not an ESP32 board selected
    Fix
    Install it from the Library Manager and select an ESP32 board

ESP32 FAQ

Can an ESP32 drive an A4988 directly? Yes, through its STEP and DIR pins at 3.3 V. The ESP32 does not power the motor; the driver and its own supply do.

Why FastAccelStepper? It generates pulses with ESP32 hardware, so high speeds and Wi-Fi work together. AccelStepper is simpler and fine for modest speeds.

Can I control it over Wi-Fi? Yes: add a web server or MQTT handler that calls stepper.move() or moveTo(), and keep the handlers short. See the ESP32 MQTT guide.

Does it work with an ESP32-S3 or C3? Yes. Change the pins to ones your board supports.

Which driver is best with the ESP32? The A4988 and DRV8825 both work. The TMC2209 gives the quietest running.

Next: the NEMA 17 overview covers torque, the Vref formula and choosing a driver. For a simpler Arduino build see the Uno guide.

Keep learning

Related guides & projects