I am using the AccelStepper library for Arduino and an A4988 driver in order to control a Nema17 stepper motor. When I establish the stepper class instance, the stepper motor ends up rotating/vibrating unexpectedly. This is the code:
#include <AccelStepper.h>
#define dirPin 3
#define stepPin 2
#define motorInterfaceType 1
AccelStepper stepper(AccelStepper::DRIVER, 2, 3);
void setup() {
}
void loop() {
}
This is a very basic question, but does anyone know how to get rid of this initial rotation+vibration when the stepper class instance is created ? See connections (on my setup the dir and step pin assignments are reversed contrary to what the screenshot has), power supply, and videos below:
https://drive.google.com/drive/folders/1-NCwAs3XnZ24EcwHJVlGVXpBPW16voSh?usp=sharing
When I program movement, the stepper goes through this same initial unexpected, clunky vibration/rotation and then goes into the actual instructed rotation (different code from what i posted initially, was just trying to see where that clunky rotation was coming from with that initial code and I see it is from the class instance).
This is the code I use for movement:
/*Example sketch to control a stepper motor with A4988 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);
// 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);
}
void loop() {
}
The stepper motor I am using is below: