Skip to main content

DRV8825 - Driving the motor (examples)

This page contains simple examples with function documentation on how to drive the stepper motor.


Initialization

To use the DRV8825 stepper driver, first, include the required library and create the driver object.

#include <Basic-Stepper-Driver-SOLDERED.h>

#define dirPin 4
#define stepPin 5

BasicStepper stepper(BasicStepper::DRIVER, stepPin, dirPin);
//...

BasicStepper()

Constructor for the BasicStepper class, derived from AccelStepper. Initializes the stepper motor interface, sets the default parameters, and configures the output pins.

Returns value: None

Function parameters:

TypeNameDescription
uint8_tinterfaceMotor interface type, such as FULL4WIRE, FULL2WIRE, DRIVER, etc. Defaults to FULL4WIRE. See table below for more details.
uint8_tpin1Digital pin number for motor control. Defaults to pin 2.
uint8_tpin2Digital pin number for motor control. Defaults to pin 3.
uint8_tpin3Digital pin number for motor control. Defaults to pin 4.
uint8_tpin4Digital pin number for motor control. Defaults to pin 5.
boolenableIf true, enables pin outputs. Defaults to true.
Interface TypeValueDescription
DRIVER1Uses a step and direction driver, requiring only 2 pins (STEP and DIR). Ideal for external stepper drivers like A4988, DRV8825.
FULL2WIRE2Controls a 2-wire stepper motor, commonly used in unipolar configurations. Requires 2 control pins.
FULL3WIRE3Supports 3-wire stepper motors, such as some older hard drive spindles. Requires 3 control pins.
FULL4WIRE4Standard 4-wire bipolar stepper motor configuration. Requires 4 control pins.
HALF3WIRE6Used for 3-wire half-stepping motors, allowing finer control over movement.
HALF4WIRE8Enables half-stepping mode for 4-wire stepper motors, doubling the resolution for smoother motion.

Constant rotation

Let's start out with a basic example, turning the motor in one direction constantly.

#include <Basic-Stepper-Driver-SOLDERED.h>

#define dirPin 4
#define stepPin 5

BasicStepper stepper(BasicStepper::DRIVER, stepPin, dirPin);
void setup() {
stepper.setMaxSpeed(1000);
stepper.setAcceleration(50);
stepper.setSpeed(200);
}

void loop() {
stepper.runSpeed();
}

stepper.setMaxSpeed()

Sets the maximum speed allowed for the motor. The motor will accelerate up to this speed during movement.

Returns value: None

Function parameters:

TypeNameDescription
floatspeedMaximum speed in steps per second. Must be greater than 0.

stepper.setAcceleration()

Sets the acceleration and deceleration rate for smooth motor control. Affects how quickly the motor reaches its max speed.

Returns value: None

Function parameters:

TypeNameDescription
floataccelerationAcceleration value in steps per second². Must be greater than 0.

stepper.setSpeed()

Sets a constant speed for the motor when using runSpeed(). The speed will be limited by the setMaxSpeed() value.

Returns value: None

Function parameters:

TypeNameDescription
floatspeedSpeed in steps per second. Positive values rotate clockwise, negative values counterclockwise.

stepper.runSpeed()

Moves the stepper motor at a constant speed based on the last setSpeed() value. This function must be called repeatedly in the loop for continuous motion.

Returns value: Returns true if the motor was stepped.


Rotating a specified number of steps

In this example, we are using the NEMA17 stepper motor which has 200 steps per revolution. Let's rotate that motor for one quarter, then one half of one full rotation:

#include <Basic-Stepper-Driver-SOLDERED.h>

#define dirPin 4
#define stepPin 5
#define motorInterfaceType 1

BasicStepper stepper(BasicStepper::DRIVER, stepPin, dirPin);
void setup() {
stepper.setMaxSpeed(1000);
stepper.setAcceleration(50);
stepper.setSpeed(200);
}

void loop() {
stepper.move(100);
while(stepper.run()){}
delay(2000);
stepper.move(50);
while(stepper.run()){}
delay(2000);
}

stepper.move()

Sets the target position relative to the current position. This function does not immediately move the motor but sets the target steps to be executed when run() is called.

Returns value: None

Function parameters:

TypeNameDescription
longrelativeThe desired position relative to the current position. Negative values move counterclockwise.

stepper.run()

Moves the motor towards the set target position while handling acceleration and deceleration automatically. Must be called repeatedly in the main loop for continuous operation.

Returns value: Returns true if the motor is still moving towards the target position.

You can also use stepper.moveTo() to set an absolute target position instead of a relative movement. Unlike stepper.move(), which moves the motor a certain number of steps from its current position, moveTo() moves the motor to a specific step position on the stepper's coordinate system.

After calling moveTo(), you must call run() repeatedly in the loop to execute the movement. Keep in mind that moveTo() also recalculates the speed for the next step, so if you want to maintain a constant speed, you should call setSpeed() again after moveTo():

stepper.moveTo(1000); // Move to step position 1000
stepper.setSpeed(200); // Maintain a constant speed
while (stepper.run()); // Run until the target position is reached

stepper.moveTo()

Sets the target position for the stepper motor. The motor will move to this position when run() is called, handling acceleration and deceleration automatically.

Returns value: None

Function parameters:

TypeNameDescription
longabsoluteThe absolute position (in steps) to move to. Negative values move counterclockwise from the home position.

Full examples

Try these full working examples from the library:

ConstantSpeed.ino

Run the motor at constant speed

Blocking.ino

Place the motor in certain positions