Arduino Stepper Motor Library: All You Need To Know

Arduino Stepper Motor Library: All You Need To Know
#include <Stepper.h>

// Define the stepper motor pins

#define STEPS 200

Then #define DIR_PIN 8

#define STEP_PIN 9

// Initialize the stepper motor object

Stepper motor(STEPS, DIR_PIN, STEP_PIN);

void setup() {

  // Set the speed of the motor in RPM

  motor.setSpeed(60);

}

void loop() {

  // Move the motor 200 steps clockwise

  motor.step(200);

  delay(500);

  // Move the motor 200 steps counterclockwise

  motor.step(-200);

  delay(500);

}
#include <AccelStepper.h>

// Define the stepper motor pins

#define STEPS 200

Then #define DIR_PIN 8

#define STEP_PIN 9

// Initialize the stepper motor object

AccelStepper motor(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);

void setup() {

  // Set the speed of the motor in RPM

  motor.setMaxSpeed(1000);

  // Set the acceleration and deceleration of the motor

  motor.setAcceleration(200);

  motor.setDeceleration(200);

  // Set the microstepping mode of the motor to 1/8th

  motor.setMicrostep(8);

}

void loop() {

  // Move the motor 200 steps clockwise

  motor.moveTo(200);

  motor.runToPosition();

  delay(500);

  // Move the motor 200 steps counterclockwise

  motor.moveTo(-200);

  motor.runToPosition();

  delay(500);

}

Subscribe to our Newsletter “Electrical Insights Daily” to get the latest updates in Electrical Engineering. You can also Follow us LinkedIn to see our latest posts.

Sharing is Caring

Similar Posts

Leave a Reply

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

Advertisement