0% found this document useful (0 votes)
17 views2 pages

Stepper

This sketch controls a stepper motor using an Arduino, AccelStepper library, and DRV8825 stepper motor driver. It defines connections to the motor and driver, creates an AccelStepper instance, and runs the motor forward and backward at varying speeds and positions, completing multiple revolutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Stepper

This sketch controls a stepper motor using an Arduino, AccelStepper library, and DRV8825 stepper motor driver. It defines connections to the motor and driver, creates an AccelStepper instance, and runs the motor forward and backward at varying speeds and positions, completing multiple revolutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*Example sketch to control a stepper motor with DRV8825 stepper motor driver,

AccelStepper library and Arduino: number of steps or revolutions. More info:


https://www.makerguides.com */

// Include the AccelStepper library:


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

You might also like