A4988 and DRV8825 stepper drivers

The two small carrier boards that turn a bipolar stepper in every 3D printer and CNC kit.

A sketch gives them a direction on DIR and a pulse on STEP, and they work out the two coil currents.

A bipolar stepper such as the NEMA 17 has two coils and no common wire, so turning it takes two full H bridges and a way of setting the current in each. These chips are both, plus a translator that turns each pulse into the next pair of currents. The two boards share a footprint and a model, and differ in the places listed below.

Pins

Pin What it does
EN Active low enable. Pulled low, so left alone the outputs are on.
MS1, MS2, MS3 (A4988) / M0, M1, M2 (DRV8825) Step resolution. Pulled low: full step.
RST Active low reset. Floats on the carrier.
SLP Active low sleep. Pulled up on the carrier, so left alone it is awake.
STEP One step on each rising edge.
DIR Which way the steps go.
VMOT Motor supply: 8 to 35 V (A4988), 8.2 to 45 V (DRV8825).
GNDM Motor ground. The two grounds are one plane on the board.
1A, 1B, 2A, 2B (A4988) / A1, A2, B1, B2 (DRV8825) The two coils.
VDD (A4988) Logic supply, 3 to 5.5 V.
FLT (DRV8825) Fault output, open drain. It never reports a fault here.
GND Logic ground.

Properties

vref: the voltage on the current-limit trimmer, 0.8 V on the A4988 and 0.5 V on the DRV8825 by default. rsense: the sense resistors, 0.1 ohm. The current limit is VREF / (8 x RSENSE) on the A4988 and VREF / (5 x RSENSE) on the DRV8825, which is 1 A for both defaults. The board shows the limit it works out.

Step resolution

A4988 MS1 MS2 MS3 DRV8825 M0 M1 M2 steps per full step
0 0 0 0 0 0 1
1 0 0 1 0 0 2
0 1 0 0 1 0 4
1 1 0 1 1 0 8
1 1 1 0 0 1 16
1 0 1, 0 1 1, 1 1 1 32

The same three wires mean different things on the two boards: all three high is 1/16 on an A4988 and 1/32 on a DRV8825, so a sketch moved from one to the other turns half as far. The board shows the resolution it is in while it drives, and "off" while it does not.

What the model gets right

The translator: a position round one electrical cycle, moved one step of the current resolution by each rising edge on STEP, and two coil currents set to its cosine and sine times the current limit. At rest it sits at the datasheets' home position, both coils at 70.7 percent. Full step visits only those 45 degree points, so switching into full step from a microstep goes to the nearest one.

The current limit, including the case people meet: the chopper cannot push more than the supply allows, so a winding whose resistance times the limit is more than VMOT just gets VMOT. That is why these want 12 V or 24 V to run a motor rated at 3.

The pins nobody wires. RST floats on the carrier and a floating reset is a reset, so a driver with nothing on RST does nothing at all: every wiring diagram has a link from RST to SLP for this reason. EN high lets go of the coils while the translator keeps counting steps, as the datasheets say. Coming out of sleep, STEP is ignored for 1 ms (1.7 ms on the DRV8825).

Logic levels. The A4988's inputs switch at 0.3 and 0.7 of VDD, so a 3.3 V board driving an A4988 whose VDD is on 5 V is out of spec: 3.3 V never reaches the 3.5 V a high needs, and here it never steps. Power VDD from 3.3 V and it does. The DRV8825 switches at fixed 0.7 V and 2.2 V, so any board drives it.

What it does not model

The chopper as a circuit: its off time, its decay modes, the ripple they leave on the current, and the whine of a slow decay at low speed. The model gives each coil exactly the table's current, worked out from the winding's resistance as the rest of the circuit shows it. Coil inductance, so the current never lags. The 2 A and 2.2 A ratings, thermal shutdown and over-current protection, so FLT never pulls low. The minimum STEP pulse widths (1 and 1.9 microseconds), which no sketch gets near. The A4988 mode combinations its datasheet does not list are read as full step. The pull resistors on the carrier are the Pololu layout; clones vary.

Code

The whole of the driving is two pins:

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 of a 200 step motor
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(2);
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(1500);
  }
  delay(500);
}

With AccelStepper, which is built in, the same two pins get a speed ramp:

#include <AccelStepper.h>
AccelStepper stepper(AccelStepper::DRIVER, 3, 4);   // STEP, DIR
void setup() {
  stepper.setMaxSpeed(1000);      // steps a second
  stepper.setAcceleration(500);
  stepper.setMinPulseWidth(2);    // the DRV8825 wants 1.9 us
  stepper.moveTo(800);
}
void loop() { stepper.run(); }

Common mistakes

Leaving RST unconnected. Forgetting to join the driver's ground to the board's. Stepping too fast without an acceleration ramp and losing steps in the motor, which is the motor's limit and not the driver's. Setting the resolution on a DRV8825 from an A4988 tutorial.

See it in action

Steppers on A4988 and DRV8825 turns two NEMA 17s from one Uno. Open it at /templates.