# DIR pin will not reverse stepper?

**URL:** <https://forum.arduino.cc/t/dir-pin-will-not-reverse-stepper/1363473>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [March 13, 2025, 6:58pm UTC](https://forum.arduino.cc/t/dir-pin-will-not-reverse-stepper/1363473 "2025-03-13T18:58:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![1936yellowbuick](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/1936yellowbuick/32/1127186_2.png) [@1936yellowbuick](https://forum.arduino.cc/u/1936yellowbuick)\
**Post date:** [March 13, 2025, 6:58pm UTC](https://forum.arduino.cc/t/dir-pin-will-not-reverse-stepper/1363473/1 "2025-03-13T18:58:54Z")

</div>

Hello, I can get the stepper to reverse in the int goBack section ONLY by putting negative numbers in the setacceleration, setMaxSpeed and setSpeed numbers. I want to control it by pulling pin 2 low, which is the direction pin. Any ideas why this code will not produce what I am looking for?

```arduino
#include <AccelStepper.h>

//A1(15)= Retract switch, cut complete, going low.
//A2= 10k Feedrate Pot 0-5v
//A3(17)= Home Switch
//A4= Rapid to position pot, 10K 0-5V
//A6= Toggle switch to the left, goes low
//A7= Toggle switch in the middle, goes low
//D2= Direction on stepper driver
//D3= Step on stepper driver
//D4= MS3 
//D5= MS2 
//D6= MS1 
//D7= Enable on stepper driver
//D10= Start switch, goes low

AccelStepper stepper(1, 3, 2);

#define MS1 6
#define MS2 5
//#define MS3 5
//#define EN 7
//#define dir 2

void setup()
{
  pinMode(10, INPUT);  
  pinMode(A1, INPUT);  
  pinMode(A3, INPUT);
  pinMode(10, INPUT); 
  pinMode(7, OUTPUT); 
  pinMode(2, OUTPUT);  
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT); 
// pinMode(MS3, OUTPUT); 
  Serial.begin(9600);               
  digitalWrite(7, HIGH);
}
void loop()
{
  digitalWrite(7, HIGH);
  digitalWrite(2, HIGH);
  int feedValue = ((analogRead(A2)) / 5) + 50; //FEEDRATE FUNCTION
  int posValue = (1023 - analogRead(A4) * .75 + 500); //RAPID POSITION FUNCTION
  if (posValue < 300)
    posValue = 300;
  if ((digitalRead(10) == LOW) && (digitalRead(17) == LOW)) 
  { digitalWrite(7, LOW);
    runIn(posValue, feedValue);
    cut(feedValue, posValue);
  }
  stepper.setCurrentPosition(0); 
  digitalWrite(7, HIGH); 
  delay(500);
}
int runIn(int posValue, int feedValue)
{
  digitalWrite(2, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(MS1, HIGH); 
  digitalWrite(MS2, LOW);
  //digitalWrite(MS3, LOW);
  stepper.setAcceleration(2000); 
  stepper.setMaxSpeed(2200);   
  stepper.setSpeed(2000);
  stepper.runToNewPosition(posValue); 
  delay(700); 
}
int goBack()
{
  digitalWrite(7, LOW);
  digitalWrite(2, LOW);
  while (digitalRead(17) == HIGH) 
  {
    digitalWrite(MS1, LOW);
    digitalWrite(MS2, HIGH);
    digitalWrite(2, LOW);
    //digitalWrite(MS3, LOW);
    stepper.setAcceleration(200); 
    stepper.setMaxSpeed(5000);   
    stepper.setSpeed(500);
    digitalWrite(2, LOW);
    stepper.runSpeed();
  }
}
int cut(int feedValue, int posValue) {
  digitalWrite(7, LOW);
  int curPos1 = stepper.currentPosition();
  stepper.setMaxSpeed(feedValue);
  digitalWrite(MS1, HIGH);
  digitalWrite(MS2, HIGH);
  //digitalWrite(MS3, HIGH);
  stepper.move(4000);
  while (digitalRead(15) == HIGH && (digitalRead(10) == HIGH)) {
    stepper.run();
  }
  int curPos2 = stepper.currentPosition();
  delay(1000);     
  stepper.stop();
  digitalWrite(2, LOW);
  goBack();
} 

```

---

<div class="post-metadata">

**Author:** ![bobcousins](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bobcousins/32/221530_2.png) [@bobcousins](https://forum.arduino.cc/u/bobcousins)\
**Post date:** [March 13, 2025, 7:06pm UTC](https://forum.arduino.cc/t/dir-pin-will-not-reverse-stepper/1363473/2 "2025-03-13T19:06:09Z")

</div>

> [@1936yellowbuick](#):
>
> ONLY by putting negative numbers in the setacceleration, setMaxSpeed and setSpeed numbers.

setSpeed with a negative value runs in reverse; that is how you are supposed to do it. You should not be writing to pin 2, that is what AccelStepper does.

---

<div class="post-metadata">

**Author:** ![ahsrabrifat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ahsrabrifat/32/1126412_2.png) [@ahsrabrifat](https://forum.arduino.cc/u/ahsrabrifat)\
**Post date:** [March 14, 2025, 11:47am UTC](https://forum.arduino.cc/t/dir-pin-will-not-reverse-stepper/1363473/4 "2025-03-14T11:47:35Z")

</div>

In `goBack()`, `stepper.setSpeed(500);` should be `stepper.setSpeed(-500);` to move in reverse.

```arduino
int goBack()
{
  digitalWrite(7, LOW); // Enable stepper driver
  digitalWrite(2, LOW); // Set direction LOW (Reverse)
  
  stepper.setAcceleration(200); 
  stepper.setMaxSpeed(5000);   
  stepper.setSpeed(-500); // Negative speed to move in reverse

  // Keep running until the home switch (A3) is triggered
  while (digitalRead(17) == HIGH) 
  {
    stepper.runSpeed(); // Keep calling runSpeed() to move stepper
  }

  stepper.stop(); // Stop movement once home switch is reached
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 10, 2025, 11:47am UTC](https://forum.arduino.cc/t/dir-pin-will-not-reverse-stepper/1363473/5 "2025-09-10T11:47:59Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
