Built-in project · Arduino Uno R3

Steppers on A4988 and DRV8825, on an Arduino Uno

STEP and DIR into an A4988 and a DRV8825, each turning a NEMA 17. The same pulses to both, and the DRV8825 turns half as far. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $55. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino A4988 and NEMA 17 Stepper: Wiring and Code

The circuit runs here once the page has loaded.

What you'll learn

  • Driving a stepper with only STEP and DIR
  • How microstepping mode pins change steps per turn
  • Why the same sketch can behave differently on two drivers
  • Why RST must be linked to SLP
  • How pulse timing limits motor speed
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 14

How it works

A stepper driver needs only two signals. DIR picks the direction, and each rising edge on STEP moves the motor one step. The sketch sends the same 3200 pulses to an A4988 on pins 3 and 4 and a DRV8825 on pins 5 and 6 at the same time.

All three mode pins on both drivers are tied high. That means 1/16 step on the A4988 and 1/32 on the DRV8825. So the same pulses turn the A4988 motor one full turn and the DRV8825 motor half a turn. Then both go back.

Parts list

14 parts, plus the jumper wires. They retail for about $55; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Arduino Uno R3 pin GNDGround
Stepper driver, DRV8825 pin DIRArduino Uno R3 pin 6
Stepper driver, DRV8825 pin STEPArduino Uno R3 pin 5
Stepper driver, A4988 pin DIRArduino Uno R3 pin 4
Stepper driver, A4988 pin STEPArduino Uno R3 pin 3
Stepper driver, A4988 pin MS1Supply
Stepper driver, A4988 pin MS2Supply
Stepper driver, A4988 pin MS3Supply
Stepper driver, A4988 pin SLPStepper driver, A4988 pin RST
Stepper driver, A4988 pin VMOT12 V
Stepper motor, NEMA 17 (1) pin A1Stepper driver, A4988 pin 2B
Stepper motor, NEMA 17 (1) pin A2Stepper driver, A4988 pin 2A
Stepper motor, NEMA 17 (1) pin B1Stepper driver, A4988 pin 1A
Stepper motor, NEMA 17 (1) pin B2Stepper driver, A4988 pin 1B
Stepper driver, A4988 pin VDDSupply
Stepper driver, A4988 pin GNDGround
Stepper driver, DRV8825 pin M0Supply
Stepper driver, DRV8825 pin M1Supply
Stepper driver, DRV8825 pin M2Supply
Stepper driver, DRV8825 pin SLPStepper driver, DRV8825 pin RST
Stepper driver, DRV8825 pin VMOT12 V
Stepper motor, NEMA 17 (2) pin A1Stepper driver, DRV8825 pin B2
Stepper motor, NEMA 17 (2) pin A2Stepper driver, DRV8825 pin B1
Stepper motor, NEMA 17 (2) pin B1Stepper driver, DRV8825 pin A1
Stepper motor, NEMA 17 (2) pin B2Stepper driver, DRV8825 pin A2
Stepper driver, DRV8825 pin GNDGround

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Steppers on A4988 and DRV8825: STEP, DIR, and nothing else.
//
//   pin 3  the A4988's STEP     pin 4  its DIR
//   pin 5  the DRV8825's STEP   pin 6  its DIR
//   both   VMOT on 12 V, the grounds to the Uno's GND, RST linked to SLP,
//          all three mode pins on 5 V
//   A4988  VDD on 5 V; its motor on 1A/1B and 2A/2B
//   DRV8825 its motor on A1/A2 and B1/B2
//
// A stepper driver takes two wires from a sketch. DIR says which way; each
// rising edge on STEP moves the motor one step. Everything else, the two coil
// currents and the order they go in, is the driver's job. That is the whole
// reason these boards exist, and why a sketch for one is a loop that toggles
// a pin. This one toggles two, one for each driver, at the same moment, so
// both get exactly the same pulses.
//
// # One pulse, two different steps
//
// Both drivers have their three mode pins tied high. On the A4988 that is
// 1/16 step; on the DRV8825 the same three pins mean 1/32. So 3200 pulses turn
// the top motor exactly once and the bottom one exactly half a turn. Moving a
// sketch from one board to the other without reading the table is a classic
// way to build a printer that prints at half size.
//
// # The link from RST to SLP
//
// RST floats on these boards and a floating reset holds the driver off. Take
// the link away and nothing moves: the outputs stay off and the board shows
// "off".
//
// # How fast
//
// 400 us a pulse at 1/16 is 156 full steps a second for the top motor, well
// under the thousand a NEMA 17 follows from a standstill. Try 20 us and watch
// the rotors fall behind and slip.

const int STEP_A = 3;
const int DIR_A = 4;
const int STEP_B = 5;
const int DIR_B = 6;

const unsigned int PULSES = 3200;   // one turn at 1/16, half a turn at 1/32
const unsigned int PERIOD_US = 400;

void turn(bool forward) {
  digitalWrite(DIR_A, forward ? HIGH : LOW);
  digitalWrite(DIR_B, forward ? HIGH : LOW);
  for (unsigned int i = 0; i < PULSES; i++) {
    digitalWrite(STEP_A, HIGH);
    digitalWrite(STEP_B, HIGH);
    delayMicroseconds(4);
    digitalWrite(STEP_A, LOW);
    digitalWrite(STEP_B, LOW);
    delayMicroseconds(PERIOD_US - 4);
  }
}

void setup() {
  pinMode(STEP_A, OUTPUT);
  pinMode(DIR_A, OUTPUT);
  pinMode(STEP_B, OUTPUT);
  pinMode(DIR_B, OUTPUT);
  Serial.begin(115200);
  Serial.println("Mokxi Uno: 3200 pulses to an A4988 and a DRV8825");
  // The drivers ignore STEP for a millisecond or two after they wake.
  delay(10);
}

void loop() {
  Serial.println("forward: A4988 one turn, DRV8825 half a turn");
  turn(true);
  delay(800);
  Serial.println("and back");
  turn(false);
  delay(800);
}

Code walkthrough

The key functions of the sketch, and what each one does.

turn sketch.ino

Sets both DIR pins for the direction, then sends PULSES steps. Each one raises both STEP pins for 4 microseconds, lowers them, and waits out the rest of PERIOD_US (400). Both drivers get identical pulses at the same moment.

void turn(bool forward) {
  digitalWrite(DIR_A, forward ? HIGH : LOW);
  digitalWrite(DIR_B, forward ? HIGH : LOW);
  for (unsigned int i = 0; i < PULSES; i++) {
    digitalWrite(STEP_A, HIGH);
    digitalWrite(STEP_B, HIGH);
    delayMicroseconds(4);
    digitalWrite(STEP_A, LOW);
    digitalWrite(STEP_B, LOW);
    delayMicroseconds(PERIOD_US - 4);
  }
}

loop sketch.ino

Prints a message and calls turn(true), waits 800 ms, then prints and calls turn(false) to return. The A4988 motor makes one turn each way and the DRV8825 motor half a turn.

void loop() {
  Serial.println("forward: A4988 one turn, DRV8825 half a turn");
  turn(true);
  delay(800);
  Serial.println("and back");
  turn(false);
  delay(800);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Neither motor moves This project
    RST must be linked to SLP on each driver. A floating reset holds the driver off. Also check that VMOT has 12 V and the grounds are joined.
  • The motors lose steps and buzz This project
    PERIOD_US is too short. Pulses closer together than the rotor can follow make it slip. Raise the period.
  • The motor does nothing and the driver stays dead. Stepper driver
    Tie RST to SLP with a jumper, or drive both HIGH. Those two pins float on many boards. Also connect the logic VDD to 5 V and ground, and the motor VMOT to its own supply.
  • The driver or the chip dies when you power up. Stepper driver
    Put a 100 uF capacitor across VMOT and GND, close to the driver. Never connect or unplug the motor while powered, because the voltage spike can kill the chip.
  • The motor does not move or skips steps. Stepper driver
    Power the motor supply with 8.2 to 45 V and add a 100 uF capacitor close to it. Pull SLEEP and RESET high, and ENABLE low. The chip gets hot without proper current setting.
  • The step size is wrong. Stepper driver
    The mode pins M0, M1, M2 set 1/1 to 1/32 stepping. They differ from the A4988, so check the table. Adjust the current limit with the trimmer.
  • The motor buzzes, shudders, or only vibrates instead of turning. Stepper motor
    Coils are swapped. Find the two coil pairs by measuring resistance with a meter (a few ohms between wires of one coil). Connect each pair to one driver coil output, A to A and B to B.
  • The motor stalls or loses steps when you speed up. Stepper motor
    A stepper cannot jump to a high speed. Start with slow step pulses and ramp up gradually. Check the driver current limit (Vref) and give the supply enough voltage, such as 12 V.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.