DRV8825 Stepper Motor Driver With Arduino.

DRV2588 Stepper Motor driver with Arduino

The DRV8825 stepper motor driver like the A4988 driver is used for controlling small and medium-sized bipolar stepper motors like NEMA 17 stepper motor used in applications that require precise stepper motor control like 3D printers and CNC machines. 

I also have tutorials on other stepper motor drivers that you can check out using the links below;

DRV8825 Stepper motor driver hardware overview

This motor driver is based on the DRV8825 Microstepping Driver IC from Texas Instruments with simple STEP/DIR interface, multiple decay modes and internal shutdown functions provided for Thermal Shutdown (TSD) overcurrent, short circuit, under voltage lockout and VM Undervoltage Lockout (UVLO) over temperature.

Specifications

  • Operating voltage of 8.2 – 45V
  • Continuous current per phase of 1.5A and Maximum current per phase of 2.2A
  • Logic voltage of 2.5 – 5.25V
  • Microstep resolution of full, 1/2, 1/4, 1/8, 1/16 and 1/32
  • No reverse voltage protection

DRV8825 Stepper Motor driver Pinout

Just like the A4988 stepper motor driver, DRV8825 stepper motor driver board has a total of 16 pins as shown below:

  •  GND and VMOT on the top right side are the connections for the motor power supply taking on 8V – 45V depending on the motor you are using.
  • GND on the lower right side is connected to ground pin of the microcontroller. Unlike the A4988 driver, the DRV8825 module doesn’t have a logic power supply pin because it draws its power from the motor power supply using an internal 3V3 voltage regulator.
  • A1, A2, B1, B2  are output pins connected to the bipolar stepper motor coils. One coil of the stepper motor goes through A1 and A2, and the other through B1 and B2.
  • FAULT – this output drives LOW whenever the H-bridge FETs are disabled as the result of over-current protection or thermal shutdown. The Fault pin is typically shorted to the SLEEP pin; therefore, whenever the Fault pin is driven LOW, the entire chip is disabled. And it will remain disabled until it is either RESET or Motor Voltage VMOT is removed and reapplied.
  • DIR – used to determine the direction of rotation of a stepper motor. The motor spins clockwise or counter-clockwise depending on whether this pin is pulled LOW or HIGH.
  • STEP – for controlling the microsteps of the motor by sending HIGH pulses to this pin. The faster the pulse, the faster the motor will spin.
  • SLEEP – is an active low input. Pulling this pin LOW puts the driver into sleep mode, minimizing power consumption especially when the motor is not in use to save power.
  • RESET – is also an active low input and when activated, it ignores all inputs sent to the “STEP” pin until deactivated. This pin is floating and if you are not using this pin, you can connect it to the SLEEP pin to make it high and enable the driver.
  • ENABLE – This pin active low and is enabled by default so the driver is always enabled unless you pull it high.
  • M0, M1, M2 – Microstep selection pins used to set the microstep resolution depending on the different combinations of inputs as detailed in a later section. These pins are pulled LOW by default using on-board pull-down resistors, therefore if you leave them unconnected, the motor will operate in full step mode.

The DRV8825 driver comes with a heatsink and it is good practice to install it before using the driver. This is because the DRV8825 driver IC has a maximum current rating of 2.5A per coil but if more than 1.5A per coil is supplied, a lot of heat is dissipated which can eventually damage the IC. Therefore to get more than 1A per coil, a heat sink is required.

DRV8825 Microstepping Settings

Stepper motors normally make 200 steps per revolution in full-step mode which gives a step size of1.8°. Motor drivers that supports microstepping mode like DRV8825 allow higher resolutions for stepper motors by allowing intermediate step locations through energizing the motor phases with intermediate current levels.

For example, when in ½ step mode, the stepper motor will make 400 microsteps per revolution and in ¼ step mode it will make 800 microsteps per revolution.

Microstep settings in DRV8825 can be changed by setting the appropriate logic levels for M0, M1 and M2 pins to give one of five step resolutions as indicated below.

M0M1M2Microstep resolutionSteps per Revolution
LowLowLowFull step200
HighLowLow1/2 step400
LowHighLow1/4 step800
HighHighLow1/8 step1600
LowLowHigh1/16 step3200
HighLowHigh1/32 step6400
LowHighHigh1/32 step6400
HighHighHigh1/32 step6400

Differences between DRV8825 and A4988

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

DRV8825A4988
Operating Voltage8.2 ~ 45V8 ~ 35V
Maximum Microstep resolution1/32 step1/16
Minimum pulse durations on the STEP pin1.9 µs1 µs
Maximum current per phase1.5A1.2A
Current limit settingVREF = Imax / 2VREF = Imax .8. Rs
SLEEP pinNot pulled up by defaultPulled up by default

Other notable differences are;

  • The current limit potentiometer is in a different location.
  • The pin used to supply logic voltage to the A4988 is used as the DRV8825’s FAULT output, since the DRV8825 does not require a logic supply

Connecting DRV8825 to Arduino and stepper motor.

The DRV88 stepper motor driver can be connected to a microcontroller like Arduino to control the speed, number of revolutions and direction of rotation of a stepper motor. It very important set the current limit before using this driver.

How to set the current limit of DRV8825 stepper motor driver.

It is crucial to set the current limit to prevent the current flowing through the stepper motor coils from exceeding the motor’s rated current. Excess current will overheat and eventually damage the motor and driver.

Also the best speed and torque for a given motor are achieved when the motor is running at its rated current.

The current limit can be got by measuring the voltage reference which corresponds to the maximum current that will flow to your stepper motor.

Reference Voltage is calculated from the formula

VREF = Imax / 2

Where 

  • Imax is the maximum motor current which can be found on the motor’s datasheet.

After calculating the Reference Voltage, you can now make adjustments to the motor driver by measuring the reference voltage between the driver’s potentiometer screw and ground. The connection is done as shown below where 5V is supplied to the SLEEP and RESET pins. The DRV8825 should also be powered via VMOT.

Schematic for setting current limit of DRV2588 Stepper motor driver

Next you need to adjust the reference voltage. You can use a small screw driver to connect the motor driver’s potentiometer to the positive lead of a multimeter using an alligator lead.

Set your multimeter to the DC voltage measurement and then place the head of the screwdriver onto the potentiometer. Adjust the voltage by turning the screwdriver anticlockwise or clockwise until the measured reference voltage equals the calculated value of VREF.

You don’t need to connect the stepper motor to the driver when setting the current limit

If the current limit is not set properly when you connect a stepper motor to the driver it will produce a lot of noise and vibrations and in such a case you need to lower the current limit. It’s best to set the current limit just high enough so the motor doesn’t miss steps.

Driving A Stepper Motor Using DRV8825 Stepper Motor Driver And Arduino.

The schematic diagram below shows how the connection for Arduino UNO, DRV8825 Stepper motor driver and the stepper motor is done.

  • The STEP and DIR pins are connected to Arduino digital pins 3 and 2 respectively.
  • RESET and SLEEP are connected to 5V to keep the driver enabled.
  • A1, A2, B1 and B2 are connected to the stepper motor coils.
  • VMOT is connected to 8-45V power supply.
  • One of the GND goes to the Arduino ground and the other to the motor power supply ground.
schematic for connecting DRV2588 Stepper motor driver to Arduino

An electrolytic capacitor of 47μF – 100μF is connected between VMOT and GND to protect the motor driver from destructive LC voltage spikes especially when using long power leads.

I have also included a 3 way DIP switch connected to the microstep selection pins M0, M1 and M2. This will enable you to adjust the microsteps for the motor by setting these pins either LOW or HIGH as described earlier.

If M0, M1 and M2 are not connected, the driver operates in full-step mode.

How to determine which wires from the stepper motor belong to the same coil.

When connecting a 4 wire bipolar stepper motor to DRV8825 motor driver, A1 and A2 are connected to the two wires from a single motor coil while B1 and B2 are connected to another coil.

You need to know which pair of wires belongs to the same motor coil. This is done by choosing a random pair of wires from the motor and connecting their bare ends together. Then try spinning the shaft of the stepper motor. If you feel a lot of resistance, you have found a pair of wires from the same coil. If you can still spin the shaft freely, try another pair of wires.

Alternatively, you can measure the resistance between the two wires using a digital multimeter. If the multimeter shows any resistance between the wires, then that pair of wires is from the same coil.

Code for controlling Stepper motor using DRV8825 stepper motor driver and Arduino.

The code below is for controlling the speed and direction of rotation of a bipolar stepper motor using the DRV8825 stepper motor driver and Arduino.

// Define pin connections & motor's steps per revolution
const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 200;

void setup()
{
	// Declare pins as Outputs
	pinMode(stepPin, OUTPUT);
	pinMode(dirPin, OUTPUT);
}
void loop()
{
	// Set motor direction clockwise
	digitalWrite(dirPin, HIGH);

	// Spin motor 4 revolutions slowly
	for(int x = 0; x < 4 * stepsPerRevolution; x++)
	{
		digitalWrite(stepPin, HIGH);
		delayMicroseconds(2000);
		digitalWrite(stepPin, LOW);
		delayMicroseconds(2000);
	}
	delay(1000); // Wait a second
	
	// Set motor direction counterclockwise
	digitalWrite(dirPin, LOW);

	// Spin motor 4 revolutions quickly
	for(int x = 0; x < 4 * stepsPerRevolution; x++)
	{
		digitalWrite(stepPin, HIGH);
		delayMicroseconds(1000);
		digitalWrite(stepPin, LOW);
		delayMicroseconds(1000);
	}
	delay(1000); // Wait a second
}

Code description

First the step (STEP) and direction (DIR) pins are declared with their corresponding Arduino pins as 2 and 3 respectively. The driver is set to full-step mode therefore the steps per revolution will be 200.

In the setup() section, all the motor control pins are declared as digital OUTPUT.

In the loop section, the motor is rotated slowly clockwise and then rapidly counterclockwise with an interval of one second. The four lines of code below will send a pulse to the step pin resulting in one microstep. A pulse is got by pulling the output HIGH, waiting a bit then pulling it LOW and waiting again.

	digitalWrite(stepPin, HIGH);
	delayMicroseconds(1000);
	digitalWrite(stepPin, LOW);
	delayMicroseconds(1000);

The for loop repeats the above lines of code a given number of times which represents the steps per revolution. The for loop in the code above is the same as the one shown below giving 800 steps which is 4 revolutions since the motor is in full step mode.

for(int i = 0; i < 800; i++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }

If the driver is set to ¼ step mode, then the code in the for loop above will result in 1 revolution and if the motor is set to 1/8 step mode, then the above for loop would result in half a revolution.

Controlling motor rotation direction.

The direction of rotation of the stepper motor is determined by setting the direction (DIR) pin either HIGH or LOW. Depending on how you connected the stepper motor, when the DIR pin is set HIGH, the motor will turn clockwise and when set LOW it turns counter clockwise.

Controlling speed of rotation of the motor.

The speed of the stepper motor is determined by the frequency of the pulses we send to the STEP pin which is set using the delayMicroseconds() function. The shorter the delay, the higher the frequency and therefore the faster the motor runs.

Using AccelStepper library to control DRV8825 Stepper Motor Driver.

In case you need to control multiple Stepper motors or to achieve more complex movements, then using a library is a better option and the AccelStepper library written by Mike McCauley is one of the best for controlling stepper motors with a number of motor drivers.

This library can be installed directly from the Arduino IDE by going to Tools > Manage Libraries… to open the Library Manager where you can search for ‘AccelStepper’ and look for and install the latest version of the library by Mike McCauley.

Installing AccelStepper library

Example code for controlling DRV8825 Stepper motor driver with Arduino using AccelStepper library.

The code below uses the AccelStepper library to run the motor back and forth using the A4988 Stepper motor driver.

#include <AccelStepper.h>

#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  stepper.setMaxSpeed(1000);
}

void loop() { 
  stepper.setCurrentPosition(0);
  while(stepper.currentPosition() != 400)
  {
    stepper.setSpeed(200);
    stepper.runSpeed();
  }
  delay(1000);
  stepper.setCurrentPosition(0);

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

  delay(1000);

  stepper.setCurrentPosition(0);

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

  delay(3000);
}

Code description:

First, you have to include the AccelStepper library and then declare the DRV8825 and Arduino connections and the motor interface type. When using a step and direction driver, the motor interface type must be set to 1.

Then you need to create a new instance of the AccelStepper class with the appropriate motor interface type and connections. You can even create multiple instances of the AccelStepper class with different names and pins which allows you to easily control two or more stepper motors at the same time.

In the setup() section, we use the setMaxSpeed() function to set the maximum speed in steps/second.

In the loop section, the setCurrentPosition() function is used to set the position of the motor to zero. A while loop is used to check if the current position of the stepper motor is not equal to 400 steps and while this is not the case, we run the stepper motor at a constant speed of 200steps/second as set by setSpeed() and runSpeed() functions.

The rest of the loop just does exactly the same but with different speed and target positions.

setSpeed() function can take on negative values indicating a backward rotation.

Example for Acceleration and deceleration of a Stepper Motor using AccelStepper library.

#include <AccelStepper.h>

#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  stepper.setMaxSpeed(200);
  stepper.setAcceleration(30);
}

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

  delay(1000);

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

  delay(1000);
}

The code sketch is similar to the previous example but in the setup() section, we use the setMaxSpeed() and setAcceleration() functions to define the maximum speed and acceleration or deceleration of the motor.

In the loop() section of the code, I have used the moveTo() function to let the motor rotate a predefined number of steps.

The runToPosition() function moves the motor with the set acceleration or deceleration to the target position. This function is blocking and therefore should not be used when you need to control other aspects at the same time.

You can also make more practice with the AccelStepper library to control various movements of stepper motors using the DRV8825 Stepper motor driver.