A NEMA 17 stepper on an A4988, driven by an Arduino
Intermediate
Or see every lesson.
- 261parts on the bench
- 31boards running now
- 1.00xreal time, on every board
The A4988 is the small driver board found in 3D printers and CNC kits. It turns a NEMA 17 stepper motor using just two wires from your Arduino. DIR says which way to turn. Each pulse on STEP moves the motor one step. The driver does everything else.
The circuit above is an Arduino Uno sending the same pulses to two drivers: an A4988 on top and a DRV8825 below, each with a NEMA 17. Watch closely. The top motor turns a full circle while the bottom one turns only half. This page explains why, and it runs in your browser on the sketch below.
What you need
- An Arduino Uno
- An A4988 stepper driver (or a DRV8825)
- A NEMA 17 bipolar stepper motor
- A 12 V power supply for the motor
- Jumper wires, and a breadboard for the driver
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| A4988 STEP | Pin 3 | One step per rising edge |
| A4988 DIR | Pin 4 | Which way to turn |
| A4988 RST | A4988 SLP | Link these two pins together, or nothing moves |
| A4988 MS1, MS2, MS3 | 5 V | All three HIGH is 1/16 step on an A4988 |
| A4988 VDD | 5 V | Logic supply |
| A4988 GND | GND | Shared with the Uno |
| A4988 VMOT | 12 V | Motor supply, 8 to 35 volts |
| A4988 1A, 1B | Motor coil B | One coil of the NEMA 17 |
| A4988 2A, 2B | Motor coil A | The other coil |
| DRV8825 STEP and DIR | Pins 5 and 6 | The second driver in the example |
STEP and DIR are the whole interface
A NEMA 17 has two coils and no common wire, so it takes two full H bridges to drive it. The A4988 is both bridges, plus a part that turns each pulse into the next pair of coil currents. Your sketch only sets DIR and then toggles STEP.
In the example, 3200 pulses go out at 400 microseconds each. The loop sets DIR, sends the pulses, waits, then flips DIR and sends them again. That is a stepper sketch in full.
const int STEP_PIN = 3;
const int DIR_PIN = 4;
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
}
void loop() {
digitalWrite(DIR_PIN, HIGH);
for (int i = 0; i < 200; i++) { // one turn in full step
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(2);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(1500);
}
delay(500);
}Microstepping, and why one motor turns half as far
A NEMA 17 has 200 full steps per turn. The MS1, MS2 and MS3 pins split each full step into smaller ones. With all three pins LOW, you get full steps: 200 pulses per turn. With all three HIGH, an A4988 does 1/16 steps, so one turn takes 3200 pulses.
Here is the catch. On a DRV8825, the same three pins all HIGH mean 1/32 step. So the same 3200 pulses turn it only half a turn. That is exactly what you see in the circuit above. If you move a sketch from one board to the other, check the table in its datasheet first.
The code snippet above uses 200 pulses per turn, which is right with all three mode pins LOW. At 1/16 step, change it to 3200.
The link from RST to SLP
On the A4988 carrier board, the RST pin is not pulled either way. A floating reset holds the driver off, so a driver with nothing on RST does nothing at all. That is why every wiring diagram joins RST to SLP with a short wire. The SLP pin is pulled HIGH on the board, so the link keeps RST HIGH too.
Try it in the editor: delete the short wire between RST and SLP on the top driver and press Run. The motor stops, and the board shows "off".
Setting the current limit
The small knob on the A4988 sets how much current goes to each coil. You set it by measuring the voltage on the knob, called VREF. On the A4988 the limit is VREF divided by eight times the sense resistor. With the common 0.1 ohm resistors, that is VREF times 1.25 amps. So 0.8 V gives a 1 A limit, which is the default here.
Click the driver and change vref in the properties panel. The board shows the new limit it works out.
Common mistakes
Leaving RST unconnected, so the driver never wakes up.
No shared ground between the driver and the Arduino.
Stepping too fast from a standstill. The motor falls behind and skips steps. Start slower, or use AccelStepper to ramp up the speed. It is built in here.
Using DRV8825 microstep settings from an A4988 guide, or the other way around.
Plugging or unplugging the motor while the driver is powered. On a real board, that can destroy the driver.
Questions
How do I wire an A4988 to an Arduino?
STEP and DIR to any two digital pins, VDD to 5 V, GND to GND, RST linked to SLP, VMOT to your motor supply (8 to 35 V), and the motor’s two coils to 1A/1B and 2A/2B.
How many steps is one turn of a NEMA 17?
Most NEMA 17 motors are 200 full steps per turn. At 1/16 microstepping, that is 3200 pulses per turn.
Can I use AccelStepper with an A4988?
Yes. AccelStepper stepper(AccelStepper::DRIVER, 3, 4) takes the STEP and DIR pins, and it compiles in Mokxi as it stands.
What does Mokxi not model on the A4988?
The chopper as a circuit, coil inductance, heat and the thermal shutdown. The step table, the current limit, the floating RST and the wake-up delay after sleep are modeled.
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.