NEMA 17 Stepper with A4988 Driver Rotating Randomly on Startup

Thank-you. I’ve just tried that, and I get the exact same results as before. Misbehaviour on startup, LEDs initialise correctly, then just a high-pitched whine from the motor.

Here’s my current code:

#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Arduino.h>
#include <LedControl.h>


#define SENSOR_PIN 10 //For hall effect sensor

//LED output pins
#define GR_LED_PIN 4
#define RD_LED_PIN 5

//Motor control pins
#define MT_STEP 2
#define MT_DIR 3
AccelStepper turnMot(1, MT_STEP,MT_DIR);

//LED Display Pins
#define RO_CLK 7
#define RO_DIN 8
#define RO_LOAD 9

//Occupancy Sensor
#define OCC_PIN A0

//Below defines and creates the LED controller object
LedControl dispLED = LedControl(RO_DIN,RO_CLK,RO_LOAD,1);

void setup() {

  //Set up the LED pins:
  pinMode(GR_LED_PIN, OUTPUT);
  pinMode(RD_LED_PIN, OUTPUT);

  //Set up the hall effect sensor
  pinMode(SENSOR_PIN, INPUT);

  // put your setup code here, to run once:
  //Initialise the LED controller
  dispLED.shutdown(0,false);
  /* Set the brightness to a medium values */
  dispLED.setIntensity(0,12);
  /* and clear the display */
  dispLED.clearDisplay(0);
  disNumLed (1234);
  delay(1000);
  dispLED.clearDisplay(0);

  //Initialise the stepper motor
  turnMot.setMaxSpeed(159);
  turnMot.setSpeed(159);

  //Calibrate the turntable
  calibrateTurntable();
}

void loop() {
  // put your main code here, to run repeatedly:

}

void calibrateTurntable(){
  /*Below boolean acts as a flag as to 
   * whether the turntable should be
   * calibrating or not.
   */
  boolean turning = true;

  while (turning){
    turnMot.runToNewPosition(1);
    delay(10);
    if (digitalRead(SENSOR_PIN) == LOW){
      turning = false;
    }
  }
}

void disNumLed (int disNum){
  //Takes an integer and displays it on the 4-digit LED display
  int firstDigit;
  int secondDigit;
  int thirdDigit;
  int fourthDigit;

  /*The 4-digit single module has digit 1 on left and 4 on the right.  Hence,
   * it needs to be added in reverse order.  firstDigit to position 3, through
   * to fourthDigit in position 0.
   */
  
  dispLED.clearDisplay(0);
  firstDigit = disNum % 10; //Return remainder of division operation (modulus)
  dispLED.setDigit(0,3,firstDigit,false);
  
  if (disNum > 9){
    //If int is still greater than 9, then there should be another digit (base 10)
    disNum /= 10; //Move to next digit
    secondDigit = disNum % 10;
    dispLED.setDigit(0,2,secondDigit,false);
  }
  if (disNum > 9){
    disNum /= 10; //Move to next digit
    thirdDigit = disNum % 10;
    dispLED.setDigit(0,1,thirdDigit,false);
  }
  if (disNum > 9){
    //Remainder would be above 10
    disNum /= 10; //Move to next digit
    fourthDigit = disNum; //As it's the only digit left
    dispLED.setDigit(0,0,fourthDigit,false);
  }
}

Thinking about it, I realised I'd seen this issue before, when I had the A4988 and the stepper motor working off a breadboard. It was covered in this thread here. So my next step, on the weekend, will be to put either an LED or a 10K resistor between the step pin of the A4988 and ground, then test with that.

I just realized I had intended to say in my earlier Reply that you should create a version of your program with none of the LED stuff and just concentrate on getting the stepper to work. Unfortunately some Arduino libraries do not work together.

...R

Tbdanny:
Thinking about it, I realised I'd seen this issue before, when I had the A4988 and the stepper motor working off a breadboard. It was covered in this thread here. So my next step, on the weekend, will be to put either an LED or a 10K resistor between the step pin of the A4988 and ground, then test with that.

Your link is broken it should be NEMA 17 Stepper with A4988 Driver Rotating Randomly on Startup - Motors, Mechanics, Power and CNC - Arduino Forum

More to the point - that is a very recent Thread that YOU started and it would have made a lot more sense if you had just continued with it so we have all the info in one place.

If you click Report to Moderator you can ask to have the Threads merged.

...R

I've just tried pasting that correct link in, as a link in the previous reply, but for some reason it's not taking. I also thought that issue had been resolved. It occurred when I tested the A4988 on the breadboard, but disappeared when I soldered everything to the circuit board. It's only just reappeared now that everything's mounted under the layout.

I've used the report to moderator function to ask that they merge this with the earlier one.

Tbdanny:
I've just tried pasting that correct link in, as a link in the previous reply, but for some reason it's not taking.

I think you need to switch to the ViewSource editor - the right-most button. It is the mode that I use all the time. I have it selected in my preferences.

Have you tried your stepper program without any of the LED code?

...R

Not yet. I'm working today (27th) and tomorrow, so I'll only have time to try it on the weekend. My plan of attack is:

  1. Existing arrangement without LED code.
  2. Adding LED between ground and A4988 step pin, then trying without LED code.
  3. If 2 works, then trying it with the LED code.

I’ve had a chance to try it. Option 1, the sketch without the LED code, produced the same results. So I went to option 2, installing an LED between the step pin and ground. This did work when I tried it on a breadboard, and it did resolve the ‘rotating randomly on startup’ issue. However, the motor is still not moving when it’s supposed to. I can feel that it’s energised, when I try to turn it by hand, however it won’t move at all.

Following this, I tried the following steps:

  1. Took the Hall Effect sensor out of the calibrateTurntable() function, so that the while loop should always remain true. Tried versions of the below code with both the AccelStepper and BasicStepper libraries:
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Arduino.h>
//#include <LedControl.h>


#define SENSOR_PIN 10 //For hall effect sensor

//LED output pins
#define GR_LED_PIN 4
#define RD_LED_PIN 5

//Motor control pins
#define MT_STEP 2
#define MT_DIR 3
AccelStepper turnMot(1, MT_STEP, MT_DIR);

//LED Display Pins
/*#define RO_CLK 7
#define RO_DIN 8
#define RO_LOAD 9*/

//Occupancy Sensor
#define OCC_PIN A0

//Below defines and creates the LED controller object
//LedControl dispLED = LedControl(RO_DIN,RO_CLK,RO_LOAD,1);

void setup() {

  //Set up the LED pins:
  pinMode(GR_LED_PIN, OUTPUT);
  pinMode(RD_LED_PIN, OUTPUT);

  //Set up the hall effect sensor
  pinMode(SENSOR_PIN, INPUT);

  // put your setup code here, to run once:
  //Initialise the LED controller
  //dispLED.shutdown(0,false);
  /* Set the brightness to a medium values */
  //dispLED.setIntensity(0,12);
  /* and clear the display */
  /*dispLED.clearDisplay(0);
  disNumLed (1234);
  delay(1000);
  dispLED.clearDisplay(0);*/

  //Initialise the stepper motor
  turnMot.setMaxSpeed(159);
  turnMot.setSpeed(159);

  //Calibrate the turntable
  calibrateTurntable();
}

void loop() {
  // put your main code here, to run repeatedly:

}

void calibrateTurntable(){
  /*Below boolean acts as a flag as to 
   * whether the turntable should be
   * calibrating or not.
   */
  boolean turning = true;

  while (turning){
    turnMot.runToNewPosition(1);
    delay(10);
    /*if (digitalRead(SENSOR_PIN) == LOW){
      turning = false;
    }*/
  }
}

/*void disNumLed (int disNum){
  //Takes an integer and displays it on the 4-digit LED display
  int firstDigit;
  int secondDigit;
  int thirdDigit;
  int fourthDigit;

  /*The 4-digit single module has digit 1 on left and 4 on the right.  Hence,
   * it needs to be added in reverse order.  firstDigit to position 3, through
   * to fourthDigit in position 0.
   */
  
  /*dispLED.clearDisplay(0);
  firstDigit = disNum % 10; //Return remainder of division operation (modulus)
  dispLED.setDigit(0,3,firstDigit,false);
  
  if (disNum > 9){
    //If int is still greater than 9, then there should be another digit (base 10)
    disNum /= 10; //Move to next digit
    secondDigit = disNum % 10;
    dispLED.setDigit(0,2,secondDigit,false);
  }
  if (disNum > 9){
    disNum /= 10; //Move to next digit
    thirdDigit = disNum % 10;
    dispLED.setDigit(0,1,thirdDigit,false);
  }
  if (disNum > 9){
    //Remainder would be above 10
    disNum /= 10; //Move to next digit
    fourthDigit = disNum; //As it's the only digit left
    dispLED.setDigit(0,0,fourthDigit,false);
  }
}*/
  1. Tried setting the MS1, MS2 and MS3 pins of the A4988 to LOW, so that no microstepping would be involved.

  2. Tried replacing the A4988 with another one.

None of this made the stepper motor move when it was supposed to. One thing I’ve noticed is that pin 3 of the Arduino, which is used for the direction, is a PWM pin. When I was doing my workbench testing, none of the pins I was using (pins 8 & 9) were PWM ones. Would this make a difference? If not, what else can I try?

I can say it’s not a question of the load on the motor. When I installed the motor, I rigged the board with the A4988 on it up temporarily, and tested it with my heaviest locomotive. It moved smoothly, without any issues. It’s only since I installed it properly that it’s been playing up like this.

When you run into a problem make things as simple as possible.

  • The Pololu A4988 web page has a good wiring diagram. It does not have any "funny" external components.
  • And try it with the simple program in my link in Reply #1

Stick with that until the problem is sorted.

...R

That is the wiring diagram I followed. I only added a DIP switch between +5v and the MS pins, to allow setting of the microstepping.

What confuses me is that this worked on my test bench. Then when I put it under the layout, it started playing up. No change to the board.

Tbdanny:
That is the wiring diagram I followed. I only added a DIP switch between +5v and the MS pins, to allow setting of the microstepping.

You mentioned LEDs and stuff elsewhere so I got very confused.

What confuses me is that this worked on my test bench. Then when I put it under the layout, it started playing up. No change to the board.

There must have been some difference.

Can you go back to the test bench version?

...R

The test bench version is what I put under the layout. Literally all I did was solder the wires to the prototyping shield I’m using to connect everything to the Arduino, and connect the power supplies. That was all I did. Worked on test bench, didn’t work under the layout.

That sounds like the sort of problem you have to figure out yourself simply because you are the guy with the misbehaving system.

Can you take it out from under the layout and go back to the bench?

...R

Here's where it gets weird. I took the motor driver board out from under the layout, and removed the 'hold down' LED I'd put in place last night. This took it back to the last known working version of the hardware. Following this, I tested it on the workbench, using the same 12v power supply for the motor, the same sketch I'd used for load testing, and a spare NEMA17 motor. It worked.

Following this, I connected it back up under the layout, using alligator clip leads to connect the wires I'd cut to remove the motor. Using the same code, it worked again. I then soldered these wires back in place, isolating them with heat shrink material, the same way I had the first time. I then tested it again, with the exact same code, and it worked.

I then loaded my calibration sketch, with the LED display code active, and that worked as well. But I'm still baffled as to why it didn't work the first time. The only explanation I can think of is that there may have been a bad solder joint in one of the wires. Having said that, I didn't attach the driver board to the case with double-sided tape this time, instead just leaving it loose. I'm glad it's working, but I'm concerned it may happen again.

Tbdanny:
Here's where it gets weird. I took the motor driver board out from under the layout, and removed the 'hold down' LED I'd put in place last night.

In Reply #29 you said you followed the Pololu wiring diagram. It does not have any "hold down LED".

The only explanation I can think of is that there may have been a bad solder joint in one of the wires

That is certainly a valid possibility.

...R

Yeah, the hold down LED was something that came up when I tested it on the breadboard. I then made the motor driver board without it, then added the LED last night in an attempt to try and solve the problem. It didn't work.

Robin, thank-you for all your help. I've now got it up and running, and I'm ready to program the program itself.

I do just have one final question. When the stepper motor isn't moving, I can hear a high-pitched whine coming from it. I'd like to be able to turn this off, if possible. Currently, the ENABLE pin on the A4988 is held to ground. I'm not quite sure of the function of this pin. Would setting it to HIGH when the motor isn't moving cut power to the motor and thus the sound? Furthermore, would the motor hold position if this were the case? It's running at 16 microsteps, which gives me the precision I need for the turntable.

If you use the ENABLE pin to disable the motor power the noise will probably stop but there will be nothing to make the motor hold position. Micro-stepping is achieved by making the motor coils pull against each other. When you re-enable the motor it may jump the nearest full-step position.

I think if you want micro-stepping precision you will have to put up with the noise. Drown it out with the DCC sound on your trains :slight_smile:

...R