A stepper motor moves in counted steps, which is why it's the heart of 3D printers and CNC machines. An Arduino can't drive it directly; it needs a driver. The best known is the A4988: cheap, and controlled with just two pins.
If you don't know the difference between a stepper and a servo, start with servos and steppers.
Wiring
| Pin | Connection |
|---|---|
| VMOT and GND | The motor supply (8–35 V) + a 100 µF capacitor close by |
| VDD and GND | 3.3 or 5 V from the Arduino |
| 1A and 1B | The motor's first coil |
| 2A and 2B | The second coil |
| STEP | A pin from the Arduino |
| DIR | A pin from the Arduino |
| RESET and SLEEP | Connected together (or to HIGH) |
| ENABLE | Ground or a pin (LOW = enabled) |
| MS1–MS3 | For microstepping |
How do you identify each coil's wires? Measure with a multimeter: the two wires with a resistance between them (a few ohms) are the same coil.
And if you have several motors, a CNC shield for the Arduino Uno holds 4 drivers and has jumper positions for microstepping, which is far more convenient than a breadboard.
The code
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++) { // 200 steps = one revolution for a 1.8° motor
digitalWrite(STEP, HIGH); delayMicroseconds(800);
digitalWrite(STEP, LOW); delayMicroseconds(800);
}
delay(1000);
}
For acceleration and higher speeds, use the AccelStepper library.
Speed in numbers
Each step in this code = 800 + 800 = 1.6 ms, so 625 steps per second.
RPM = steps per second × 60 ÷ (200 × microstepping)
In full steps: 625 × 60 ÷ 200 = 187.5 RPM. And at 1/16 with the same code: only about 11.7. So with microstepping the pulses must be much faster for the same speed.
Microstepping
| MS1 | MS2 | MS3 | Step |
|---|---|---|---|
| L | L | L | Full |
| H | L | L | 1/2 |
| L | H | L | 1/4 |
| H | H | L | 1/8 |
| H | H | H | 1/16 |
A smaller step = smoother movement and quieter sound, but you need more pulses for the same revolution.
Setting the current: the most important step
The driver has a small potentiometer that sets the maximum coil current. Leave it at random and the motor will be weak, or it and the driver will overheat.
I_max = Vref ÷ (8 × Rcs)
- Rcs: the sense resistors on the driver (read their marking: R068 = 0.068, R100 = 0.1, R050 = 0.05).
- With 0.068 Ω: I_max ≈ Vref × 1.84.
- Set it to about 70% of the motor's rated current.
Measuring: power VDD, measure the voltage between the potentiometer's wiper and ground with the multimeter, and turn it with a plastic screwdriver.
A worked example
A NEMA 17 motor rated 1.5 A per coil, and the driver has R068:
- The current needed = 70% × 1.5 = 1.05 A.
- Vref = 1.05 ÷ 1.84 ≈ 0.57 V.
- If the resistors are R100: I = Vref × 1.25, so Vref = 1.05 ÷ 1.25 ≈ 0.84 V.
Or calculate it directly with the Vref calculator.
The motor voltage
A motor rated 3 V and you're feeding it 12 or 24? That's normal and intended. The driver limits the current, and a higher voltage makes the current reach the coil faster, so the motor can spin faster without losing torque. That's why printers run on 12 or 24 V. But don't exceed the driver's maximum voltage (35 V for the A4988), and leave a margin.
The ENABLE pin
- LOW: the driver is on and the motor holds its position.
- HIGH: the coils are switched off, and the motor turns easily by hand and cools down.
If the motor stands still for long periods and doesn't need to hold a load, set ENABLE = HIGH while it's stopped, saving power and heat. But it loses its position if it moves, so home it again.
Mistakes that burn out the driver
- Disconnecting or connecting the motor with the power on. That's the most common cause of a burnt driver.
- No capacitor on VMOT. The high voltage spikes exceed its limit.
- High current without cooling: without a heatsink and fan, don't go above about 1 A.
- Fitting it backwards in the shield.
Common faults
| Fault | Usual cause |
|---|---|
| The motor shudders but doesn't turn | Two wires from different coils swapped, or high speed without acceleration |
| It turns the wrong way | Invert DIR in the code, or reverse one coil's wires |
| It loses steps | Current too low, a heavy load, or a sudden high speed |
| The driver is hot and cuts out | High current without cooling |
| Loud whining | Full steps; try 1/8 or 1/16 |
| No movement at all | RESET and SLEEP not connected, or ENABLE is HIGH |
Alternatives
- DRV8825: up to 1/32 steps and higher current, and its formula is I_max = Vref × 2 on most modules.
- TMC2208 and TMC2209: very quiet, and popular in 3D printers.
FAQ
The motor is hot while stopped; is that normal? Yes, up to about 50–60 °C. If you can't hold it in your hand, lower Vref.
Can I drive two motors from one driver? In parallel it's possible if the total current is within the limit, and both will move exactly together (like the Z axis on some printers). But one driver per motor is better.