Stepper motors
What you’ll learn
- How a stepper moves in exact steps
- The two common types and their drivers
- When to choose a stepper instead of a servo or DC motor
Steps
A stepper has several coils. Energising them in sequence pulls the rotor round one step at a time. A common NEMA 17 motor has 200 steps per revolution (1.8° per step). Count steps and you know the position exactly, with no sensor, as long as it never skips steps.
Two common types
| Motor | Driver | Notes |
|---|---|---|
| 28BYJ-48 (5 V, unipolar, geared) | ULN2003 board | Cheap, slow, about 2048 steps per output turn |
| NEMA 17 (bipolar) | A4988, DRV8825, TMC2208 | Used in 3D printers; strong and precise |
Drivers like the A4988 need only two pins: STEP (one pulse = one step) and DIR (direction). They also offer microstepping (1/2, 1/4 … 1/16 steps) for smoother, quieter motion.
Choosing a motor
| Need | Use |
|---|---|
| Spin continuously, fast (wheels) | DC gear motor |
| Go to an angle within ~180° | Servo |
| Many exact positions, full turns, holding torque | Stepper |
Example: A4988 STEP/DIR control
const int STEP = 3, DIR = 4;
void setup() { pinMode(STEP, OUTPUT); pinMode(DIR, OUTPUT);}
void loop() { digitalWrite(DIR, HIGH); for (int i = 0; i < 200; i++) { // one turn digitalWrite(STEP, HIGH); delayMicroseconds(800); digitalWrite(STEP, LOW); delayMicroseconds(800); } delay(1000);}Example: how far is one step?
A lead screw moves 8 mm per turn. With 200 steps per turn: 8 ÷ 200 = 0.04 mm per step. With 1/16 microstepping: 0.0025 mm. That’s why 3D printers are so precise.
Try it in the virtual Arduino
Press Start simulator, replace diagram.json, paste the sketch and press ▶. (In the simulator the stepper connects straight to the pins; a real one always needs a driver board.) If Wokwi says a library is missing, click its Install button and press ▶ again.
{ "version": 1, "author": "RoboLearn", "editor": "wokwi", "parts": [ { "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} }, { "type": "wokwi-stepper-motor", "id": "stepper1", "top": -170, "left": 140, "attrs": { "size": "17" } } ], "connections": [ [ "stepper1:A-", "uno:8", "green", [] ], [ "stepper1:A+", "uno:9", "green", [] ], [ "stepper1:B+", "uno:10", "blue", [] ], [ "stepper1:B-", "uno:11", "blue", [] ] ], "dependencies": {}}#include <Stepper.h>
const int STEPS_PER_REV = 200; // 1.8° per stepStepper motor(STEPS_PER_REV, 8, 9, 10, 11);
void setup() { motor.setSpeed(30); // rpm}
void loop() { motor.step(STEPS_PER_REV); // one full turn delay(500); motor.step(-STEPS_PER_REV / 2); // half a turn the other way delay(500);}Runs on Wokwi, free, no account needed.
Quarter turnsEasy
Make the stepper move a quarter turn, pause, and repeat.
motor.step(50); (200 ÷ 4) followed by delay(500);
Exact angleMedium
How many steps for 135° with a 1.8° stepper? 135 ÷ 1.8 = 75 steps.
Clock handHard
Make the stepper act like a seconds hand: one full turn per 60 seconds, in 60 even ticks. 200 steps ÷ 60 ticks isn’t a whole number. Keep a running total so the error doesn’t add up.
Show an answer
int done = 0;void loop() { for (int tick = 1; tick <= 60; tick++) { int target = tick * 200 / 60; // where we should be motor.step(target - done); done = target; delay(1000); } done = 0;}Quick check
Finished this lesson?