IOT

How to Control Stepper Motor with DRV8825 Driver & Arduino

A stepper motor is an electromechanical device that converts electrical power into mechanical power.  It’s also a brushless, synchronous electric motor with the ability to divide a full rotation into a significant number of stages. As long as the motor is properly sized for the application, the position of the motor can be precisely regulated without the use of any feedback device.

In this tutorial, we’ll use the DRV8825 Driver Module and Arduino to control a NEMA17 stepper motor. The DRV8825 is a micro-stepping driver with a built-in translator for easy use that can control bipolar stepper motors. With just two pins from our controller, we can control the stepper motor. The STEP pin will control the steps while the DIR pin will control the rotation direction.

DRV8825 Stepper Motor Arduino

Components Required:

  • Arduino UNO R3/ Nano or Any other Arduino Board
  • DRV8825 Stepper Motor Driver
  • NEMA17 Stepper Motor
  • Electrolytic Capacitors-100uF
  • Power supply-12V, 2A DC Adapter
  • Power supply-5V DC Adapter
  • Connecting Wires
  • Breadboard

DRV8825 Stepper Motor Driver Module

DRV8825 Stepper Driver Module

The DRV8825 is a complete Microstepping Motor Driver with an easy-to-use built-in translator. Texas Instruments’ breakout board includes configurable current limiting, over-current, and over-temperature protection, and six microstep resolutions. It operates between 8.2V and 45 volts and can deliver up to 1.5 amps per phase without the use of a heat sink or forced airflow. It has a current rating of 2.2 A per coil and adequate 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 lockout
  • 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 a total of 16 pins which are as follows:

DRV8825 Pinout

1. Power Supply Pins: This pin has the VMOT, GND MOT, and GND Logic functions. Because it is powered by an internal 3V3 voltage regulator, the DRV8825 module does not have a logic supply pin. The VMOT provides power to the motor, which can range from 8.2 to 45 volts.

2. Microstep Selection Pins: The DRV8825 driver provides three micro-step resolution selector inputs, M0, M1, and M2. We’ll set the motors to at least one of the six-step resolutions by setting appropriate logic levels to those pins.

DRV8825 Step Resoultion

3. Control Input Pins: The two control input pins are STEP and DIR. The micro-steps of the motor are controlled by the STEP input. The motor will rotate faster if the pulses are quicker. The DIR input controls the motor’s spinning direction. When you pull it HIGH, the motor spins clockwise, and when you pull it LOW, the motor spins anti-clockwise.

4. Power States Control Pin: TThe EN, RST, and SLP inputs of the DRV8825 are used to control the device’s power states. The EN pin is always active low input by default, enabling the driver. SLP Pin is a low-input active pin. Pulling this pin LOW puts the driver into sleep mode, reducing the amount of energy used by the facility. The RST is a lively low input that ignores all STEP inputs when pulled LOW. It also resets the driver by resetting the internal translator to the initial stage of the motor..

5. Output Pins: B2, B1, A2, and A1 are the four output pins. Any bipolar stepper motor with a voltage range of 8.2V to 45V can be connected to those pins. The module’s output pins can give up to 2.2A to the motor.

6. Fault Detection Pin: When the H-bridge FETs are disabled owing to over-current protection or thermal shutdown, the DRV8825 has a FAULT output that drives LOW. The Fault pin is connected to the SLEEP pin, and driving it low disables the entire chip.

Heat Sink Requirement

If the current rating is up to 1.5A, the DRV8825 Driver can be used without a heat sink. A heat sink or other cooling mechanism is required to get more than 1.5A per coil, i.e. 2.2A.

The DRV8825 driver’s excessive power dissipation causes a temperature rise that can exceed the IC’s capacity, potentially destroying it.

Setting Up Current Limit

Before connecting the motor, make sure the driver’s current limiting is set to keep the current within the motor’s limits. We can accomplish so by altering the reference voltage on the board with the potentiometer and utilizing the calculation below.

Current Limit = VRef x 2

If the Stepper Motor is rated for 350mA, for example, the reference voltage must be set to 0.17V. Adjust the current limit with a potentiometer with a little screwdriver until you achieve the rated current.

DRV8825 Current Limit Set

NEMA17 Stepper Motor

NEMA 17 is a 1.8° step-angle hybrid stepping motor with 200 steps per revolution. At 4 V, each phase draws 1.2 A, resulting in a holding torque of 3.2 kg-cm. Printers, CNC equipment, and Laser Cutters all require NEMA 17 stepper motors. These stepper motors move in precisely repeatable steps. Hence they are the ideal motors for the machines requiring precise position control. Stepper Motor Drivers used to command to move or hold at one position of the motor. The NEMA17 3.2 kg-cm Stepper Motor features an excellent response to starting, stopping, and reversing pulses from the stepper motor driver.

NEMA17 Stepper Motor

Six wires are connected to two split windings in this motor. The first winding includes black, yellow, and green wires, whereas the second winding includes red, white, and blue wires.

Interfacing NEMA17 Stepper Motor with Arduino using DRV8825 Driver

Now we’ll connect the DRV8825 Stepper Motor Driver to the Arduino and use it to control the NEMA17 Stepper Motor. To control the motor direction and stride, I used the D2 and D3 pins. The link is shown schematically below.

DRV8825 Arduino Nema17 Stepper Motor

The VDD pin is powered by a 5V supply, whereas the VMOT pin is powered by a 12V supply. Remember to connect the motor power supply pins to the board with a hefty 100F decoupling electrolytic capacitor.

DRV8825 Arduino Stepper Motor Control

Project PCB Gerber File & PCB Ordering Online

If you don’t want to put the circuit together on a breadboard and instead prefer a PCB, this is the PCB for you.  EasyEDA online Circuit Schematics & PCB Design tool was used to create the PCB Board for the Nema17 Stepper Motor Control with DRV8825 & Arduino. The PCB’s front and back sides are shown below.

Fig: Front ViewFig: Back View

The Gerber File for the PCB Board is given below. You can simply download the Gerber File from the following link.

Download Gerber File: DRV8825 + Arduino + NEMA17 PCB

Basic Stepper Motor Control Code

It’s time to connect the Arduino to the computer and upload some code now that you’ve connected up the driver and set the current limit. This sketch only moves the motor in one direction.



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

The direction of the stepper motor can be controlled with this code. The motor can be rotated in either a clockwise or anticlockwise direction. The stepper motor’s speed, number of rotations, and spinning direction are all controlled by this sketch.

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

The Arduino AccelStepper library can be used to operate a stepper motor. It provides an object-oriented interface for stepper motors and motor drivers with 2, 3, or 4 pins.

AccelStepper outperforms the conventional Arduino Stepper library in various respects, including acceleration and deceleration capabilities. It also enables many steppers running at the same time, with each stepper doing independent concurrent stepping. Even the slowest speeds are supported.

The code below demonstrates all of the characteristics stated above.

#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 adds acceleration and deceleration to the stepper motor’s motions. The motor will move back and forth at a speed of 200 steps per second and a 30 step per second acceleration.


#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 potentiometer can also be used to control the stepper motor. I used a 10K potentiometer and linked it to the Arduino Nano’s A0 analog pin. The voltage sent to Arduino’s Analog pin can be utilized as a reference voltage to control the Stepper Motor’s speed. The following is a schematic of the connections.

Stepper Motor DRV8825 Potentiometer Arduino

Copy the code from below and upload it to the Arduino Nano Board.

/ 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;
}

Conclusion

I hope all of you understand how to Control Stepper Motor with DRV8825 Driver & Arduino. We MATHA ELECTRONICS will be back soon with more informative blogs soon.

Leave a Reply

Your email address will not be published.