Skip to content

Controlling Stepper Motor with DRV8825 Driver Module & Arduino

In this article, we will be discussing how to control a stepper motor with a DRV8825 driver module & Arduino, This article will give you ample information on the components used and how to drive them.

What is a Stepper Motor ??     

A stepper motor, also known as a step motor or stepping motor, is a brushless dc electric motor that divides a full rotation into a number of equal steps.

The motor’s position can be commanded to move and hold at one of these steps without any position sensor for feedback (an open-loop controller ), as long as the motor is correctly sized to the application in respect to torque and speed.

Where are these Stepper Motors Used??

The stepper motor is used for precise positioning with a motor, such as hard disk drives, robotics, antennas, telescopes, and some toys. Stepper motors cannot run at high speeds but have a high holding torque.

Difference Between Stepper Motor & DC Motor??

DCStepper
Some DC motors also generate high torques at low speeds but are more suited towards continuous uses, as their torque is constant over their speed range. The main difference is that, while stepper motors can push harder from rest, DC motors tend to have more sustained output.
Stepper motors provide their maximum torque at low speeds, which makes them especially useful in high precision, holding applications such as robotics, 3D printers, and similar devices where the position is fundamental.
A DC motor cannot control the position of the rotorA stepper motor has the ability to control the position of the rotor.
DC motors can also run this long depending on the type chosen, but require constant maintenance to prevent failure.
Both the stepper motor and DC motor are reliable, with caveats. When stepper motors are not run continuously, they can easily reach 10,000 hours of useful life or almost five years of use.

Both of these motor types are inexpensive, so the purchase price is of little concern. DC motors and stepper motors are useful for applications where the price must be minimized.

How to Drive a Stepper Motor ??

The following section is very detailed. The basics presented here are pretty universal and widely used. So, what do we need to get these motors going??? Let’s break it down into components and explain each part briefly.

Driver

You all know that – the stepping motor can be moved one step at a time by applying electricity to coils in the correct order (and polarities). You could do this manually with some switches – step by step, but it has no practical use other than learning. This is where the driver comes into play.

The driver is doing the heavy lifting and it hides all the complexity behind a simple interface. It makes correct windings be excited in the correct way based on the input signals. They usually have only 2 input pins that take commands in form of Digital high(1) and low(0). One sets the direction of rotation and the other is for step commands.

But it’s not as simple as connecting steps pin into logical HIGH and expecting the driver to move the motor continuously. It would still make only one step.

Steps are given as digital pulses. After each step (HIGH) there must be (LOW) input for a moment. So drive can detect when new step command is given. If there is are no pulses given- there will be no steps done by the drive and motor.

Direction input pin can be LOW or HIGH all the time, while steps are made, depending on the direction needed. The direction does not need impulses.

Microcontroller

It is possible to make motors move by touching the step pin on the driver manually with HIGH wire. But that would not be very practical other than testing. This is where the microcontroller comes into play. Microcontrollers can give many hundreds or even thousands of impulses per second so the motor can be rotated very fast and accurately.

The controller itself does not know how you want to move your motor to be useful. Some kind of software (firmware) must be installed on the controller. If you are building a CNC router or a 3d printer you can use software already written by others. For example:

For simpler applications, it’s probably enough, but for anything that handles heavier loads and more complex operations you should use some library. For Arduino, you can use the AccelStepper library.

Power Supply

You need an external power supply for most drivers and motors. Some small Arduino kit motors can be run on USB/Arduino power. But everything bigger needs more power and voltage. For testing, most likely you can use an average computer or laptop power supply.

Chopper Driver

A chopper drive can run the stepper motor with a much higher voltage than the motor’s rated voltage. Higher voltage allows the current to flow through the stepper motor faster, which gives the ability to turn it faster with more torque. Drive keeps current in the motor below the fixed value which keeps the motor burning out. Additionally- higher voltage means less heat.

When using a chopper drive, the nominal voltage of the motor is mostly irrelevant for practical purposes. At least for hobby users and Arduino enthusiasts. So don’t get scared away from stepper motor based on very low rated voltage. The important thing to figure out is the rated current.

Micro Stepping

Microstepping is a way of moving the stator flux of a stepper more smoothly than in full- or half-step drive modes. This results in less vibration and makes noiseless stepping possible down to 0 Hz. It also makes smaller step angles and better positioning possible.

Although Microstepping gives the designer more resolution, improved accuracy is not realized. Reduction in mechanical and electromagnetically induced noise is, however, a real benefit.

The mechanical transmission of torque will also be much gentler and resonance problems are reduced. This gives better confidence in maintaining synchronization of the open-loop system and less wear and tear on the mechanical transmission system.

In fact, taking an infinite number of microsteps per full step results in two-phase synchronous permanent magnet ac motor operation.

How to Wire a Stepper Motor to a Driver using a Microcontroller??

Now that we know everything about the driver, we will connect it to our Arduino. Connections are fairly simple. Start by connecting the RST pin to the adjacent SLP/SLEEP pin and both to the 5V on the Arduino to keep the driver enabled.

Connect the GND LOGIC pin to the ground pin on the Arduino. DIR and STEP input pins are connected to #2 & #3 digital output pins on Arduino respectively.

Connect the stepper motor to the B2, B1, A1 & A2 pins. Actually, DRV8825 is conveniently laid out to match the 4-pin connector on several bipolar motors so, that shouldn’t be a problem.

Note: Connecting or disconnecting a stepper motor while the driver is powered can destroy the driver.

Remember to keep the micro-step selection pins disconnected to operate the motor in full-step mode.

Finally, connect the motor power supply to the VMOT and GND MOT pins. Remember to put a large 100µF decoupling electrolytic capacitor across motor power supply pins, close to the board.

Pic Credits:Lastminuteengineers.com

Microstep Selection Pins

The DRV8825 driver allows micro-stepping by allowing intermediate step locations. This is achieved by energizing the coils with intermediate current levels.

For example, if you choose to drive NEMA 17 having 1.8° or 200 steps per revolution in quarter-step mode, the motor will give 800 microsteps per revolution.

The DRV8825 driver has three-step size(resolution) selector inputs viz. M0, M1 & M2 . By setting appropriate logic levels to these pins we can set the motors to one of the six-step resolutions.

M0M1M2Microstep Resolution
LowLowLowFull step
HighLowLowHalf step
LowHighLow1/4 step
HighHighLow1/8 step
LowLowHigh1/16 step
HighLowHigh1/32 step
LowHighHigh1/32 step
HighHighHigh1/32 step

These three micro-step selection pins are pulled LOW by internal pull-down resistors, so if we leave them disconnected, the motor will operate in full-step mode.

Control Input Pins

The DRV8825 has two control inputs viz. STEP and DIR.

STEP input controls the micro-steps of the motor. Each HIGH pulse sent to this pin steps the motor by a number of microsteps set by Microstep Selection Pins. The faster the pulses, the faster the motor will rotate.

DIR input controls the spinning direction of the motor. Pulling it HIGH drives the motor clockwise and pulling it LOW drives the motor counterclockwise. If you just want the motor to rotate in a single direction, you can tie DIR directly to VCC or GND accordingly.

Current Limiting

Before using the motor, there’s a small adjustment that we need to make. We need to limit the maximum amount of current flowing through the stepper coils and prevent it from exceeding the motor’s rated current.

There’s a small trimmer potentiometer on the DRV8825 driver that can be used to set the current limit. You should set the current limit to be at or lower than the current rating of the motor.

To make this adjustment there are two methods: we will go with the simpler one.

In this method, we are going to set the current limit by measuring the voltage (Vref) on the “ref” pin.

1. Take a look at the datasheet for your stepper motor. Note down it’s rated current. In our case, we are using NEMA 17 200steps/rev, 12V 350mA.

2.Put the driver into full-step mode by leaving the three micro-step selection pins disconnected.

3.Hold the motor at a fixed position by not clocking the STEP input.

4.Measure the voltage (Vref) on the metal trimmer pot itself while you adjust it.

5.Adjust the Vref voltage using the formula

Current Limit = Vref x 2

For example, if your motor is rated for 350mA, you would adjust the reference voltage to 0.175V.

Code:

Controlling the stepper without a library is perfectly fine for simple, single motor applications. But when you want to control multiple steppers, you’ll need a library.

So, for our next experiment, we will make use of an advanced stepper motor library called AccelStepper library. It support

1.Acceleration and deceleration.

2.Multiple simultaneous steppers, with independent concurrent stepping on each stepper.

This library is not included in the Arduino IDE, so you will need to install it first.

Library Installation

To install the library go to the Sketch > Include Library > Manage Libraries. 

Now type in the library name and wait for it to show up and once the name pops up click on install.

Here’s the simple sketch that accelerates the stepper motor in one direction and then decelerates to come to rest. Once the motor makes one revolution, it changes the spinning direction. And it keeps doing that over and over again.

// Include the AccelStepper Library
#include <AccelStepper.h>

// Define pin connections
const int dirPin = 2;
const int stepPin = 3;

// Define motor interface type
#define motorInterfaceType 1

// 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());

  // Move the motor one step
   myStepper.run();
}

Code Explanation:

We define Arduino pins to which DRV8825’s STEP & DIR pins are connected. We also set motorInterfaceType to 1. (1 means an external stepper driver with Step and Direction pins)



// Define pin connections
const int dirPin = 2;
const int stepPin = 3;

// Define motor interface type
#define motorInterfaceType 1



Next, we create an instance of the stepper library called mystepper.



// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);

In the setup function, we first set the maximum speed of the motor to a thousand. We then set an acceleration factor for the motor to add acceleration and deceleration to the movements of the stepper motor.

Next, we set the regular speed of 200 and the number of steps we’re going to move it to i.e. 200 (as NEMA 17 moves 200 steps per revolution).



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

In the loop function, we use an If statement to check how far the motor needs to travel (by reading the distanceToGo function) until it reaches the target position (set by moveTo). Once distanceToGo reaches zero we will move the motor in the opposite direction by changing the moveTo position to the negative of its current position.

Now at the bottom of the loop, you’ll notice we have called a run() function. This is the most important function because the stepper will not run until this function is executed.



void loop() 
{
   // Change direction once the motor reaches target position
   if (myStepper.distanceToGo() == 0) 
   myStepper.moveTo(-myStepper.currentPosition());

  // Move the motor one step
   myStepper.run();
}


2 thoughts on “Controlling Stepper Motor with DRV8825 Driver Module & Arduino”

Leave a Reply

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