In this post I go over how to use the DRV8825 Stepper Motor Controller. We will learn about the basics of the DRV8825, how to use the microstepping function of the DRV8825, how to set the current limit to avoid exceeding your motor current limit, how to connect to Arduino, and we will go over several examples.
Examples
Move from one position to another
|
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#include <AccelStepper.h> // Define stepper pins #define STEP_PIN 3 // Step pin #define DIR_PIN 2 // Direction pin // Microstepping control pins #define M0_PIN 7 #define M1_PIN 6 #define M2_PIN 5 // Steps per revolution for the motor const float stepsPerRevolution = 200; // Microstepping multiplier (1, 2, 4, 8, 16, or 32) int microstepSetting = 1; // AccelStepper instance in driver mode AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); // Function to calculate steps based on desired rotations float convert_rotational_position_to_steps(float rotations) { return rotations * stepsPerRevolution * microstepSetting; } void setup() { // Set microstepping pins as outputs pinMode(M0_PIN, OUTPUT); pinMode(M1_PIN, OUTPUT); pinMode(M2_PIN, OUTPUT); // Set microstepping mode (adjust as needed: HIGH or LOW) digitalWrite(M0_PIN, LOW); // Adjust microstepping settings as needed digitalWrite(M1_PIN, LOW); digitalWrite(M2_PIN, LOW); // Set the max speed and acceleration float MaxRPM = 300; // Set max speed in rpm (revolutions per minute) float Max_Speed_StepsPerSec = microstepSetting * stepsPerRevolution * MaxRPM / 60; // Specify max speed in steps/sec (converted from RPM) stepper.setMaxSpeed(Max_Speed_StepsPerSec); float AccelRPMperSec = 3000; // Set acceleration in rpm/sec float Accel_StepsPerSec2 = microstepSetting * stepsPerRevolution * AccelRPMperSec / 60; stepper.setAcceleration(Accel_StepsPerSec2); // Move to position 0 as the starting point stepper.setCurrentPosition(0); } void loop() { // Move to 3 o'clock position stepper.moveTo(convert_rotational_position_to_steps(0.25)); while (stepper.distanceToGo() != 0) { stepper.run(); } delay(500); // Move to 9 o'clock position stepper.moveTo(convert_rotational_position_to_steps(0.75)); while (stepper.distanceToGo() != 0) { stepper.run(); } delay(500); // Move to 6 o'clock position stepper.moveTo(convert_rotational_position_to_steps(0.50)); while (stepper.distanceToGo() != 0) { stepper.run(); } delay(500); // Move to 12 o'clock position stepper.moveTo(convert_rotational_position_to_steps(1)); while (stepper.distanceToGo() != 0) { stepper.run(); } delay(500); // Move to starting position (will rotate in opposite direction) stepper.moveTo(convert_rotational_position_to_steps(0)); while (stepper.distanceToGo() != 0) { stepper.run(); } delay(2000); } |
Constant RPM
|
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 |
#include <AccelStepper.h> // Define stepper pins #define STEP_PIN 3 // Step pin #define DIR_PIN 2 // Direction pin // Microstepping control pins #define M0_PIN 7 #define M1_PIN 6 #define M2_PIN 5 // Steps per revolution for the motor const float stepsPerRevolution = 200; // Microstepping multiplier (1, 2, 4, 8, 16, or 32) int microstepSetting = 1; // AccelStepper instance in driver mode AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); void setup() { // Set microstepping pins as outputs pinMode(M0_PIN, OUTPUT); pinMode(M1_PIN, OUTPUT); pinMode(M2_PIN, OUTPUT); // Set microstepping mode (adjust as needed: HIGH or LOW) digitalWrite(M0_PIN, LOW); // Set to LOW or HIGH for desired microstep setting digitalWrite(M1_PIN, LOW); // Set to LOW or HIGH for desired microstep setting digitalWrite(M2_PIN, LOW); // Set to LOW or HIGH for desired microstep setting // Set the desired RPM and the max RPM float desiredRPM = 60; // Set the desired speed in rpm (revolutions per minute) float MaxRPM = 300; // Set max speed in rpm (revolutions per minute) // Calculate and set the desired and max speed in steps per second float speedStepsPerSec = (microstepSetting * stepsPerRevolution*desiredRPM) / 60.0; float Max_Speed_StepsPerSec = microstepSetting * stepsPerRevolution * MaxRPM / 60; // Specify max speed in steps/sec (converted from RPM) stepper.setMaxSpeed(Max_Speed_StepsPerSec); stepper.setSpeed(speedStepsPerSec); } void loop() { // Run the motor at constant speed stepper.runSpeed(); } |
1 RPM (Clock speed) – Full Steps
|
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 |
#include <AccelStepper.h> // Define stepper pins #define STEP_PIN 3 // Step pin #define DIR_PIN 2 // Direction pin // Microstepping control pins #define M0_PIN 7 #define M1_PIN 6 #define M2_PIN 5 // Steps per revolution for the motor const float stepsPerRevolution = 200; // Microstepping multiplier (1, 2, 4, 8, 16, or 32) int microstepSetting = 1; // AccelStepper instance in driver mode AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); void setup() { // Set microstepping pins as outputs pinMode(M0_PIN, OUTPUT); pinMode(M1_PIN, OUTPUT); pinMode(M2_PIN, OUTPUT); // Set microstepping mode (adjust as needed: HIGH or LOW) digitalWrite(M0_PIN, LOW); // Set to LOW or HIGH for desired microstep setting digitalWrite(M1_PIN, LOW); // Set to LOW or HIGH for desired microstep setting digitalWrite(M2_PIN, LOW); // Set to LOW or HIGH for desired microstep setting // Set the desired RPM and the max RPM float desiredRPM = 1; // Set the desired speed in rpm (revolutions per minute) float MaxRPM = 300; // Set max speed in rpm (revolutions per minute) // Calculate and set the desired and max speed in steps per second float speedStepsPerSec = (microstepSetting * stepsPerRevolution*desiredRPM) / 60.0; float Max_Speed_StepsPerSec = microstepSetting * stepsPerRevolution * MaxRPM / 60; // Specify max speed in steps/sec (converted from RPM) stepper.setMaxSpeed(Max_Speed_StepsPerSec); stepper.setSpeed(speedStepsPerSec); } void loop() { // Run the motor at constant speed stepper.runSpeed(); } |
1 RPM (Clock speed) – 1/32 Microstepping
|
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 |
#include <AccelStepper.h> // Define stepper pins #define STEP_PIN 3 // Step pin #define DIR_PIN 2 // Direction pin // Microstepping control pins #define M0_PIN 7 #define M1_PIN 6 #define M2_PIN 5 // Steps per revolution for the motor const float stepsPerRevolution = 200; // Microstepping multiplier (1, 2, 4, 8, 16, or 32) int microstepSetting = 32; // AccelStepper instance in driver mode AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); void setup() { // Set microstepping pins as outputs pinMode(M0_PIN, OUTPUT); pinMode(M1_PIN, OUTPUT); pinMode(M2_PIN, OUTPUT); // Set microstepping mode (adjust as needed: HIGH or LOW) digitalWrite(M0_PIN, HIGH); // Set to LOW or HIGH for desired microstep setting digitalWrite(M1_PIN, HIGH); // Set to LOW or HIGH for desired microstep setting digitalWrite(M2_PIN, HIGH); // Set to LOW or HIGH for desired microstep setting // Set the desired RPM and the max RPM float desiredRPM = 1; // Set the desired speed in rpm (revolutions per minute) float MaxRPM = 300; // Set max speed in rpm (revolutions per minute) // Calculate and set the desired and max speed in steps per second float speedStepsPerSec = (microstepSetting * stepsPerRevolution*desiredRPM) / 60.0; float Max_Speed_StepsPerSec = microstepSetting * stepsPerRevolution * MaxRPM / 60; // Specify max speed in steps/sec (converted from RPM) stepper.setMaxSpeed(Max_Speed_StepsPerSec); stepper.setSpeed(speedStepsPerSec); } void loop() { // Run the motor at constant speed stepper.runSpeed(); } |
Where to Buy:
Where to buy [Amazon Affiliate]:
DRV8825 Stepper Motor Driver: https://amzn.to/3CAjtDe
Arduino UNO: https://amzn.to/3U7xAoT
NEMA 14 – 14HS10-0404S Stepper Motor: https://amzn.to/4aKwQgD
