Unexpected Initial Clunky Vibration and Rotation from Nema17 using A4988 Driver

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:

Hello.

We did not write that library, so we can only offer limited support for it. If you would like to do some general troubleshooting of your system, I can try to help with that. Can you include pictures that show your connections to the A4988 carrier? It looks like it might be suspended by the wires in your test; I suggest not letting it dangle from the wires, especially if you are having problems, since intermittent connections could be adding to it.

Does the motion happen while the program is being uploaded or when the program is actually running? You might try switching to other pins on your Arduino for STEP/DIR to see if that helps at all. If not, you could also try adding some pull-up resistors.

Brandon



These are my connections

I have connect an external power supply (24V 2A). I am not sure if I wired incorrectly or if I have a faulty driver.

Also I switched the pins, it seems the issue is consistent for different I/O pins on Arduino.

I do not see any obvious problems with your connections; however, I suggest adding the capacitor shown in the minimal wiring diagram on the A4988 carrier’s product page.

If the driver works fine after the initial rotation/jitter, then it is almost certainly not a problem with the driver itself. Could you try running the sample code from this blog post? If it works fine, that would point toward something in the AccelStepper library causing the problem.

Brandon



I added the capacitor suggested (mine is 100mF/25 V). When I run the code on the blog post, it looks like this (see Pololu Blog Post Code run video):

https://drive.google.com/drive/folders/1-NCwAs3XnZ24EcwHJVlGVXpBPW16voSh?usp=sharing

It starts running and then starts oddly clicking but not moving?

It looks like your setup changed quite a bit. Aside from the Arduino changing, are you still using the same power supply and stepper motor, or is this a different setup altogether?

What did you set the VREF voltage to on your A4988 carrier? Can you try running it again without any load on your stepper motor?

Also, the code in the blog posts uses pin 2 for STEP and pin 3 for DIR, but the connections in your most recent pictures are different. Did you modify the code? If so, did you make any other modifications?

Brandon