Overview: Control Stepper Motor with DRV8825 Driver & Arduino
In this tutorial we will Control the NEMA17 Stepper Motor with DRV8825 Driver Module & Arduino. The DRV8825 is a micro-stepping driver for controlling bipolar stepper motors which have a built-in translator for easy operation. Thus, we can control the stepper motor with just 2 pins from our controller. The DIR pin will control the rotation direction and the STEP pin will control the steps.
In the previous tutorial we learned Controlling the Stepper Motor with Potentiometer and also with Joystick. The 28BYJ-48 is a 5-wire unipolar stepper motor that runs on 5 volts and doesn’t require any driver. But the NEMA17 Stepper Motor is requires 8V-35V Power as the torque is too high. Therefore we need Stepper Driver Module like A4988 or DRV8825.
Bill of Materials
The following are the components required for learning this tutorial. All the components can be easily purchased from Amazon. The component purchase link is given as well.
| S.N. | Components Name | Quantity | Purchase Links |
|---|---|---|---|
| 1 | Arduino Nano | 1 | Amazon | AliExpress |
| 2 | DRV8825 Stepper Motor Driver | 1 | Amazon | AliExpress |
| 3. | NEMA17 Stepper Motor | 1 | Amazon | AliExpress |
| 4 | Capacitor 100uF | 2 | Amazon | AliExpress |
| 5 | 12V, 2A Power Supply | 1 | Amazon | AliExpress |
| 6 | 5V DC Adapter | 1 | Amazon | AliExpress |
| 7 | Connecting Wires | 20 | Amazon | AliExpress |
| 8 | Breadboard | 1 | Amazon | AliExpress |
DRV8825 Stepper Motor Driver Module
The DRV8825 is a complete Microstepping Motor Driver with a built-in translator for easy operation. The breakout board from Texas Instruments features adjustable current limiting, over-current and over-temperature protection, and six different microstep resolutions. It operates from 8.2 V to 45 V and can deliver up to approximately 1.5 A Current per phase without a heat sink or forced airflow. It is rated for 2.2 A Current per coil with sufficient additional cooling.
Features
- Max. Operating Voltage: 45V
- Min. Operating Voltage: 8.2V
- Max. Current Per Phase: 2.5A
- Microstep resolution: Full step, 1/2 step, 1/4 step, 1/8 step, 1/16 step 1/32 step
- Over temperature shutdown circuit
- Under-voltage lock out
- Over current shutdown
- Dimensions: 20.5 x 15.5 mm (0.8″ × 0.6″)
- Short-to-ground and shorted-load protection
- Low RDS(ON) outputs
DRV8825 Motor Driver Pinout
The DRV8825 driver has total of 16 pins which are as follows:
1. Power Supply Pins: The pin includes VMOT, GND MOT & GND Logic. The DRV8825 module does not have any logic supply pin as it gets its power from the internal 3V3 voltage regulator. The VMOT supplies power for the motor which may be 8.2V to 45 V.
2. Microstep Selection Pins: The DRV8825 driver has three-step resolution selector inputs, i.e., M0, M1 & M2. By setting appropriate logic levels to those pins we will set the motors to at least one of the six-step resolutions.
3. Control Input Pins: STEP & DIR are the 2 control input pins. STEP input controls the micro-steps of the motor. The faster the pulses, the faster the motor will rotate. The DIR input controls the spinning direction of the motor. Pulling it HIGH drives the motor clockwise and pulling it LOW drives the motor anti-clockwise
4. Power States Control Pin: The DRV8825 has three different inputs for controlling its power states, i.e EN, RST, and SLP. The EN pin is always active low input by default which enables the driver. SLP Pin is active low input. Pulling this pin LOW puts the driver in sleep mode, minimizing the facility consumption. The RST is a lively low input which when pulled LOW, all STEP inputs are ignored. It also resets the driver by setting the internal translator to a motor initial stage.
5. Output Pins: There are 4 output pins as B2, B1, A2, A1. We can connect any bipolar stepper motor having voltages between 8.2V to 45 V to those pins. Each output pin on the module can deliver up to 2.2A to the motor
6. Fault Detection Pin: The DRV8825 features a FAULT output that drives LOW whenever the H-bridge FETs are disabled due to over-current protection or thermal shutdown. The Fault pin is shorted to the SLEEP pin & when it is driven LOW, the whole chip is disabled.
Heat Sink Requirement
It is safe to use the DRV8825 Driver without a heat sink if the current rating is up to 1.5A. For achieving more than 1.5A per coil, i.e. 2.2A, a heat sink or other cooling method is required.
Because of the excessive power dissipation of the DRV8825 driver, there is a rise in temperature that can go beyond the capacity of IC, probably damaging it.
Setting Up Current Limit
Before we connect the motor we should adjust the current limiting of the driver so that the current is within the limit of the motor. We can do that by adjusting the reference voltage using the potentiometer on the board and considering this equation below.
|
1 |
Current Limit = VRef x 2 |
For example, if the Stepper Motor is rated for 350mA, we need to adjust the reference voltage to 0.17V. Take a small screwdriver and adjust the current limit with a potentiometer until you reach the rated current.
NEMA17 Stepper Motor
NEMA 17 is a hybrid stepping motor with a 1.8° step angle (200 steps/revolution). Each phase draws 1.2 A at 4 V, allowing for a holding torque of 3.2 kg-cm. NEMA 17 Stepper motor is generally used in Printers, CNC machines and Laser Cutters. Check out Stepper Motor Calculator to calculate various stepper motor parameters.
This motor has six wires, connected to two split windings. Black, Yellow, Green wires are part of the first winding while Red, White, and Blue is part of the second winding.
Interfacing NEMA17 Stepper Motor with Arduino using DRV8825 Driver
Now let’s connect the DRV8825 Stepper Motor driver to Arduino and control NEMA17 Stepper Motor. I used D2 & D3 pins to control the motor direction and step. The schematic for the connection is given below.
The VMOT pin is powered by 12V Power Supply and VDD is powered using a 5V Supply. Remember to put a large 100µF decoupling electrolytic capacitor across motor power supply pins, close to the board.
Basic Stepper Motor Control Code
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. This sketch controls motor in a single direction.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
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 slowly for(int x = 0; x < 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 quickly for(int x = 0; x < stepsPerRevolution; x++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } delay(1000); // Wait a second } |
Controlling Stepper Motor Spinning Direction
Using this code, the stepper motor direction can be controlled. You can rotate the motor in either clockwise direction or in anticlockwise direction. This sketch controls both the speed, the number of revolutions, and the spinning direction of the stepper motor.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
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 slowly for(int x = 0; x < 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 quickly for(int x = 0; x < stepsPerRevolution; x++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } delay(1000); // Wait a second } |
Controlling Stepper Motor with AccelStepper Library
Stepper Motor can be controlled using the Arduino AccelStepper library. It provides an object-oriented interface for 2, 3, or 4 pin stepper motors and motor drivers.
AccelStepper significantly improves on the standard Arduino Stepper library in several ways like it supports acceleration and deceleration. It also supports multiple simultaneous steppers, with independent concurrent stepping on each stepper. Even very slow speeds are also supported
The following code shows all the above-mentioned features.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <AccelStepper.h> // Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver: #define dirPin 2 #define stepPin 3 #define motorInterfaceType 1 // Create a new instance of the AccelStepper class: AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin); void setup() { // Set the maximum speed in steps per second: stepper.setMaxSpeed(1000); } void loop() { // Set the current position to 0: stepper.setCurrentPosition(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(); } delay(1000); // Reset the position to 0: stepper.setCurrentPosition(0); // 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(); } delay(1000); // Reset the position to 0: stepper.setCurrentPosition(0); // Run the motor forward at 400 steps/second until the motor reaches 600 steps (3 revolutions): while(stepper.currentPosition() != 600) { stepper.setSpeed(400); stepper.runSpeed(); } delay(3000); } |
Stepper Motor Acceleration & deceleration Code
The following sketch will add acceleration and deceleration to the movements of the stepper motor. The motor will run back and forth with a speed of 200 steps per second and an acceleration of 30 steps per second.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <AccelStepper.h> #define dirPin 2 #define stepPin 3 #define motorInterfaceType 1 // Create a new instance of the AccelStepper class: AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin); void setup() { // Set the maximum speed and acceleration: stepper.setMaxSpeed(200); stepper.setAcceleration(30); } void loop() { // Set the target position: stepper.moveTo(600); // Run to target position with set speed and acceleration/deceleration: stepper.runToPosition(); delay(1000); // Move back to zero: stepper.moveTo(0); stepper.runToPosition(); delay(1000); } |
Control NEMA17 Stepper Motor with DRV8825 & Potentiometer
The stepper motor can be controlled using Potentiometer as well. I used a 10K Potentiometer and connected it to the A0, analog pin of the Arduino Nano board. The voltage fed to the Analog pin of Arduino can be used as a reference voltage to control the speed of the Stepper Motor. The connection diagram is given below.
Copy the code from below and upload it to the Arduino Nano Board.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/ Defines pins numbers const int stepPin = 3; const int dirPin = 4; int customDelay,customDelayMapped; // Defines variables void setup() { // Sets the two pins as Outputs pinMode(stepPin,OUTPUT); pinMode(dirPin,OUTPUT); digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction } void loop() { customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends digitalWrite(stepPin, HIGH); delayMicroseconds(customDelayMapped); digitalWrite(stepPin, LOW); delayMicroseconds(customDelayMapped); } // Function for reading the Potentiometer int speedUp() { int customDelay = analogRead(A0); // Reads the potentiometer int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000) return newCustom; } |


















4 Comments
Why is the FAULT output pin of the DRV8825 shown apparently connected to +5V?
It was a very good explanation. I need to run 2 stepper motors for a project, but the 2nd stepper driver does not run the stepper motor. Do you have sample code for 2 stepper Motors?
Pas d’explication sur le fonctionnement de Enable
So, as a noob with code, I have wired it all up perfectly copy and paste the code for the potentiometer version and the code fails as it is missing some part of the code, it does not tell me what part is missing can anyone show what the complete code is?