How to Use A4988 Stepper Driver with Arduino: Simple Guide
To use the
A4988 stepper driver with an Arduino, connect the driver pins to the Arduino and power supply, then control the motor by sending pulses to the STEP pin and direction signals to the DIR pin. Use digital pins to toggle these signals in your Arduino code to move the motor step by step.Syntax
The basic connections and control signals for the A4988 driver with Arduino are:
- STEP pin: Receives pulses to move the motor one step per pulse.
- DIR pin: Sets the rotation direction (HIGH or LOW).
- Enable pin (optional): Enables or disables the driver output.
- Power pins: Connect motor power (VMOT) and ground, and logic power (VDD) and ground.
In Arduino code, you set the DIR pin HIGH or LOW to choose direction, then toggle the STEP pin HIGH and LOW with delays to move the motor.
arduino
const int dirPin = 2; // Direction pin const int stepPin = 3; // Step pin void setup() { pinMode(dirPin, OUTPUT); pinMode(stepPin, OUTPUT); } void loop() { digitalWrite(dirPin, HIGH); // Set direction digitalWrite(stepPin, HIGH); // Step pulse start delayMicroseconds(1000); // Wait digitalWrite(stepPin, LOW); // Step pulse end delayMicroseconds(1000); // Wait before next step }
Example
This example code shows how to rotate a stepper motor 200 steps clockwise, pause, then 200 steps counterclockwise using the A4988 driver and Arduino.
arduino
const int dirPin = 2; // Direction pin const int stepPin = 3; // Step pin void setup() { pinMode(dirPin, OUTPUT); pinMode(stepPin, OUTPUT); } void loop() { // Rotate clockwise digitalWrite(dirPin, HIGH); for (int i = 0; i < 200; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } delay(1000); // Pause 1 second // Rotate counterclockwise digitalWrite(dirPin, LOW); for (int i = 0; i < 200; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } delay(1000); // Pause 1 second }
Output
The stepper motor rotates 200 steps clockwise, pauses 1 second, then rotates 200 steps counterclockwise, and repeats.
Common Pitfalls
- Incorrect wiring: Make sure motor coils connect correctly to the A4988 outputs; reversing coils can cause no movement or noise.
- Power supply issues: Use a suitable power supply for the motor voltage and current; underpowering can stall the motor.
- Missing delays: Too fast step pulses without delays can cause missed steps or motor skipping.
- Not setting direction: Forgetting to set the
DIRpin before stepping can cause unexpected rotation direction. - Overheating: The A4988 driver needs a heat sink and proper current limiting to avoid damage.
arduino
/* Wrong way: No delay between steps causes motor to skip steps */ for (int i = 0; i < 200; i++) { digitalWrite(stepPin, HIGH); digitalWrite(stepPin, LOW); } /* Right way: Add delay for stable stepping */ for (int i = 0; i < 200; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); }
Quick Reference
Summary tips for using A4988 with Arduino:
- Connect
DIRandSTEPpins to Arduino digital outputs. - Use a separate power supply for the motor (VMOT) with common ground.
- Set
DIRHIGH or LOW before stepping to control direction. - Toggle
STEPpin HIGH then LOW with delays to move motor steps. - Adjust current limit on A4988 with potentiometer to protect motor and driver.
Key Takeaways
Connect A4988 STEP and DIR pins to Arduino digital pins to control motor steps and direction.
Use delays between STEP pulses to ensure smooth motor movement and avoid missed steps.
Provide proper power supply and set current limit on the driver to protect hardware.
Always set the direction pin before sending step pulses to control rotation direction.
Check wiring carefully to avoid coil misconnection and driver damage.