Pololu A4988 / Step motor accidentally changes direction

Hello, we are facing a strange issue that I do not really understand. The step motor is controlled by the AccelStepper lib. Basically the implementation looks ok.

But whenever the platform moved by motor hits an obstacle the motor changes the direction. My expectation would be that the motor used max. power in this situation or just stops. But why changing the direction?

The code for moving to the initial parking position looks quite simple (see below). I don’t see anything that may cause the direction change. Code initially based on… https://github.com/sidlauskaslukas/barbot

Any idea?

#include "AccelStepper.h"
#include "PololuMaestro.h"
#include "Configuration.h"

String serialBuffer = "";
String actions[TOTAL_ACTIONS];

int counter = 0;
int lastIndex = 0;

AccelStepper stepper(X_INTERFACE_TYPE, X_STEP_PIN, X_DIR_PIN); // Define a stepper and the pins it will use
MicroMaestro maestro(maestroSerial); // Define a servo controller

void setup() {
  Serial.begin(9600); // Serial port for debugging
  maestroSerial.begin(9600); // Servo controller
  Serial2.begin(9600); // Bluetooth module
  stepper.setMaxSpeed(X_MAX_SPEED); // Sets the maximum speed the X axis accelerate up to
  pinMode(X_ENDSTOP_PIN, INPUT_PULLUP); // Initialize endstop pin with the internal pull-up resistor enabled
  homeXAxis(); // Return the X axis to it's home position at the startup

void homeXAxis() {
  int endStopState = digitalRead(X_ENDSTOP_PIN);
  long lastLog = millis();

  Serial.println("Home X axis 1");
  while (endStopState == HIGH) {
    if (millis() - lastLog > 1000) {
      lastLog = millis();
      Serial.print("Current position: ");
      Serial.print("Distance to go: ");
    endStopState = digitalRead(X_ENDSTOP_PIN);

  Serial.println("Home X axis 2");

  stepper.moveTo(stepper.currentPosition() - 50);
  while (stepper.distanceToGo() != 0) {
    stepper.setSpeed(X_PARK_SPEED * -1);

  Serial.println("Home X axis 3");

  endStopState = digitalRead(X_ENDSTOP_PIN);

  while (endStopState == HIGH) {
    endStopState = digitalRead(X_ENDSTOP_PIN);

  Serial.println("Home X axis 4");

void loop() {


What are the voltage and current ratings of your stepper motor? Could you post pictures of your setup that show all connections and a video of the issue?


Hi Claire,

Thanks for your quick reply. This is this basic setup…

Will try sending some pics/video tomorrow.

Again to the main problem… It works partly. But wheneven the tabel (moved by the step motor) hits something (e.g. my hand) the motor goes to other direction. On software side this direction change is not detected. We don’t understand why the direction is changed - a stop when hitting an obstacle would be easier to understand.

Is there something like a security stop?

Cheers, JC

Hi Claire, hope the upload works :wink:

And also as video…

From your video it looks to me like the extra load in your system might be causing your motor to skip steps or your driver to overheat (or some combination of that). You could start troubleshooting by trying to slow your motor down a lot and see if it still has the issue. I also recommend checking that your current limit is not set too high for your carrier. It looks like you are not using one of our A4988 carriers, so you should contact the manufacturer of your board to ask what is appropriate.


Hi Claire, thanks for the update. You are right it’s not your original A4988. We had your A4988; but colleages burned it. Then a replacement was ordered.

From your knowledge do you think this might be the reason? Maybe the replacement is poor quality?

It don’t know which current is used. But the A4988 is getting really hot. The A4988 is connected to a 12V source.

Cheers, JC

It is possible your issue is coming from a bad board, but I think it is probably more likely that the current limit is just not set appropriately.


Hi Claire, we found the root cause of our issue - at the end it was quite simple.

The wiring between A4988 and step motor was slightly mixed. Unlickily the documentation of the motor pinout was not really clear.

Thanks for your support!

1 Like