Try Embedded

Microstepping with a DRV8825

Pulse STEP yourself and switch a DRV8825 between full steps and quarter steps with its mode pins.

Motors · Intermediate

In a real product: Camera slider (Consumer electronics): Microstepping makes slow, smooth motion for time-lapse video.

Microstepping splits each full step into smaller ones by setting the two coils' currents in between their on and off levels. The motor runs smoother and quieter, at the cost of needing more pulses. The DRV8825 can divide a step by up to 32; its mode pins M0–M2 choose how much.

What you'll learn

  • Generating step pulses yourself with digitalWrite() and delayMicroseconds()
  • Microstep modes: full, ½, ¼, ⅛, 1/16 and 1/32
  • The trade-off between resolution and speed

How it works

STEP and DIR are on pins 3 and 4, and the mode pins M0, M1, M2 on pins 5, 6, 7. The mode is a 3-bit number:

M2 M1 M0Step sizePulses a turn
0 0 0full200
0 0 1½400
0 1 0¼800
0 1 1⅛1600
1 0 01/163200

The loop turns one way in full steps (200 pulses), then sets ¼ steps and turns back (800 pulses). Both are one turn, but the second takes four times as many pulses for the same distance. The motor's label shows its angle.

The DRV8825 makes its own logic supply, so it needs only GND in common with the board, and its motor supply on VMOT (8.2–45 V): here a 12 V supply.

Try this

  • Try 1/16 steps (M2 high) and see how many pulses a turn takes.
  • Make one turn always take 1 second, whatever the mode, by adjusting the delay.
  • Replace the loops with AccelStepper, as in the A4988 project.

sketch.ino

// A DRV8825: STEP pin 3, DIR pin 4, mode pins M0-M2 on pins 5-7.
// One turn in full steps, then one turn back in quarter steps.
const int STEP = 3, DIR = 4, M0 = 5, M1 = 6, M2 = 7;

void setMode(bool m2, bool m1, bool m0) {
  digitalWrite(M0, m0);
  digitalWrite(M1, m1);
  digitalWrite(M2, m2);
}

void pulses(long count, int gapUs) {
  for (long i = 0; i < count; i++) {
    digitalWrite(STEP, HIGH);
    delayMicroseconds(4);
    digitalWrite(STEP, LOW);
    delayMicroseconds(gapUs);
  }
}

void setup() {
  for (int pin = 3; pin <= 7; pin++) pinMode(pin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  setMode(0, 0, 0);
  digitalWrite(DIR, HIGH);
  Serial.println("Full steps: one turn = 200 pulses");
  pulses(200, 4000);
  delay(500);

  setMode(0, 1, 0);
  digitalWrite(DIR, LOW);
  Serial.println("Quarter steps: one turn back = 800 pulses");
  pulses(800, 1000);
  delay(500);
}

Lessons behind it