1

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); 
  }
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
BoatingPuppy
  • 31
  • 1
  • 5
  • 1
    Hard to tell without knowing which stepper motor it is, and how it is wired, but I have seen a few with unusual wiring; you could try Stepper myStepMotor (myStepsPerRev, 8, 10, 9, 11); instead and see what happens. – ocrdu Oct 29 '20 at 10:17
  • Sorry about that, I should have mentioned the stepper motor and the motor driver. I am using the 28BYJ-48 stepper motor and the ULN2003 stepper motor driver module. I will try what you suggested with rearranging the pins in the code. – BoatingPuppy Oct 29 '20 at 21:23
  • 1
    I changed the pin order for the Stepper myStepMotor to the way suggested and it fixed the issue. It works properly now and the stepper motor goes in two different directions depending on what button is pushed. Thank you for the help! – BoatingPuppy Oct 30 '20 at 13:37
  • Great, and you're welcome. – ocrdu Oct 30 '20 at 13:58

1 Answers1

1

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.

ocrdu
  • 2,172
  • 6
  • 15
  • 22