arduinoarduino-unostepper

Arduino Uno stepper direction


I am having trouble getting a stepper motor to rotate counterclockwise with code written for an Arduino Uno.

This is a simple setup that uses a push button to move the stepper as you hold the button.

Originally the code was written to move the stepper clockwise and I changed the myStepMotor.step to have a -1 instead of 1. From my understanding this should cause the stepper to go in the other direction, but with both a positive or negative number it still rotates in the clockwise direction.

#include <Stepper.h>

const int myStepsPerRev = 64; 
char k_ccw; 
int myStepsTook = 0; 
// allocate a stepper motor object for pins 8 through 11: 
Stepper myStepMotor (myStepsPerRev, 8, 9, 10, 11); 

void setup() { 
  pinMode(3, INPUT); 
  myStepMotor.setSpeed(60); // set the motor speed for 60rpm 
  myStepsTook = 0; // initialize steps taken to zero 
  Serial.begin(9600); // initialize the serial port 
} 
  
void loop() { 
  k_ccw = digitalRead(3); 
  if (k_ccw == LOW) {
    delay(125); 
    myStepsTook++; 
    Serial.print("Steps Taken: "); 
    Serial.println(myStepsTook);
    myStepMotor.step(-1); 
  }
}

Solution

  • The pin sequence you want is a bit unexpected; if you connected it in the "standard" way, the 28BYJ-48 should be initialised in software like so:

    Stepper myStepMotor(myStepsPerRev, 8, 10, 9, 11);
    

    It should then work properly in both directions.

    See here for more information, should you need it.