Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- // Pin definitions
- #define STEP_PIN 18 // Step pin for DRV8825
- #define DIR_PIN 19 // Direction pin for DRV8825
- #define ENABLE_PIN 23 // Enable pin for DRV8825 (optional)
- // Stepper motor parameters
- int stepsPerRevolution = 200; // Motor-specific (commonly 200 steps/rev)
- int microstepping = 32; // Microstepping setting (DRV8825 jumpers)
- int delayHigh = 1500; // Duration HIGH in microseconds
- int delayLow = 1500; // Duration LOW in microseconds
- int totalSteps; // Total steps for one revolution
- void setup() {
- // Initialize pins
- pinMode(STEP_PIN, OUTPUT);
- pinMode(DIR_PIN, OUTPUT);
- pinMode(ENABLE_PIN, OUTPUT);
- // Enable the motor
- digitalWrite(ENABLE_PIN, LOW);
- // Set direction to forward
- digitalWrite(DIR_PIN, HIGH);
- // Calculate total steps per revolution with microstepping
- totalSteps = stepsPerRevolution * microstepping;
- Serial.begin(115200);
- Serial.println("Stepper motor initialized.");
- }
- void loop() {
- // Move one full revolution forward
- Serial.println("Moving forward one revolution...");
- moveStepper(totalSteps);
- // Pause for a moment
- delay(2000);
- // Change direction to backward
- digitalWrite(DIR_PIN, LOW);
- Serial.println("Moving backward one revolution...");
- moveStepper(totalSteps);
- // Pause for a moment before repeating
- delay(2000);
- }
- // Function to move the stepper motor a given number of steps
- void moveStepper(int steps) {
- for (int i = 0; i < steps; i++) {
- digitalWrite(STEP_PIN, HIGH); // Set STEP pin HIGH
- delayMicroseconds(delayHigh); // Wait for HIGH duration
- digitalWrite(STEP_PIN, LOW); // Set STEP pin LOW
- delayMicroseconds(delayLow); // Wait for LOW duration
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement