NEMA 17 with A4988 Runs Smoothly During Code Upload but Moves Erratically Afterward

Problem Description:

I'm controlling two NEMA 17 stepper motors with A4988 drivers and an Arduino Uno. I'm experiencing a peculiar issue where one motor runs perfectly smooth during code upload but becomes erratic afterward, while the second motor works correctly in both scenarios.

The Specific Problem:

During upload: Both motors run smoothly

After upload:

Motor #1: Jerky, inconsistent movement, sometimes stalling or missing steps

Motor #2: Continues to work perfectly
The same code produces different behaviors between the two motors

Hardware Setup:

  • Arduino Uno
  • Two A4988 drivers (one for each motor)
  • Two NEMA 17 stepper motors (identical models)
  • Power Supply: 19V, 3.42A (both motors connected in parallel)
  • USB power from computer during programming

Circuit Configuration:

  • 19V, 3.42A Power Supply
  • A4988 #1 ── NEMA 17 #1 (problematic)
  • A4988 #2 ── NEMA 17 #2 (working correctly)

What I've Tried:

  • Verified power supply - 19V, 3.42A should be sufficient for both motors

  • Added decoupling capacitors (470µF) near each A4988

  • Verified all connections - No loose wires or poor connections

  • Added proper initialization in setup()

Current Code:

#include <Arduino.h>
#include <AccelStepper.h>
const int STEPA = 3;
const int DIRA = 4;
const int ENA = 2;
const int STEPB = 13;
const int DIRB = 8;
const int ENB = 12;
// const int EM = 8;
#define motorInterfaceType 1

AccelStepper StepperA(motorInterfaceType, STEPA, DIRA);
AccelStepper StepperB(motorInterfaceType, STEPB, DIRB);

void setup() {
  Serial.begin(115200);
  //pinMode(EM, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  StepperA.setMaxSpeed(2500);
  StepperA.setAcceleration(100);
  StepperA.setSpeed(2000);

  StepperB.setMaxSpeed(2000);
  StepperB.setAcceleration(100);
  StepperB.setSpeed(2500);
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
}


int prev_place[] = {0, 0};
bool running = true;
void loop() {
  // ALWAYS RUN THE MOTORS
  StepperA.run();
  StepperB.run();

  // Only read commands when motors are idle
  if (StepperA.distanceToGo() == 0 && StepperB.distanceToGo() == 0) {
    digitalWrite(ENA, HIGH);
    digitalWrite(ENB, HIGH);

    if (Serial.available() > 0) {

      String cmd = Serial.readStringUntil('\n');
      cmd.trim();

      int dx = 0;
      int dy = 0;

      if (cmd == "F")      { dx = 0;  dy = 200; }
      else if (cmd == "B") { dx = 0;  dy = -200; }
      else if (cmd == "R") { dx = 200; dy = 0; }
      else if (cmd == "L") { dx = -200;dy = 0; }
      else if (cmd == "D1") { dx = -100;dy = 100; }
      else if (cmd == "D2") { dx = 1000;dy = 1000; }
      else if (cmd == "Q") { StepperA.stop(); StepperB.stop();}

      int da = dx + dy;
      int db = dx - dy;
      // Serial.write);
      if(da>0 || da<0){
        digitalWrite(ENA, LOW);
      }
      if(db>0 || db<0){
        digitalWrite(ENB, LOW);
      }
      int dadeg = (36 * da) / (10 * PI);
      int dbdeg = (36 * db) / (10 * PI);
      Serial.print(dadeg*10000);
      StepperA.move(dadeg / 1.8);
      StepperB.move(dbdeg / 1.8);
    }
  }
}

Key Observations:
Power: 19V should be within A4988 specs (8-35V), 3.42A should be enough for two motors

During upload: Both motors work perfectly (as a matter of fact, i don't why they move while code is being uploaded)

After upload: Only one motor becomes problematic

The problematic motor/driver combination is consistent (problem follows the specific A4988)

Current sharing: Both VREF pots set to same voltage

Heating: Neither A4988 gets excessively hot

What is the issue and how to solve?

I am new to this forum and any advice would be appreciated!

BONUS CLUE: If I hold the reset button on the Arduino, for as long as I hold it, it keeps rotating the motors perfectly, when i leave it they stop. Also, they were working perfectly 3 days ago. We just plugged them in today and this problem occured.

Welcome to the Arduino forum. But, without showing how you actually have everything wired, there is not much we can do. Can you make a block diagram, with boxes for each piece and lines connecting each piece, just as you have it wired?

The fact that any motor is running during program compile/upload is concerning as no part of your program is controlling the motors. So something is definitely not wired correctly or you are not using stepper motors.

1 Like

The A4988 driver can handle at most 1 Ampere/winding without forced air cooling. To what value did you set the current limit?

"StepperA.setMaxSpeed(2500);,
StepperB.setMaxSpeed(2000);"

Reduce these numbers by a factor of 4 (625, 500) and try it.

Do not use Pin 13.

Which motor is "one motor?" Which pins?

Which motor is "second motor?" Which pins?

During upload: Both motors work perfectly (as a matter of fact, i don't why they move while code is being uploaded)

BONUS CLUE: If I hold the reset button on the Arduino, for as long as I hold it, it keeps rotating the motors perfectly, when i leave it they stop. Also, they were working perfectly 3 days ago. We just plugged them in today and this problem occured.

As @Paul_KD7HB said in post #2 this means that your code is not running the motors, your wiring arrangement is running them with out code.

the circuit design

I couldn’t really attach an image. So i designed it on cirkit designer. Also, Kindly note the following: Cirkit designer doesn’t really have a capacitor, so please just imagine that it is there, also the ioref pin is actually a 5v pin on my Arduino Uno so, please just imagine that the ioref is actually a 5v. I can that doesn’t work here’s the link:

Yes, but the thing is, it IS listening to my commands, and also i am able to upload a different code with a different set of commands and it is listening to that as well. its just that the motor moves in both direction wonkily on command but smoothly while the code is being uploaded by itself.

STEPA is the one moving wonkily and STEPB is moving fine. Also, out of curiosity, why should one not use pin 13?

I have attached a circuit diagram in a comment below, kindly check. Thanks!

I have a heat sink attached and have checked and tuned the a4988 with a multimeter to the right voltage.

I tried decreasing the motor speed, and that did not work. Though, thanks for the suggestion!

Come on! Did the speed change? That is what the change was for!

Pin 13 has the on-board, "L" LED... why that causes problems in some sketches, I do not know. I stay away from it, except for using it for the L-LED and clock signal.

The only program being run during upload is the upload program in your Arduino!

1 Like

I tried your sketch in a simulation, and the sketch using PIN 13 failed just as you described.

I moved PIN 13 to ANY OTHER PIN (Pin 10 in the simulation), and your sketch worked.

Your sketch with Pin 10 replacing Pin 13
#include <Arduino.h>
#include <AccelStepper.h>

const int STEPA = 3;
const int DIRA = 4;
const int ENA = 2;

// const int STEPB = 13;
const int STEPB = 10;
const int DIRB = 8;
const int ENB = 12;

// const int EM = 8;
#define motorInterfaceType 1

AccelStepper StepperA(motorInterfaceType, STEPA, DIRA);
AccelStepper StepperB(motorInterfaceType, STEPB, DIRB);

void setup() {
  Serial.begin(115200);
  Serial.println();
  //pinMode(EM, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  StepperA.setMaxSpeed(2500);
  StepperA.setAcceleration(100);
  StepperA.setSpeed(2000);

  StepperB.setMaxSpeed(2000);
  StepperB.setAcceleration(100);
  StepperB.setSpeed(2500);
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
}


int prev_place[] = { 0, 0 };
bool running = true;
void loop() {
  // ALWAYS RUN THE MOTORS
  StepperA.run();
  StepperB.run();

  // Only read commands when motors are idle
  if (StepperA.distanceToGo() == 0 && StepperB.distanceToGo() == 0) {
    digitalWrite(ENA, HIGH);
    digitalWrite(ENB, HIGH);

    if (Serial.available() > 0) {

      String cmd = Serial.readStringUntil('\n');
      cmd.trim();

      int dx = 0;
      int dy = 0;

      if (cmd == "F") {
        dx = 0;
        dy = 200;
      } else if (cmd == "B") {
        dx = 0;
        dy = -200;
      } else if (cmd == "R") {
        dx = 200;
        dy = 0;
      } else if (cmd == "L") {
        dx = -200;
        dy = 0;
      } else if (cmd == "D1") {
        dx = -100;
        dy = 100;
      } else if (cmd == "D2") {
        dx = 1000;
        dy = 1000;
      } else if (cmd == "Q") {
        StepperA.stop();
        StepperB.stop();
      }

      int da = dx + dy;
      int db = dx - dy;
      // Serial.write);
      if (da > 0 || da < 0) {
        digitalWrite(ENA, LOW);
      }
      if (db > 0 || db < 0) {
        digitalWrite(ENB, LOW);
      }
      int dadeg = (36 * da) / (10 * PI);
      int dbdeg = (36 * db) / (10 * PI);
      Serial.print(dadeg * 10000);
      StepperA.move(dadeg / 1.8);
      StepperB.move(dbdeg / 1.8);
    }
  }
}
diagram.json for wokwi simulation
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 4.8, "left": -0.5, "attrs": {} },
    { "type": "wokwi-a4988", "id": "drv1", "top": -91.2, "left": 139.2, "attrs": {} },
    { "type": "wokwi-a4988", "id": "drv2", "top": -235.2, "left": 139.2, "attrs": {} },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -296.84, "left": 192, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": -9.6, "left": 201, "attrs": {} },
    {
      "type": "wokwi-stepper-motor",
      "id": "stepper1",
      "top": -307.21,
      "left": 214.62,
      "attrs": { "size": "8", "arrow": "white" }
    },
    {
      "type": "wokwi-stepper-motor",
      "id": "stepper2",
      "top": -172.81,
      "left": 214.62,
      "attrs": { "size": "8", "arrow": "white" }
    },
    { "type": "wokwi-text", "id": "text1", "top": -144, "left": 297.6, "attrs": { "text": "A" } },
    {
      "type": "wokwi-text",
      "id": "text2",
      "top": -278.4,
      "left": 297.6,
      "attrs": { "text": "B" }
    }
  ],
  "connections": [
    [ "drv1:SLEEP", "drv1:RESET", "green", [ "h-9.6", "v-9.6", "h-9.6" ] ],
    [ "drv2:SLEEP", "drv2:RESET", "green", [ "h-9.6", "v-9.6" ] ],
    [ "vcc1:VCC", "drv2:VMOT", "red", [ "v0" ] ],
    [ "vcc1:VCC", "drv2:VDD", "red", [ "v0" ] ],
    [ "gnd1:GND", "drv1:GND.1", "black", [ "v0" ] ],
    [ "gnd1:GND", "drv1:GND.2", "black", [ "v0" ] ],
    [ "gnd1:GND", "drv2:GND.1", "black", [ "v0" ] ],
    [ "gnd1:GND", "drv2:GND.2", "black", [ "v0" ] ],
    [ "vcc1:VCC", "drv1:VMOT", "red", [ "v0" ] ],
    [ "vcc1:VCC", "drv1:VDD", "red", [ "v0" ] ],
    [ "drv2:2B", "stepper1:A+", "green", [ "h0" ] ],
    [ "drv2:2A", "stepper1:A-", "green", [ "h0" ] ],
    [ "drv2:1A", "stepper1:B-", "green", [ "h0" ] ],
    [ "drv2:1B", "stepper1:B+", "green", [ "h0" ] ],
    [ "drv1:2B", "stepper2:A+", "green", [ "h0" ] ],
    [ "drv1:1A", "stepper2:B-", "green", [ "h0" ] ],
    [ "drv1:1B", "stepper2:B+", "green", [ "h0" ] ],
    [ "drv1:2A", "stepper2:A-", "green", [ "h0" ] ],
    [ "nano:4", "drv1:DIR", "green", [ "v0" ] ],
    [ "nano:3", "drv1:STEP", "green", [ "v0" ] ],
    [ "nano:2", "drv1:ENABLE", "green", [ "v0" ] ],
    [ "nano:8", "drv2:DIR", "green", [ "v0" ] ],
    [ "nano:10", "drv2:STEP", "green", [ "v0" ] ],
    [ "nano:12", "drv2:ENABLE", "green", [ "v0" ] ],
    [ "nano:13", "drv2:ENABLE", "green", [ "v9.6", "h-19.2", "v-307.2" ] ]
  ],
  "dependencies": {}
}

I needed to add a Serial.println() to wake the serial line, but all commands (F, B, L, R) made both steppers work (D1, D2 were only one-stepper movements for skid-steering). Strange output: -376037603760-3760018384 (this results from F, B, L, R, D1, D2)

There was really not a change, to be honest.

I completely understand that and it makes sense. But why, then are the motors moving? That is what I cannot understand. Moreover, the motors also keep moving smoothly for as long as I press the reset button but when I leave it, the stop. Also, when I write the command to move them, one of them moves weirdly.

I shifted the pin 13 to the pin 11. But still, the same problem is occurring. However, I think it is so, because the one where pin 13 is involved is working fine. It is the other one that is moving wonkily. Just to keep it out there as an option, could it be that the motor has some issue?

What is the voltage rating for each motor?