How to control a stepper motor with A4988 driver and Arduino

A4988 Arduino stepper motor wiring schematic diagram pinout

This article includes everything you need to know about controlling a stepper motor with the A4988 stepper motor driver and Arduino. I have included a wiring diagram, a tutorial on how to set the current limit and many example codes. 

Although you can use this driver without an Arduino library, I highly recommend you also take a look at the example code for the AccelStepper library at the end of this tutorial. This library is fairly easy to use and can greatly improve the performance of your hardware.

What this article covers

If you have an DRV8825 stepper motor driver instead of the A4988 or want to know different ways to control stepper motors, take a look at the articles below.

Other stepper motor tutorials:

About the driver

At the heart of the driver you will find a chip made by Allegro MicroSystems: the A4988 DMOS Microstepping Driver with Translator and Overcurrent Protection. This integrated motor driver makes interfacing with a microcontroller super easy as you only need two pins to control both the speed and the direction of the stepper motor. 

The driver has a maximum output capacity of 35 V and ± 2 A which is great for driving small to medium sized stepper motors like a NEMA 17 bipolar stepper motor.

The chip has several safety functions built-in like overcurrent, short circuit, under voltage lockout and over temperature protection. You can find more specifications in the table below.

A4988 Specifications

A4988Specifications
Minimum operating voltage8 V
Maximum operating voltage35 V
Continuous current per phase1 A
Maximum current per phase2 A
Minimum logic voltage3 V
Maximum logic voltage5.5 V
Microstep resolutionfull, 1/2, 1/4, 1/8 and 1/16
Reverse voltage protection?No
Dimensions15.5 × 20.5 mm (0.6" × 0.8")

For more information you can check out the datasheet here.

Differences between A4988 and DRV8825

The DRV8825 is quite similar to the A4988 but there are some key differences:

  • The DRV8825 offers 1/32 microstepping, whereas the A4988 only goes down to 1/16-step. Higher microstepping results in smoother, quieter operation but is not always needed.
  • The current limit potentiometer is at a different location
  • The relation between the reference voltage and the current limit is different.
  • The DRV8825 requires a minimum STEP pulse duration of 1.9µs; the A4988 requires 1µs minimum.
  •  The DRV8825 can be used with higher voltage motor power supply (45 V vs 35 V). This means it is less susceptible to damage from LC voltage spikes.
  •  The DRV8825 can deliver slightly more current than the A4988 without any additional cooling.
A4988 vs DRV8825 stepper motor driver
A4899 (left) vs DRV8825 (right)

The pinout of the DRV8825 is exactly the same as for the A4988, so it can be used as a drop in replacement!

Microstep settings

Stepper motors typically have a step size of 1.8° or 200 steps per revolution, this refers to full steps. A microstepping driver such as the A4988 allows higher resolutions by allowing intermediate step locations. This is achieved by energizing the coils with intermediate current levels. 

For instance, driving a motor in quarter-step mode will give the 200-step-per-revolution motor 800 microsteps per revolution by using four different current levels.

A4988 Connections pinout microstepping pins
A4988 Pinout

The resolution (step size) selector pins (MS1, MS2, and MS3) allow you to select one of the six step resolutions according to the table below.

M0M1M2Microstep resolution
LowLowLowFull step
HighLowLow1/2 step
LowHighLow1/4 step
HighHighLow1/8 step
HighHighHigh1/16 step

All three inputs have internal 100kΩ pull-down resistors, so leaving the three microstep selection pins disconnected results in full-step mode.

I often use a CNC-shield or expansion board in combination with these drivers. The expansion board has 3 dip switches to set MS1 – MS3 high or low and on the CNC-shield you can install jumpers. If you are using the driver with a breadboard, you can just use jumper wires to connect the selector pins to 5V.

Things used in this tutorial:

Hardware components

Parts are available on Amazon via the links below. You can click the plus sign on the right for additional information about the specific item.
NameQuantityDescriptionBuy
Arduino UNO R3x 1If you want to control many stepper motors, you can use an Arduino Mega.Amazon
A4988 stepper motor driverx 1The color of the PCB can be different, depending on the manufacturer.Amazon
NEMA 17 stepper motorx 1In this tutorial I used a basic bipolar stepper motor with 4 wires.Amazon
Breadboardx 1I highly recommend to buy at least 1 good quality breadboard like the BusBoard Prototype Systems BB400 or BB830.Amazon
Jumper wires~ 10Male/female and male/male needed.Amazon
USB Type-B cablex 1Used to power the Arduino UNOAmazon
100µF capacitorx 1With correct voltage rating.Amazon
Power supply (8-35V)x 1I recommend a 12V or 24V power supply.Amazon

Tools

NameDescriptionBuy
MultimeterAmazon
Small screwdriverI like the Wiha PicoFinish.Amazon
Alligator test leads(Optional but very handy)Amazon

Software

PictureNameLink
Arduino-LogoArduino IDE

As I mentioned earlier, I like to use this driver in combination with a CNC-shield or expansion board. Such a shield already includes capacitors and offers an easy way to select the microstepping resolution. It makes wiring much easier and is a great option if you need a more permanent solution than a breadboard.

*Note: the above links are affiliated which means – at no additional cost to you – if you purchase anything using them you’re helping to support my work on this site. My humble thanks (I really appreciate it)! 

Wiring – Connecting A4988 to Arduino and stepper motor

A4988 Arduino stepper motor wiring schematic diagram pinout
Wiring diagram/schematic for A4988 stepper motor driver with Arduino and stepper motor.

The wiring diagram/schematic above shows you how to connect the A4899 driver to a stepper motor and the Arduino. 

The connections are also given in the following table:

A4988 Connections

A4988Connection
VMOT8-35V
GNDMotor ground
SLPRST
RSTSLP
VDD5V
GNDLogic ground
STPPin 3
DIRPin 2
A1, A2, B1, B2Stepper motor
  • The motor power supply is connected to GND and VMOT (top right).
  • The two coils of the stepper motor are connected to A1, A2 and B1, B2 (see below).
  • The GND pin (lower right) is connected to the ground pin of the microcontroller and VDD is connected to 5V.
  • The STP (step) and DIR (direction) pin are connected to digital pin 3 and 2 respectively. You can choose a different digital pin if you want, but these are the ones I used for this tutorial and the example code.
  • The SLP Pin is an active low input. Meaning, pulling this pin low puts the driver in sleep mode, minimizing the power consumption. RST is also an active low input. When pulled low, all STEP inputs are ignored, until you pull it high. If you are not using the pin, you can connect it to the adjacent SLP/SLEEP pin to bring it high and enable the driver.
  • The EN (enable) pin can be left disconnected, it is pulled low by default. When this pin is set high the driver is disabled.

Warning

The A4988 carrier board uses low-ESR ceramic capacitor, which makes it susceptible to destructive LC voltage spikes, especially when using power leads longer than a few inches.

 To protect the driver you can connect an electrolytic capacitor between VMOT and GND. Pololu suggests a capacitor of 47µF or more (I used a 100 µF capacitor). I like these assortment boxes from Amazon, this way I always have some capacitors of the right size on hand.

Connect the positive lead of the capacitor to the VMOT wire connection and the negative lead to the GND wire connection (see picture). Capacitors are often marked with a stripe which indicates the negative lead. The positive lead is often longer then the negative lead.

In the rest of this tutorial I have left MS1, MS2 and MS3 disconnected, so the driver operates in full step mode. This makes explaining the code a bit easier. Normally I would use 1/8 or 1/16 microstepping and connect the appropriate pins to 5V (see table in introduction).

How to determine the stepper motor wiring?

If you can’t find the datasheet of your stepper motor, it can be difficult to figure out how to wire your motor correctly. I use the following trick to determine how to connect 4 wire bipolar stepper motors:

The only thing you need to identify is the two pairs of wires which are connected to each of the two coils. One coil gets connect to 1A and 1B and the other to 2A and 2B, the polarity doesn’t matter.

To find the two wires from one coil, do the following with the motor disconnected:

  1. Try to spin the shaft of the stepper motor by hand and notice how hard it is to turn.
  2. Now pick a random pair of wires from the motor and touch the bare ends together.
  3. Next, try to spin the shaft of the stepper motor again.

If you feel a lot of resistance, you have found a pair of wires from the same coil. If you can spin the shaft freely, try another pair of wires. Now connect the two coils to the pins shown in the wiring diagram above. 

(If it is still unclear, please leave a comment below, more info can also be found on the RepRap.org wiki)

How to set the current limit?

Before you start programming your Arduino and start using the driver there is one very important thing you need to do that a lot of people forget: set the current limit!

This step is not very complicated but absolutely necessary to protect your stepper motor and the driver. If you do not set an appropriate current limit, your motor can draw more current than it or your driver can handle, this is likely to damage one or both of them.

To set the current limit you need to measure a reference voltage and adjust the on-board potentiometer accordingly. You will need a small screwdriver, a multimeter to measure the reference voltage and alligator test leads (optional but very handy).

A4988 current limit wiring diagram schematic
Current limit wiring diagram for A4988 driver.

To measure the reference voltage, the driver needs to be powered. The A4988 only needs power via VDD (5V) and you need to connect RST and SLP together, otherwise the driver won’t turn on. It’s best to disconnect the stepper motor while you do this. 

If you have already wired up the driver, you can leave everything but the stepper motor connected. You can apply power through the USB port of the Arduino.

A4988Connection
VDD5V
RSTSLP
SLPRST
GNDGround

Current limit formula

The next step is to calculate the current limit with the following formula:

Current Limit = Vref ÷ (8 × Rcs)

The Rcs is the current sense resistance. If you bought a A4988 driver from Pololu before January 2017, the Rcs will be 0.050 Ω. Drivers sold after that have 0.068 Ω current sense resistors.

.To select the right current limit, take a look at the datasheet of your stepper motor. If you can’t find the current rating of your motor, I recommend starting with a current limit of 1A. You can always increase it later if your motor/driver is missing steps.

So this means that for a current limit of 1A for a board with 0.068  Ω current sense resistors, the Vref should be 540 mV .

Current-sense-resistor-locations-for-A4988-stepper-motor-driver-Pololu
Current sense resistor locations. Picture: www.pololu.com

Bonus info: When using the driver in full-step mode, the current through each coil is limited to approximately 70% of the set current limit. This means that you would need to set the current limit 40% higher or 1.4 A in full-step mode. When using microstepping, the formula above applies.

Note that you need to re-calibrate the current limit if you change the motor power supply voltage. If your motor is making a lot of noise, try to lower the current limit. It’s best to set the current limit just high enough so the motor doesn’t miss steps.

Measuring Vref

Now you will need to measure the reference voltage (Vref) between the two points marked on the picture below (GND and the potentiometer) and adjust it to the value you calculated.

A4988 Current limit probe points
Vref probe points (GND and potentiometer)

I recommend using alligator test leads clamped to the screwdriver to set the current limit. This allows you to adjust the potentiometer and measure the reference voltage at the same time.

Note: There is another way to measure the current limit and that is to directly measure the current draw of the stepper motor. Personally I find the above method a lot easier. 

Pololu mentions the following on their website: “Note: The coil current can be very different from the power supply current, so you should not use the current measured at the power supply to set the current limit. The appropriate place to put your current meter is in series with one of your stepper motor coils.”

Current limit FAQ

Do I need to have the stepper motor connected or not?
No, you don’t need to connect te stepper motor to the driver when setting the current limit. To be on the safe side, disconnect your motor, it sometimes interferes with measuring the Vref voltage.

Do I need to turn the motor by running the Arduino motor sketch?
No, see question above.

Do I need to turn the potentiometer clock- or counterclockwise to raise Vref?
This depends on the manufacturer of the driver. If you have genuine Polulu breakout boards of the DRV8825 or A4988 you turn the potentiometer clockwise to raise Vref and counterclockwise to lower it.

Cooling the driver

The A4988 driver IC has a maximum current rating of 2 A per coil, but without a heat sink it can only supply about 1 A per coil before it starts to overheat.

A4988-with-heatsink-Arduino-tutorial
A4988 with adhesive-backed heatsink.

The driver usually comes with a small adhesive-backed heat sink, which I recommend you to install right away. You can also buy a bunch of small heat sinks from Amazon for really cheap.

Basic Arduino example code to control a stepper motor

Now that you have wired up the driver and set the current limit, it is time to connect the Arduino to the computer and upload some code. You can upload the following example code to your Arduino using the Arduino IDE

This sketch controls both the speed, the number of revolutions and the spinning direction of the stepper motor.

You can open the code in a new window by clicking on the button in the top right corner.

/*Example sketch to control a stepper motor with A4988 stepper motor driver and Arduino. More info: https://www.makerguides.com */

// Define stepper motor connections and steps per revolution:
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200

void setup()
{
  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop()
{
  // Set the spinning direction clockwise:
  digitalWrite(dirPin, HIGH);

  // Spin the stepper motor 1 revolution slowly:
  for(int i = 0; i < stepsPerRevolution; i++)
  {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
  
  delay(1000);
  
  // Set the spinning direction counterclockwise:
  digitalWrite(dirPin, LOW);

  // Spin the stepper motor 1 revolution quickly:
  for(int i = 0; i < stepsPerRevolution; i++)
  {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  
  delay(1000);
  
  // Set the spinning direction clockwise:
  digitalWrite(dirPin, HIGH);

  // Spin the stepper motor 5 revolutions fast:
  for(int i = 0; i < 5*stepsPerRevolution; i++)
  {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH); 
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  
  delay(1000);

  // Set the spinning direction counterclockwise:
  digitalWrite(dirPin, LOW);

  //Spin the stepper motor 5 revolutions fast:
  for(int i = 0; i < 5*stepsPerRevolution; i++)
  {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  
  delay(1000);
}

How the code works:

This Arduino example code is not very complicated and you don’t need any libraries to get it working.

The sketch starts with defining the step and direction pins. I connected them to Arduino pin 3 and 2. 

The statement #define is used to give a name to a constant value. The compiler will replace any references to this constant with the defined value when the the program is compiled. So everywhere you mention dirPin, the compiler will replace it with the value 2 when the program is compiled.

I also defined a stepsPerRevolution constant. Because I set the driver to full step mode I set it to 200 steps per revolution. Change this value if your setup is different.

// Define stepper motor connections and steps per revolution:
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200

In the setup() section of the code, all the motor control pins are declared as digital OUTPUT with the function pinMode().

void setup()
{
  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

In the loop() section of the code, we let the motor spin one revolution slowly in the CW direction and one revolution quickly in the CCW direction. Next we let the motor spin 5 revolutions in each directions with a high speed. So how do you control the speed, spinning direction and number of revolutions?

  // Set the spinning direction clockwise:
  digitalWrite(dirPin, HIGH);

  // Spin the stepper motor 1 revolution slowly:
  for(int i = 0; i < stepsPerRevolution; i++)
  {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }

Control spinning direction:

To control the spinning direction of the stepper motor we set the DIR (direction) pin either HIGH or LOW. For this we use the function digitalWrite(). Depending on how you connected the stepper motor, setting the DIR pin high will let the motor turn CW or CCW.

Control number of steps or revolutions:

In this example sketch, the for loops control the number of steps the stepper motor will take. The code within the for loop results in 1 step of the stepper motor. Because the code in the first two loops is executed 200 times (stepsPerRevolution), this results in 1 revolution. In the last two loops, the code within the for loop is executed 1000 times, which results in 1000 steps or 5 revolutions. 

Note that you can change the second term in the for loop to whatever number of steps you want. for(int i = 0; i < 100; i++) would result in 100 steps, or half a revolution.

Control speed:

The speed of the stepper motor is determined by the frequency of the pulses we send to the STEP pin. The higher the frequency, the faster the motor runs. You can control the frequency of the pulses by changing  delayMicroseconds() in the code. The shorter the delay, the higher the frequency, the faster the motor runs.

AccelStepper library tutorial

The AccelStepper library written by Mike McCauley is an awesome library to use for your project. One of the advantages is that it supports acceleration and deceleration, but it has a lot of other nice functions too. 

You can download the latest version of this library here or click the button below.

You can install the library by going to Sketch > Include Library > Add .ZIP Library… in the Arduino IDE.

Another option is to navigate to Tools > Manage Libraries… or type Ctrl + Shift + I on Windows. The Library Manager will open and update the list of installed libraries.

Installing an Arduino library step 1 open Library Manager

You can search for ‘accelstepper‘ and look for the library by Mike McCauley. Select the latest version and then click Install.

Installing an Arduino library step 2 AccelStepper

1. Continuous rotation example code

The following sketch can be used to run one or more stepper motors continuously at a constant speed. (No acceleration or deceleration is used).

/*Example sketch to control a stepper motor with A4988 stepper motor driver AccelStepper library and Arduino. More info: https://www.makerguides.com */

#include <AccelStepper.h>

//Define stepper motor connections
#define dirPin 2
#define stepPin 3

//Create stepper object
AccelStepper stepper(1,stepPin,dirPin); //motor interface type must be set to 1 when using a driver.


void setup()
{
  stepper.setMaxSpeed(1000); //maximum steps per second
}

void loop()
{
  stepper.setSpeed(400); //steps per second
  stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
}

Code explanation

After including the library, the first step is to define the step and direction pins, just like before. Next, you need to create a stepper object with the appropriate motor interface type and step and direction pins. Because we are using a driver, we set the motor interface type to 1. You can find the other interface types here.

In this case I called the stepper motor ‘stepper’ but you can use other names as well, like ‘stepper1’ or ‘liftmotor’ etc. AccelStepper liftmotor(1,stepPin,dirPin);. You can create multiple stepper motor objects with different names and step and direction pins. This allows you to easily control 2 or more stepper motors at the same time.

In the setup() section of the code we define the maximum stepper motor speed in steps/second. Speeds of more than 1000 steps per second are unreliable. This means that if you are using microstepping, you won’t be able to run the motor extremely fast.

In the loop() we first set the speed that the motor should run at (you can also place this in the setup section of the code) and then we let the motor run. Stepper.runSpeed() polls the motor and when a step is due, executes 1 step. This depends on the set speed and the time since the last step. If you want to change the direction of the motor, you can set a negative speed: stepper.setSpeed(-400);

2. Code to control number of steps or revolutions

To let the motor rotate a specific number of steps I prefer to use a while loop in combination with stepper.currentPosition(). If you replace the loop with the following code, you can let the motor run back and forth.

void loop()
{ 
  stepper.setCurrentPosition(0); //set the current position to 0

  //run the motor forward at 200 steps/second until the motor reaches 400 steps (2 revolutions).
  while(stepper.currentPosition() != 400)
  {
    stepper.setSpeed(200);
    stepper.runSpeed();
  }

  stepper.setCurrentPosition(0); //reset the position
  delay(1000);

  //run the motor backwards at 600 steps/second until the motor reaches -200 steps (1 revolution).
  while(stepper.currentPosition() != -200) 
  {
    stepper.setSpeed(-600);
    stepper.runSpeed();
  }

  stepper.setCurrentPosition(0);
  delay(1000);

  while(stepper.currentPosition() != 600)
  {
    stepper.setSpeed(400);
    stepper.runSpeed();
  }

  delay(3000);
}

The while loop checks the current motor position in steps with stepper.currentPosition() and as long as it is not equal to 400 (!= 400), the microcontroller will step the motor with a speed of 200 steps per second. Next, the motor position is reset to zero. You don’t have to do this necessarily, but I find this convenient when doing multiple rotations in a row. 

The next example shows another easy way to control the position of the motor with acceleration and deceleration.

3. Acceleration and deceleration example code

With the following sketch you we can add acceleration and deceleration to the movements without any complicated coding. You can replace the setup and loop section of the code with the following example. The motor will run back and forth with a speed of 200 steps per second and an acceleration of 30 steps per second per second.

void setup()
{
  stepper.setMaxSpeed(200); //maximum steps per second
  stepper.setAcceleration(30);
}

void loop()
{
  stepper.moveTo(600);
  stepper.runToPosition();

  delay(1000);

  stepper.moveTo(0);
  stepper.runToPosition();

  delay(1000);
}

In this sketch I start by adding an acceleration with stepper.setAcceleration(). Note that I changed the maximum speed to 200 steps per second for this example. Next I used stepper.moveTo() to set the target position. The function stepper.runToPostion() moves the motor (with acceleration/deceleration) to the target position and blocks until it is at position. Because this function is blocking, you shouldn’t use this when you need to control other things at the same time.

Conclusion

In this article I have shown you how to control a stepper motor with the A4988 stepper motor driver and Arduino. I hope you found it useful and informative. If you did, please share it with a friend who also likes electronics and making things!

I have personally used this driver a lot for a bunch of 3D printers and other CNC related projects but I would love to know what projects you plan on building (or have already built) with this driver. Crazy robots, drawings machines, automation projects… Please leave a comment down below and share your ideas.

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

Beginner

Creative Commons License

If you have any questions, suggestions or if you think that things are missing in this tutorial, please leave a comment down below. Note that comments are held for moderation in order to prevent spam.

2 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *