Sub topic 9
Sending STEP and DIR signals
Prerequisites and scope
This chapter assumes you can write basic Arduino sketches (setup, loop, digitalWrite, delayMicroseconds). It also assumes you have already wired a motor to a driver with a separate motor supply and set the current limit, as covered in the earlier chapters. Here you will learn the signal interface between the Arduino and the driver: what STEP and DIR mean, the timing the driver requires, and the common mistakes.
The two-wire interface
Most stepper driver boards (A4988, DRV8825, TMC2208 and similar) hide the coil sequencing inside the driver chip. You do not energize coils yourself. You send two logic signals:
- STEP: each rising edge (LOW to HIGH) tells the driver to advance the motor by one step. In microstepping mode, one pulse advances one microstep.
- DIR: its level selects the direction. The driver reads it at the moment of the step pulse. HIGH turns one way and LOW turns the other. Which physical direction that is depends on how you wired the coils.
Two more pins often matter:
- EN (enable, usually active LOW): when HIGH, the driver turns off the motor outputs and the shaft spins freely.
- MS1, MS2, MS3: microstep selection pins, covered in the microstepping chapter.
The A4988 and DRV8825 also have RESET and SLEEP pins. On many boards, RESET floats, so you must connect it to SLEEP or to logic voltage. Otherwise the driver may never respond.
Wiring the signals
Connect the Arduino pins to the driver's logic side:
- Arduino pin 3 to
STEP. - Arduino pin 4 to
DIR. - Arduino
GNDto the driver's logicGND. - Arduino 5 V to the driver's
VDD(logic supply), if your board needs it.
The shared ground is the step people most often miss. Without a common reference, the driver cannot interpret the logic levels, and the motor behaves erratically or not at all. The motor supply (VMOT) stays separate and has its own ground connection to the driver. Do not power the motor from the Arduino's 5 V pin.
Pulse timing requirements
A driver does not accept pulses of any length. The datasheet lists a minimum STEP high time and low time. Typical values:
| Driver | Minimum STEP high | Minimum STEP low | DIR setup before STEP |
|---|---|---|---|
| A4988 | 1 µs | 1 µs | 200 ns |
| DRV8825 | 1.9 µs | 1.9 µs | 650 ns |
| TMC2208 | about 100 ns | about 100 ns | about 20 ns |
These values are approximate. Check your own board's datasheet. A pulse that is too short can be missed, and then the motor loses steps without any error.
On a classic Arduino Uno, digitalWrite takes roughly 4 µs, so the library call overhead alone usually satisfies the minimum. A 2 µs delayMicroseconds is a safe pulse width for DRV8825 boards. Faster boards (an ESP32 or a Teensy, for example) can produce pulses shorter than the minimum, so you must add a delay there.
You must also change DIR before the STEP pulse, not at the same time. The driver needs the setup time listed above. Changing the direction and pulsing in consecutive lines is fine on an Uno. On fast boards, add a short delayMicroseconds(5) after changing DIR.
A first working sketch
This sketch drives a constant 250 steps per second. The delay between edges is 2000 µs, so one full pulse cycle takes 4000 µs.
const int STEP_PIN = 3;
const int DIR_PIN = 4;
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(DIR_PIN, HIGH); // choose direction
}
void loop() {
// One step per rising edge on STEP.
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(2000);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(2000); // 4000 us period = 250 steps/s
}With a 200-step motor in full-step mode, 250 steps per second is 1.25 revolutions per second, or 75 RPM. If you enable 1/16 microstepping, the same pulses give only 1/16 of that speed, about 4.7 RPM.
Choosing a pulse width and period
Two things set your speed: the period between rising edges and the high time within it. The period decides the speed. The high time only needs to exceed the driver's minimum. Many sketches use a short fixed high time and vary only the low time:
const int STEP_PIN = 3;
const int DIR_PIN = 4;
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
}
void stepOnce(unsigned int periodMicros) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(5); // pulse width, above the driver minimum
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(periodMicros - 5); // remainder of the period
}
void loop() {
digitalWrite(DIR_PIN, HIGH);
delayMicroseconds(10); // DIR setup time
for (int i = 0; i < 200; i++) stepOnce(4000); // one revolution forward
delay(500);
digitalWrite(DIR_PIN, LOW);
delayMicroseconds(10);
for (int i = 0; i < 200; i++) stepOnce(4000); // one revolution back
delay(500);
}This sketch turns one full revolution each way with a half-second pause. Note that delayMicroseconds is accurate only for values above about 3 µs and is unreliable above 16383 µs on AVR boards. For longer delays, use delay or micros().
Changing direction safely
Reversing a motor that is running at speed is a common source of missed steps. At speed, the rotor has momentum. If you flip DIR and immediately continue at the same step rate, the motor must stop and reverse within one step period, which it usually cannot do. Slow down first, stop, flip DIR, and then speed up again. The acceleration chapter covers ramping in detail.
Using the enable pin
Driving EN HIGH disconnects the motor coils. This has two uses:
- Saving power and reducing heat when the motor is idle and carries no load.
- Letting the shaft be turned by hand.
The trade-off is that the motor loses its holding torque. A vertical axis or a loaded arm may drop when you disable the driver. Also, the driver forgets its position in the step sequence on some boards, so the shaft may jump by a fraction of a step when you re-enable it. Enable the driver before sending pulses and allow a few milliseconds for the outputs to settle.
const int EN_PIN = 8;
void setup() {
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW); // LOW = driver enabled
}Limits of blocking pulse loops
The sketches above use delayMicroseconds, so the Arduino can do nothing else while stepping. You cannot read a button, update a display, or run a second motor at a different speed. Later chapters cover non-blocking approaches using timers and the AccelStepper library. For now, treat this style as a good way to verify wiring and understand the signals.
Common mistakes
- No common ground. The motor twitches or does nothing. Connect Arduino
GNDto driverGND. RESETandSLEEPleft floating. The driver stays dead. Tie them together or follow your board's documentation.- Pulses too short. The motor stalls or runs at an unexpected speed. Increase the high time.
- Step rate too high. The motor hums and does not turn, or stutters. Lower the rate. Torque falls as speed rises.
- Wrong direction. Swap one coil pair's two wires, or invert DIR in your code. Do this only with the power off.
- Driver enabled with wrong enable polarity. If the motor is limp, check whether your board's
ENis active LOW.
Summary
STEP advances the motor one step per rising edge, and DIR picks the direction. Your code must meet the driver's minimum pulse widths, set DIR before stepping, and share a ground with the driver. Speed is set by the period between pulses.
Exercise
A 200-step motor runs in 1/8 microstepping. You want 120 RPM. How many STEP pulses per second do you need, and what is the period between rising edges in microseconds?
Show answer
Steps per revolution is 200 × 8 = 1600. Pulses per second = 120 × 1600 / 60 = 3200. The period is 1,000,000 / 3200 = 312.5 µs, so use about 312 µs between rising edges. This is well above the minimum pulse width, but delayMicroseconds resolution matters at this rate.
Exercise
Your motor does nothing, and you measure the STEP pin with a meter and see activity. List three wiring or configuration checks you would make on the driver.
Show answer
Check that Arduino GND is connected to driver GND, that the motor supply is on and connected to VMOT, and that RESET and SLEEP are not floating. Also check that EN is at the active level and that the coil pairs are wired correctly.