Electric Motors

How to Drive a Stepper Motor With an Arduino and an A4988 Driver

Wire an A4988 stepper driver to an Arduino, set the current limit safely, pick microstepping, and run a simple sketch that moves a NEMA 17 motor.

On this page
  1. What you need
  2. Wiring
  3. Set the current limit before you run
  4. Microstepping
  5. Test sketch
  6. Troubleshooting
  7. Safety

The A4988 is a small driver module. It takes step and direction signals from an Arduino and drives a bipolar stepper motor, such as a NEMA 17. The module handles the coil switching and limits the current, so the Arduino needs only a few pins. This guide covers the wiring, the current limit, microstepping and a simple test sketch.

What you need

  • An Arduino board, such as an Uno.
  • An A4988 module, or a compatible board.
  • A bipolar stepper motor, such as a NEMA 17. Note its rated current and coil resistance.
  • A motor supply within the A4988’s range, which is 8 V to 35 V on the motor side. Match it to the motor’s rated voltage.
  • An electrolytic capacitor of about 100 µF, rated above the motor supply voltage. Many module makers recommend one across the motor supply.
  • Jumper wires and a breadboard or perfboard.

Wiring

A4988 pin Connect to Notes
VDD Arduino 5 V Logic power
GND (logic) Arduino GND Common ground
VMOT Motor supply + Put the capacitor across VMOT and GND, close to the module
GND (motor side) Motor supply − Also tied to the Arduino GND
1A, 1B, 2A, 2B Motor coils Two wires per coil. See stepper motor wiring to identify the pairs.
STEP Arduino pin 3 Each pulse moves the motor one step
DIR Arduino pin 2 HIGH and LOW set the direction
MS1, MS2, MS3 Microstepping setting See the table below
ENABLE Arduino GND, or a spare pin Active low: LOW enables the outputs
RESET and SLEEP Tied together, usually on the module Both must be HIGH for the driver to run

Connect the motor coils with the power off. Keep the Arduino ground and the motor supply ground common.

Set the current limit before you run

The A4988 limits the current in each coil using a reference voltage, V_ref, that you set with the small potentiometer on the board. The datasheet gives the relationship:

I_max = V_ref ÷ (8 × R_sense)

R_sense is the sense resistor value on your board, and it differs between module designs, so check your module’s documentation. Set the limit below the motor’s rated current, especially if the module has no heat sink. To set it, measure V_ref at the test point your module documents, with the motor supply on, and turn the potentiometer until you reach the value you calculated.

Microstepping

MS1, MS2 and MS3 set the step size. With all three low, the driver uses full steps.

MS1 MS2 MS3 Mode Steps per revolution (1.8° motor)
Low Low Low Full step 200
High Low Low Half step 400
Low High Low Quarter step 800
High High Low Eighth step 1600
High High High Sixteenth step 3200

Smaller steps give smoother motion, but each step gives less torque. They also need more pulses per revolution, so the Arduino must send them faster for the same speed.

Test sketch

const int STEP_PIN = 3;
const int DIR_PIN = 2;

void setup() {
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  digitalWrite(DIR_PIN, HIGH);    // HIGH or LOW sets the direction
}

void loop() {
  // One step per pulse
  digitalWrite(STEP_PIN, HIGH);
  delayMicroseconds(1000);        // the A4988 needs only about 1 µs; 1 ms is safe
  digitalWrite(STEP_PIN, LOW);
  delayMicroseconds(1000);        // 2 ms per step: about 500 steps per second
}

At 500 steps per second, a 1.8° motor at full step turns about 2.5 revolutions per second. For smooth, accelerated motion, a library such as AccelStepper handles the timing for you.

Troubleshooting

  • The motor does not move: check the power on VMOT, ENABLE is LOW, RESET and SLEEP are HIGH, the coil wires are correct, and the current limit is not set too low.
  • The motor buzzes but does not turn: the coil pairs are probably wrong. Identify them again using the stepper motor wiring guide.
  • The motor skips steps: the load is too heavy, the speed is too high, or the current limit is too low.
  • The driver overheats: lower the current limit, add a heat sink, or improve airflow.

Safety

  • Disconnect power before connecting or disconnecting the motor. Hot-plugging a stepper can damage the driver.
  • Do not exceed the motor’s rated current.
  • Keep the motor supply within the A4988’s limits, and do not run the Arduino from the motor supply.

The complete guide to electric motors

Types of Electric Motors: DC, Servo and Stepper Compared
Read the guide

Keep exploring

Guides from other topics

All topics →