Arduino Stepper Motor with A4988: Wiring, Code and Simulator

To build this Arduino Stepper Motor with A4988 project you need an Arduino Uno and an A4988 stepper driver and a stepper motor. STEP and DIR pulses on pins 3 and 4 turn a 200-step stepper through an A4988 driver. Make the 11 connections in the wiring table, upload the Arduino C++ code, and watch the shaft: 100 steps is half a turn, 50 back is a quarter.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1A4988 stepper driverEach rising edge on STEP moves one full step; DIR picks the way. ENABLE LOW, SLEEP and RESET HIGH, VDD 5V. Wire 1A/1B to A+/A−, 2A/2B to B+/B−.
1Stepper MotorA bipolar stepper, 200 steps a turn. Drive coil A (A+/A−) and coil B (B+/B−) in sequence, usually from an A4988.

Wiring

#FromTo
1Arduino Uno pin 3A4988 stepper driver STEP
2Arduino Uno pin 4A4988 stepper driver DIR
3Arduino Uno GNDA4988 stepper driver ENABLE
4A4988 stepper driver RESETA4988 stepper driver SLEEP
5Arduino Uno 5VA4988 stepper driver SLEEP
6Arduino Uno 5VA4988 stepper driver VDD
7Arduino Uno GNDA4988 stepper driver GND
8A4988 stepper driver 1AStepper Motor A+
9A4988 stepper driver 1BStepper Motor A-
10A4988 stepper driver 2AStepper Motor B+
11A4988 stepper driver 2BStepper Motor B-

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add an A4988 stepper driver and a stepper motor from the parts palette.
  3. Wire 1. Connect Arduino Uno pin 3 to A4988 stepper driver STEP.
  4. Wire 2. Connect Arduino Uno pin 4 to A4988 stepper driver DIR.
  5. Wire 3. Connect Arduino Uno GND to A4988 stepper driver ENABLE.
  6. Wire 4. Connect A4988 stepper driver RESET to A4988 stepper driver SLEEP.
  7. Wire 5. Connect Arduino Uno 5V to A4988 stepper driver SLEEP.
  8. Wire 6. Connect Arduino Uno 5V to A4988 stepper driver VDD.
  9. Wire 7. Connect Arduino Uno GND to A4988 stepper driver GND.
  10. Wire 8. Connect A4988 stepper driver 1A to Stepper Motor A+.
  11. Wire 9. Connect A4988 stepper driver 1B to Stepper Motor A-.
  12. Wire 10. Connect A4988 stepper driver 2A to Stepper Motor B+.
  13. Wire 11. Connect A4988 stepper driver 2B to Stepper Motor B-.
  14. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  15. Run it. Press Run. Watch the shaft: 100 steps is half a turn, 50 back is a quarter.

Arduino C++ code

// Stepper motor through an A4988 driver: STEP on pin 3, DIR on pin 4.
// Each pulse on STEP is one step; 200 steps turn the shaft once.
const int STEP = 3, DIR = 4;

void turn(int steps, bool clockwise) {
  digitalWrite(DIR, clockwise ? HIGH : LOW);
  for (int i = 0; i < steps; i++) {
    digitalWrite(STEP, HIGH);
    delayMicroseconds(800);
    digitalWrite(STEP, LOW);
    delayMicroseconds(800);
  }
}

void setup() {
  pinMode(STEP, OUTPUT);
  pinMode(DIR, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.println("Half a turn clockwise");
  turn(100, true);
  delay(500);
  Serial.println("Quarter turn back");
  turn(50, false);
  delay(500);
}

What to try

Watch the shaft: 100 steps is half a turn, 50 back is a quarter.

Frequently asked questions

How do I wire the Stepper Motor with A4988 project on an Arduino Uno?

Make 11 connections: Arduino Uno pin 3 to A4988 stepper driver STEP; Arduino Uno pin 4 to A4988 stepper driver DIR; Arduino Uno GND to A4988 stepper driver ENABLE; A4988 stepper driver RESET to A4988 stepper driver SLEEP; Arduino Uno 5V to A4988 stepper driver SLEEP; Arduino Uno 5V to A4988 stepper driver VDD; Arduino Uno GND to A4988 stepper driver GND; A4988 stepper driver 1A to Stepper Motor A+; A4988 stepper driver 1B to Stepper Motor A-; A4988 stepper driver 2A to Stepper Motor B+; A4988 stepper driver 2B to Stepper Motor B-.

What parts do I need for this Arduino project?

An Arduino Uno and an A4988 stepper driver and a stepper motor.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

STEP and DIR pulses on pins 3 and 4 turn a 200-step stepper through an A4988 driver.

More Arduino Uno projects