A4988 odd behaviour

I want to drive a NEMA17 HS4401S-24 stepper motor with a A4988 driver however I’m experiencing very odd issues of the driver randomly working and not. My A4988 is connected to my NodeMCU V3 (ESP) and I’m using this code:

const int dirPin = D0;
const int stepPin = D1;
const int stepsPerRevolution = 2000;

void setup()
{
	// Declare pins as Outputs
	pinMode(stepPin, OUTPUT);
	pinMode(dirPin, OUTPUT);
}
void loop()
{
	// Set motor direction clockwise
	digitalWrite(dirPin, HIGH);

	// Spin motor slowly
	for(int x = 0; x < stepsPerRevolution; x++)
	{
		digitalWrite(stepPin, HIGH);
		delayMicroseconds(2000);
		digitalWrite(stepPin, LOW);
		delayMicroseconds(2000);
	}
	delay(1000); // Wait a second
	
	// Set motor direction counterclockwise
	digitalWrite(dirPin, LOW);

	// Spin motor quickly
	for(int x = 0; x < stepsPerRevolution; x++)
	{
		digitalWrite(stepPin, HIGH);
		delayMicroseconds(1000);
		digitalWrite(stepPin, LOW);
		delayMicroseconds(1000);
	}
	delay(1000); // Wait a second
}

Note: i have also tried using AccelStepper and Stepper libraries and have experienced the same results.

The behaviour seems to be completely random. First time I uploaded this code to my Arduino, I than connected the power supply to the A4988 for the motor and it works, motor starts spinning as expected.

Then I disconnect the motor power supply to the A4988, I make a small change in code let’s say for the faster loop I change delayMicroseconds to 1500, upload the code and reconnect motor power supply and it’s no longer working. I revert the code and the code that used to work does not work anymore… Complete random. The motor holds with a lot of torque it’s impossible to move the shaft by hand maybe it also buzzes but does not move.

Ok I replace the code with AccelStepper or Stepper implementation with something simple like:

#include "AccelStepper.h"

#define motorInterfaceType 1

AccelStepper ballFeederStepper = AccelStepper(motorInterfaceType, D0, D1);

void setup() {
  ballFeederStepper.setMaxSpeed(1600);
  ballFeederStepper.setSpeed(600);
Serial.begin(9600); 
}

void loop() {

  ballFeederStepper.runSpeed();
}

And completely randomly it works or does not work.
Note: when I say it does not work I just mean that motor does not step/move but holds position with high torque. Than I disconnect it go away come back and at times it just starts working. Complete random…

The heat sink does not get hot, i made sure current limiting was set correctly… I’m not sure why this is happening. Is there some procedure to reset the driver after reprograming the ESP. It’s not the wiring. I did connect RST + SLEEP pins…

I’m kinda confused. I have many available news ones from 3 manufacturers (some 70 units) tried multiple and same issues :confused: I don’t think it’s a defective batch.

Hello.

The basic code you posted looks okay to me. Could you post more details about your setup? For example, what power supply and stepper motor are you using and what are you setting the VREF voltage to? Also, could you post some pictures of your setup showing all of your connections?

Brandon