Motor
Controlling NEMA17 Motor with ESP32
What is the NEMA17?
NEMA17 practical reference
What is a NEMA 17 stepper motor?
A stepper motor turns in fixed steps instead of spinning freely. Tell it to move 200 steps and it makes exactly one revolution, with no encoder or feedback needed. The NEMA 17 is the size you will find on almost every desktop 3D printer, small CNC router, plotter and pick-and-place machine.
The name describes the frame, not the performance. NEMA 17 means a faceplate about 1.7 inches (42.3 mm) square, four M3 mounting holes 31 mm apart, a 22 mm centring boss and a 5 mm shaft. Two motors that both carry the name can differ a lot in torque, current and length, so the numbers on your motor's label matter more than the frame size.
Most are 1.8° per step (200 steps per revolution) and bipolar, which means two coils and four wires.
Choose the body length
A longer body holds more rotor and winding, so it makes more torque. The table compares four real motors from the PBC Linear and Handson Technology datasheets.
| Motor | Body length (max) | Holding torque | Rated current | Resistance | Inductance | Weight |
|---|---|---|---|---|---|---|
| PBC Linear single stack | 39.8 mm | 48 N·cm | 2 A | 1.04 Ω | 2.2 mH | 0.28 kg |
| PBC Linear double stack | 48.3 mm | 63 N·cm | 2 A | 1.3 Ω | 2.9 mH | 0.36 kg |
| PBC Linear triple stack | 62.8 mm | 83 N·cm | 2 A | 1.49 Ω | 3.8 mH | 0.6 kg |
| Handson 42BYGH60 | 60 mm | 80 N·cm | 1.5 A | 1.8 Ω | 7 mH | 0.56 kg |
Holding torque is the force the shaft resists with the coils powered and the motor standing still. It is a good way to compare motors, but it is not the torque you get while the motor is turning. More on that below.
The inductance column hints at something the headline numbers hide. The 42BYGH60 has almost twice the inductance of the PBC triple stack at similar torque, so it loses torque sooner as speed rises. On a slow lead-screw axis that does not matter. On a fast belt-driven axis it does.
For 3D printer axes and light CNC work, a 40 mm motor is plenty. Move up to 48 mm or 60 mm when the axis carries real weight or needs headroom at higher speed.
Match the driver to the motor
The rated current is per coil. The Handson datasheet gives 1.5 A and 1.8 Ω, which works out to 1.5 × 1.8 = 2.7 V per phase. Connect that motor straight to 12 V and each coil would try to pull about 6.7 A, which is why a NEMA 17 always needs a stepper driver. The driver switches the supply on and off very quickly (chopping) to hold each coil at the current you set.
Set that current limit before you run the motor, using the small trimmer on the driver:
A4988: Vref = I × 8 × Rs
DRV8825: Vref = I × 5 × Rs
I is the current you want in amps, and Rs is the value of the two small sense resistors next to the chip. Read their marking: R100 is 0.1 Ω, R068 is 0.068 Ω and R050 is 0.05 Ω. Measure Vref between the trimmer's metal screw and GND.
For example, a 1.5 A motor run at 80 % (1.2 A) on an A4988 with 0.1 Ω sense resistors needs Vref = 1.2 × 8 × 0.1 = 0.96 V. On a DRV8825 with the same resistors it is 1.2 × 5 × 0.1 = 0.6 V.
Running at 70 to 80 % of the rated current is a good habit. The motor keeps plenty of torque, and both motor and driver stay cooler. An A4988 without a heatsink is usually comfortable up to about 1 A per coil, so add a heatsink and some airflow above that.
Torque falls as speed rises
Each coil is an inductor, so current takes time to build up in it. At low speed there is plenty of time at every step. As the step rate climbs, the current never quite reaches its target and the torque drops. This is the most important thing to know when sizing a stepper.
The chart shows how much the supply voltage helps. A higher voltage pushes current into the coil faster, so more torque survives at speed. Use a 24 V supply if you want real speed from a NEMA 17, and keep 12 V for slow axes.
Two things stand out. First, the curve starts at about 34 N·cm even though the same motor is rated at 48 N·cm holding, so plan with the running curve and not the headline number. Second, at 3,000 steps per second (900 RPM) the 36 V curve still gives about 27 N·cm, while the 12 V curve has fallen to about 9 N·cm.
To check a load, use T = F × d. A 1 kg load hanging from a pulley with a 2 cm radius pulls with 9.81 N at 0.02 m, which is about 0.196 N·m, or 20 N·cm. A common rule of thumb is to pick a motor that still delivers about twice your load torque at the speed you need, which would be around 40 N·cm here.
Torque is quoted in several units, so keep the conversion handy: 1 N·m = 100 N·cm = 10.2 kg·cm = 141.6 oz·in.
Resolution and accuracy
One full step is 1.8°. A driver can divide each step into microsteps (1/16 gives 3,200 steps per revolution), which makes motion smoother and quieter. It does not make the motor more accurate. The PBC datasheet lists a step accuracy of ±5 %, so each full step can land about 0.09° away from where it should, and microstepping does not remove that. Torque per microstep is also much smaller than at a full step, so a heavily microstepped motor is easier to push off position.
For positioning, rely on full-step accuracy and add a home switch rather than trusting the step count forever.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Motor buzzes or shakes but does not turn | A wire swapped between the two coils, or the current limit is too low | Re-check the coil pairs with a multimeter, then raise Vref slightly |
| Motor turns the wrong way | Direction wiring | Reverse the direction in code, or swap the two wires of one coil with power off |
| Motor stalls, skips steps or loses position | Torque falling with speed, or acceleration too sharp | Raise the supply voltage, lower the top speed and use acceleration ramps such as AccelStepper |
| Driver gets very hot or shuts off | Current limit too high, or no heatsink | Lower Vref, add a heatsink and airflow |
| Driver resets or behaves erratically | Supply sag or voltage spikes | Add a 100 µF capacitor across VMOT and GND, and use a stronger supply |
| Motor case is hot to touch | Normal at rated current | The Handson datasheet allows up to an 80 °C rise. Lower Vref if it is too hot for your enclosure |
Frequently asked questions
Can I connect a NEMA 17 directly to an Arduino or ESP32? No. A GPIO pin supplies only a few tens of milliamps, and the motor needs an amp or more per coil. Use a driver such as the A4988 or DRV8825, or a TMC2209 if you want quiet running.
How many steps per revolution? 200 full steps, or 3,200 at 1/16 microstepping.
How fast can it turn? It depends on supply voltage and load. On 12 V the torque has already dropped sharply by 300 RPM, while 24 to 36 V keeps useful torque well past 900 RPM, as the chart shows.
What is 80 N·cm in kg·cm? About 8.2 kg·cm.
Does it know where it is? No. A standard NEMA 17 runs open loop, so a skipped step goes unnoticed. Use a home switch, or choose a closed-loop motor with an encoder if you cannot afford to lose position.
Key specifications
- Frame standard: NEMA 17 denotes an approximately 42.3 mm square mounting face; it does not define torque or current.
- Common step angle: 1.8° (200 full steps/revolution) is common, but verify the motor datasheet.
- Coil arrangement: Commonly a 4-wire bipolar stepper with two separate coils; wiring and connector order vary by model.
- Current and torque: Model-specific. Set the stepper-driver current limit from the exact motor datasheet.
- Driver requirement: Use a suitable current-regulating stepper driver such as A4988/DRV8825/TMC family; do not drive coils directly from GPIO.
Pinout and connection notes
NEMA 17 is a mechanical frame size, not a single electrical model. Confirm the exact rated coil current, resistance, inductance, step angle, and wire pairs from the motor datasheet before setting the driver current limit.
- A+ / A-: One motor winding. Identify the pair using the motor datasheet or a multimeter; wire colors are not standardized.
- B+ / B-: The other motor winding. Connect to the second driver output pair; swap one pair to reverse direction if needed, with power off.
Integration checklist
- Confirm the exact part number and breakout-board revision.
- Verify supply voltage, current demand, logic levels, and signal direction against the datasheet.
- Check controller pin assignments and bus address or interface mode against the example code.
- Power up with external loads disconnected, then test signals before full integration.
- If readings or communication fail, check power and ground first, then pin mapping, interface settings, and library initialization.
Troubleshooting and reproducibility
Record the board used, wiring, library/version, supply arrangement, and measured behavior. Use the selected board-specific wiring example as the source of truth. Never assume visually similar modules share identical voltage tolerance or pin order.
Setting Up NEMA17 with ESP32
Library
AccelStepper by Mike McCauley
Arduino IDE → Tools → Manage Libraries → search "AccelStepper"
Wiring
| Component pin | ESP32 pin | Note |
|---|---|---|
| STEP (on A4988/DRV8825 driver) | GPIO25 | Pulses HIGH-LOW to advance one microstep each time |
| DIR (on driver) | GPIO26 | Sets rotation direction |
| ENABLE (on driver) | GPIO27 | Optional -- pull LOW to enable driver outputs; leave floating if unused |
| VDD (driver logic supply) | 3.3V | |
| GND (driver logic) | GND | Common ground with the motor power supply is required |
| VMOT + GND (driver motor supply) | External 12-24V supply, not the ESP32 | NEMA17 coils draw far more current than the ESP32 can source -- never power them from the board itself |
| A+ / A- / B+ / B- (motor coil leads) | Driver's 1A/1B/2A/2B screw terminals | Goes to the driver board, not directly to the ESP32 |
Swipe sideways to see every column →
Requires an external stepper driver (A4988, DRV8825, or similar) between the ESP32 and the motor -- the NEMA17 draws far more current than a GPIO can supply directly. Power the driver's motor rail from a separate 12-24V supply, not the ESP32.
ESP32 integration notes\n\nUse an A4988, DRV8825, or compatible current-regulating stepper driver between the ESP32 and the motor. Connect GPIO25 to STEP, GPIO26 to DIR, and GPIO27 to ENABLE (optional; ENABLE is active LOW on common A4988/DRV8825 carrier boards). ESP32 GPIO is 3.3 V logic; confirm the selected driver carrier reliably recognizes 3.3 V HIGH before wiring. Power the driver logic pin from a voltage supported by that carrier—do not assume every module has the same pinout or requirements. Connect ESP32 GND to driver logic GND and the external motor-supply negative. Supply VMOT from a separate supply within the driver and motor ratings; never power the motor from the ESP32. Place the recommended bulk capacitor close to VMOT/GND and set the current limit for the exact motor and driver. Power everything off before connecting or disconnecting motor leads.\n\nThe example commands 200 full steps in each direction, equal to one nominal revolution for a 1.8-degree motor in full-step mode. Microstepping changes the required pulse count per revolution. AccelStepper’s run() must be called frequently; avoid long blocking work in the loop when adding Wi-Fi or other tasks.
Warning
Do not connect motor coils directly to ESP32 pins. Verify the exact driver module’s logic-voltage range and pin labels before powering it; clone carrier boards and sense-resistor values vary. Never hot-plug the motor while the driver is powered.
Related Components
A4988
The small STEP/DIR driver that runs a NEMA 17 from an Arduino or ESP32.
BLDC ESC
High-speed brushless motor and electronic speed controller combo.
DC Motor L298N
Classic brushed DC motor + dual H-bridge driver combo.
L298N Driver
Dual H-bridge motor driver — the standard for controlling two DC motors or one stepper.
Stepper 28BYJ48
Low-cost geared stepper for precise, low-speed positioning.
Related Guides & Projects
MicroPython vs Arduino on ESP32: Which Should You Learn First?
An honest comparison for beginners: where MicroPython is easier, where Arduino C++ is faster, how libraries and memory differ, and how to choose based on your project instead of hype.
MicroPython ESP32 Web Server: Control an LED or Relay from Your Browser
Create a working web server with the socket module, parse requests, serve a responsive control page for an LED or a relay, keep the displayed state truthful, choose a safe relay startup state and make the server survive bad or repeated requests.
MicroPython on ESP32 and Raspberry Pi Pico W: The Complete Guide
A structured MicroPython learning path for ESP32 and Pico W. Set up your tools, control GPIO, read sensors, connect to WiFi, build web servers, publish MQTT data and make projects reliable. Start here, then follow the links by level.
boot.py vs main.py in MicroPython: What Runs, When and Why
Understand the difference between boot.py and main.py in MicroPython, startup order, Wi-Fi setup, project structure, and recovery from boot errors.