Stepper motor direction program

Good day All,
I need assistance with a programming issue I have on my stepper motor. Attached is a diagram of my circuit in question. The program was initially without an enable pin connection and direction control was working perfectly. Problem was with the stepper motor controller always activated their was a small dc voltage sitting on the motor output that would cause the motor to heat up.

I then included the enable function into the program to only turn on the stepper motor controller when button is pressed for either clockwise or anti-clockwise control.

The heat issue is resolved now with the addition of the enable function in the program but the unit now sometimes turn anti-clockwise when it is suppose to be turning clockwise and visa versa.

Could someone please assist me with an amendment to my program that can resolve this thanks.

// testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW

byte directionPin = 9;
byte stepPin = 8;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;
byte Enablepin = 12;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;

byte ledPin = 13;

unsigned long Millis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 1; // milliseconds

void setup() { 

  Serial.println("Starting Stepper Demo with millis()");

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonCWpin, INPUT_PULLUP);
  pinMode(buttonCCWpin, INPUT_PULLUP);

void loop() { 
  Millis = millis();
  digitalWrite(Enablepin, HIGH); 

void readButtons() {
  buttonCCWpressed = false;
  buttonCWpressed = false;
  if (digitalRead(buttonCWpin) == LOW) {
    buttonCWpressed = true;
  if (digitalRead(buttonCCWpin) == LOW) {
    buttonCCWpressed = true;

void actOnButtons() {
  if (buttonCWpressed == true) {
     digitalWrite(Enablepin, LOW);
    digitalWrite(directionPin, LOW);
  if (buttonCCWpressed == true) {
     digitalWrite(Enablepin, LOW);
    digitalWrite(directionPin, HIGH);

void singleStep() {
  if (Millis - prevStepMillis >= millisBetweenSteps) {
    prevStepMillis += millisBetweenSteps;
    digitalWrite(stepPin, HIGH);
    digitalWrite(stepPin, LOW);

It is normal for stepper motors to get very hot - uncomfortable to touch. They are designed for that. If you disable the motor you cannot be certain that it will retain its position.

I never disable my motors.

Whatever may be the arguments about disabling a motor if it in not used for (say) 5 minutes it makes no sense to disable the motor between individual steps the way your program does.

The problem with the wrong direction may be because you are not allowing enough time between enabling the motor driver and giving it a command.


I think @Robin2 is right. The driver needs a little bit of time to get back to work again after chilling. As I had the same problem with heating - in my case I was not concerned with the stepper, as these guys can still operate being heated up - the driver (drv8825), I used the same method and added a short delay of 10ms to cure the problem.

This is now working flawlessly over a couple of months being used in an industrial environment on a daily 8h shift basis.

Thank you. I will try the delay and confirm if it resolves this issue. Is their a possibility you can highlight where I should put this delay?

Freddie911: Thank you. I will try the delay and confirm if it resolves this issue. Is their a possibility you can highlight where I should put this delay?

Immediately after the line of code that enables the Stepper Driver.