Control Stepper Motor With A4988 Driver Module
Control Stepper Motor With A4988 Driver Module
https://lastminuteengineers.com/a4988-stepper-motor-driver-arduino-tutorial/
The A4988 driver has a total of 16 pins that connect it to the outside world. The
pinout is as follows:
Power Pins
VDD and GND are used to power the internal logic circuitry, which can range from 3V
to 5.5V.
Whereas,
VMOT and GND supply power to the motor, which can range from 8V to 35V.
According to the datasheet, in order to sustain 4A, the motor supply requires a
suitable decoupling capacitor close to the board.
The A4988 driver supports microstepping by dividing a single step into smaller steps. This is
achieved by energizing the coils with intermediate current levels.
For example, if you choose to drive the NEMA 17 (with 1.8° step angle or 200
steps/revolution) in quarter-step mode, the motor will produce 800 microsteps per revolution.
The A4988 driver has three step size (resolution) selector inputs: MS1, MS2 & MS3. By
setting the appropriate logic levels for these pins, we can set the motor to one of five step
resolutions.
These three microstep selection pins are pulled LOW by internal pull-down resistors, so if you
leave them unconnected, the motor will operate in full step mode.
DIR input controls the spinning direction of the motor. Pulling it HIGH turns the motor
clockwise, while pulling it LOW turns it counterclockwise.
If you want the motor to only turn in one direction, you can connect the DIR directly to VCC
or GND.
The STEP and DIR pins are not pulled to any specific voltage, so you should not leave them
floating in your application.
Pins For Controlling Power States
The A4988 has three separate inputs for controlling its power states: EN, RST, and SLP.
EN is an active low input pin. When this pin is pulled LOW, the A4988 driver is enabled. By
default, this pin is pulled low, so unless you pull it high, the driver is always enabled. This pin
is particularly useful when implementing an emergency stop or shutdown system.
SLP is an active low input pin. Pulling this pin LOW puts the driver into sleep mode, reducing
power consumption to a minimum. You can use this to save power, especially when the motor
is not in use.
RST is an active low input as well. When this pin is pulled LOW, all STEP inputs are ignored.
It also resets the driver by setting the internal translator to a predefined “home” state. Home
state is basically the initial position from which the motor starts, and it varies based on
microstep resolution.
RST is a floating pin. If you’re not using this pin, connect it to an adjacent SLP/SLEEP pin to
make it high and enable the driver.
After the wake-up event (logic HIGH on the SLEEP pin), wait 1 millisecond before issuing a
Step command to allow the charge pump to stabilize.
Output Pins
The output channels of the A4988 motor driver are broken out to the side of the module with
pins 1B, 1A, 2A & 2B.
You can connect any small to medium-sized bipolar stepper motor, such as NEMA 17, to
these pins.
Each output pin can supply up to 2A to the motor. However, the amount of current supplied to
the motor is determined by the power supply, cooling system, and current limiting setting of
the system.
Excessive power dissipation of the A4988 driver IC causes a temperature rise, which could
potentially damage the IC if it exceeds its capacity.
Despite having a maximum current rating of 2A per coil, the A4988 driver IC can only supply
about 1A per coil without overheating. To achieve more than 1A per coil, a heat sink or other
cooling method is required.
Usually, the A4988 driver comes with the heatsink. It is recommended that you install the
heatsink before using the driver.
Current limiting
Before running the motor, you must limit the maximum current flowing through the stepper
coils so that it does not exceed the motor’s rated current.
The A4988 driver includes a small trimmer potentiometer for setting the current limit.
Method 1:
In this method, the current limit is determined by measuring the voltage (Vref) at the “ref”
pin.
1. Take a look at the datasheet for your stepper motor. Make a note of the rated current. In our
case, NEMA 17 200steps/rev, 12V 350mA is used.
2. Disconnect the three microstep selection pins to put the driver in full-step mode.
3. Hold the motor in a fixed position without clocking the STEP input.
4. Measure the voltage (Vref) on the metal trimmer pot as you adjust it.
For example, if your motor is rated at 350mA, you would set the reference voltage to 0.14V.
You can make this adjustment quickly and easily by connecting one end of the alligator clip
test lead to the shank of a metal screwdriver and the other end to your multimeter. This allows
you to measure the voltage while making the adjustment.
Method 2:
In this method, the current limit is determined by measuring the current flowing through the
coil.
1. Take a look at the datasheet for your stepper motor. Make a note of the rated current. In our
case, NEMA 17 200steps/rev, 12V 350mA is used.
2. Disconnect the three microstep selection pins to put the driver in full-step mode.
3. Hold the motor in a fixed position without clocking the STEP input. Don’t leave the STEP
input floating; instead, connect it to a logic power supply (5V).
4. Put the ammeter in series with one of the coils on your stepper motor and measure the
actual current flowing.
5. Take a small screwdriver and adjust the current limit potentiometer until you reach the rated
current.
If you ever change the logic voltage (VDD), you will have to redo this adjustment.
Wiring an A4988 Stepper Motor Driver to an Arduino
Now that we know everything about the driver, let’s hook it up to our Arduino.
The connections are straightforward. Begin by connecting VDD and GND (next to VDD) to
the Arduino’s 5V and Ground pins. Connect the DIR and STEP input pins to the Arduino’s
digital output pins #2 and #3.
Connect the stepper motor to the 2B, 2A, 1A, and 1B pins. Actually, the A4988 module is
conveniently laid out to match the 4-pin connector on bipolar stepper motors, so that
shouldn’t be a problem.
Warning:
Do not attempt to connect or disconnect the stepper motor while the driver is running; doing
so could damage the driver.
To keep the driver enabled, connect the RST pin to the adjacent SLP/SLEEP pin. Keep the
microstep selection pins disconnected if you want to run the motor in full step mode.
Finally, connect the motor power supply to the VMOT and GND pins. Remember to put a
large 100μF decoupling electrolytic capacitor across the motor power supply pins to avoid
large voltage spikes.
Arduino Code – Without a Library
The sketch below will show you how to control the speed and spinning direction of a bipolar
stepper motor using the A4988 stepper motor driver and can serve as the basis for more
practical experiments and projects.
void setup()
{
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop()
{
// Set motor direction clockwise
digitalWrite(dirPin, HIGH);
The sketch begins by defining the Arduino pins to which the A4988’s STEP and DIR pins are
connected. A variable called stepsPerRevolution is also defined. You can set it to match
the specs of your stepper motor.
In the setup section, all motor control pins are configured as digital OUTPUT.
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
In the loop section, the motor is rotated slowly clockwise and then rapidly counterclockwise
with one second intervals.
Controlling the Spinning Direction: To control the spinning direction of the motor, the DIR
pin is set HIGH or LOW. A HIGH input turns the motor clockwise, while a LOW input turns
it counterclockwise.
digitalWrite(dirPin, HIGH);
Controlling Speed: The frequency of pulses sent to the STEP pin determines the speed of the
motor. The higher the pulse frequency, the faster the motor runs. A pulse is simply pulling the
output HIGH, waiting a few milliseconds, then pulling it LOW and waiting again. By
adjusting the delay, you can alter the frequency of the pulses and thus the speed of the motor.
Controlling a stepper without a library is perfectly fine for simple, single motor applications.
However, if you want to control multiple steppers, you’ll need to use a library.
So, for our next experiment, we will use an advanced stepper motor library
called AccelStepper library. It supports:
This library is not included in the Arduino IDE, so you must first install it.
Library Installation
To install the library navigate to Sketch > Include Libraries > Manage Libraries… Wait for
Library Manager to download the library index and update the list of installed libraries.
Filter your search by typing ‘accelstepper’. Click on the first entry and then select Install.
Arduino Code
Here is a simple sketch that accelerates the stepper motor in one direction and then decelerates
to come to rest. After one revolution, the motor reverses its spinning direction and repeats the
process.
// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(1000);
myStepper.setAcceleration(50);
myStepper.setSpeed(200);
myStepper.moveTo(200);
}
void loop() {
// Change direction once the motor reaches target position
if (myStepper.distanceToGo() == 0)
myStepper.moveTo(-myStepper.currentPosition());
#include <AccelStepper.h>
First, the Arduino pins are defined, to which the A4988’s STEP and DIR pins are connected.
The motorInterfaceType is also set to 1. (1 means an external stepper driver with step and
direction pins).
In the setup function, the maximum permitted speed of the motor is set to 1000 (the motor
will accelerate up to this speed when we run it). The acceleration/deceleration rate is then set
to add acceleration and deceleration to the stepper motor’s movements.
The constant speed is set to 200. And, because the NEMA 17 takes 200 steps per turn, the
target position is also set to 200.
void setup() {
myStepper.setMaxSpeed(1000);
myStepper.setAcceleration(50);
myStepper.setSpeed(200);
myStepper.moveTo(200);
}
In the loop function, an If statement is used to determine how far the motor needs to travel
(by reading the distanceToGo property) before reaching the target position (set by moveTo ).
When the distanceToGo reaches zero, the motor is rotated in the opposite direction by
setting the moveTo position to negative of its current position.
At the bottom of the loop, you’ll notice that the run() function is called. This is the most
critical function because the stepper will not move unless this function is executed.
void loop() {
if (myStepper.distanceToGo() == 0)
myStepper.moveTo(-myStepper.currentPosition());
myStepper.run();
}