# Arduino, 2x Limit Switches, Stepper Motor not changing direction

**URL:** <https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [June 28, 2022, 7:44am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343 "2022-06-28T07:44:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gasPhase](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@gasPhase](https://forum.arduino.cc/u/gasPhase)\
**Post date:** [June 28, 2022, 7:44am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/1 "2022-06-28T07:44:29Z")

</div>

I'm beginner to Arduino, including some electronics, and coding, sorry 😊

My project for now is simple, 1x Stepper Motor to move Clockwise then  
Counter Clockwise, triggered by 2x Limit Switches

Problem; limit switches show that they trigger via the Serial Monitor,  
but the Motor does not respond by changing direction

 ![IMG_3052.PNG](https://europe1.discourse-cdn.com/arduino/original/4X/6/5/4/654ed07a44f7f1ce9ab600b85b9daea8f953e4a9.jpeg)

![Screenshot 2022-06-15 at 09.12.37](https://europe1.discourse-cdn.com/arduino/original/4X/4/4/c/44cdea61f84da43f49982ee16958cb18e5f24d53.png)

hope i did all of that correct 🙈

```cpp
[code]
/*
   Created by ArduinoGetStarted.com

   This example code is in the public domain

   Tutorial page: https://arduinogetstarted.com/tutorials/arduino-stepper-motor-and-limit-switch
*/

#include <ezButton.h>
#include <AccelStepper.h>

#define DIRECTION_CCW -1
#define DIRECTION_CW 1

#define STATE_CHANGE_DIR 1
#define STATE_MOVE 2
#define STATE_MOVING 3

#define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type)

ezButton limitSwitch_1(A0); // create ezButton object that attach to pin A0;
ezButton limitSwitch_2(A1); // create ezButton object that attach to pin A1;

AccelStepper stepper(AccelStepper::FULL4WIRE, 7, 6, 5, 4);

int stepperState = STATE_MOVE;
int direction = DIRECTION_CW;
long targetPos = 0;

void setup() {
  Serial.begin(9600);

  limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds
  limitSwitch_2.setDebounceTime(50); // set debounce time to 50 milliseconds

  stepper.setMaxSpeed(500.0); // set the maximum speed
  stepper.setAcceleration(50.0); // set acceleration
  stepper.setSpeed(100); // set initial speed
  stepper.setCurrentPosition(0); // set position
}

void loop() {
  limitSwitch_1.loop(); // MUST call the loop() function first
  limitSwitch_2.loop(); // MUST call the loop() function first

  if (limitSwitch_1.isPressed()) {
    stepperState = STATE_CHANGE_DIR;
    Serial.println(F("The limit switch 1: TOUCHED"));
  }

  if (limitSwitch_2.isPressed()) {
    stepperState = STATE_CHANGE_DIR;
    Serial.println(F("The limit switch 2: TOUCHED"));
  }

  switch (stepperState) {
    case STATE_CHANGE_DIR:
      direction *= -1; // change direction
      Serial.print(F("The direction -> "));
      if (direction == DIRECTION_CW)
        Serial.println(F("CLOCKWISE"));
      else
        Serial.println(F("ANTI-CLOCKWISE"));

      stepperState = STATE_MOVE; // after changing direction, go to the next state to move the motor
      break;

    case STATE_MOVE:
      targetPos = direction * MAX_POSITION;
      stepper.setCurrentPosition(0); // set position
      stepper.moveTo(targetPos);
      stepperState = STATE_MOVING; // after moving, go to the next state to keep the motor moving infinity
      break;

    case STATE_MOVING: // without this state, the move will stop after reaching maximum position
      if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position
        stepper.setCurrentPosition(0); // reset position to 0
        stepper.moveTo(targetPos); // move the motor to maximum position again
      }
      break;
  }

  stepper.run(); // MUST be called in loop() function
}
[/code]

```

---

<div class="post-metadata">

**Author:** ![pmagowan](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pmagowan](https://forum.arduino.cc/u/pmagowan)\
**Post date:** [June 28, 2022, 8:02am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/2 "2022-06-28T08:02:50Z")

</div>

Does 1\*=-1 change direction?

---

<div class="post-metadata">

**Author:** ![gasPhase](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@gasPhase](https://forum.arduino.cc/u/gasPhase)\
**Post date:** [June 28, 2022, 8:08am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/3 "2022-06-28T08:08:48Z")

</div>

in my mind yes, but when altering those numbers, nothing  
changed... i changed many things, coding and wiring wise,  
nothing changed

the code as you can see is from [ArduinoGetStarted.com](http://ArduinoGetStarted.com),  
so i assumed it will work, the engineer that works for them  
couldn't help me either

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [June 28, 2022, 1:17pm UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/4 "2022-06-28T13:17:32Z")

</div>

You posted a pretty picture but it is of no use to me. Posting a schematic with all the connections as you have wired it will help a lot. Include links to technical data on all of the hardware items. Links to axon are about useless.

---

<div class="post-metadata">

**Author:** ![gasPhase](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@gasPhase](https://forum.arduino.cc/u/gasPhase)\
**Post date:** [June 28, 2022, 2:06pm UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/5 "2022-06-28T14:06:06Z")

</div>

to me everything schematic wise shows in my pretty picture,  
please show what in a schematic will show what the picture  
does not?

[Stepper\_Motor\_Shield\_For\_Arduino\_DRV8825\_\_SKU\_DRI0023](https://wiki.dfrobot.com/Stepper_Motor_Shield_For_Arduino_DRV8825__SKU_DRI0023)

DFRobot Shield DRV8825 SKU:DRI0023

- Support 3.3V and 5V operating voltage
- Suitable for two-phase and four-wire stepper motor
- Board with two DRV8825 driver chip and a heat sink has been mounted.
- Input Voltage：8.2-45V DC (Just power the stepper motor driver), 1.6A output current per coil
- Driving Pins: D4,D5,D6,D7,D8,D12
- Squeeze connector, quite easy and convenient.
- 8 channel digital I/O pins & 6 channel Analog input pins
- DRV8825 Microstepping bipolar stepper motor driver
- Six different microstep resolutions (full-step, 1/2-step, 1/4-step, 1/8-step, 1/16-step, 1/32-step)
- Compatible with Arduino UNO R3, Leonardo, Mega and other controllers, with full port extensions. Support XBee, XBee Wi-Fi, Bluetooth and RF modules, ensure that the needs of your wireless communications.
- There is a switch on the board Xbee wireless interfaces corner. PROG stop wireless module, available USB programming. RUN time can use the wireless communication module.

Hybrid Stepper Motor 0.5Nm NEMA17 0.9 deg/step [42BYGHM809](https://cdn.sparkfun.com/datasheets/Robotics/42BYGHM809.PDF)

- 0.5Nm (48N.cm) - 4 Wire
- Step Angle (degrees) : 0.9 Deg (400 steps/Rev)
- 2-Phase (Rated Current: 1.7A/Phase)
- Rated Voltage: 3V
- 5mm Diameter Drive Shaft
- NEMA 17 form factor

---

<div class="post-metadata">

**Author:** ![gasPhase](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@gasPhase](https://forum.arduino.cc/u/gasPhase)\
**Post date:** [June 28, 2022, 2:42pm UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/6 "2022-06-28T14:42:41Z")

</div>

for interest sake this was the pretty picture from  
[ArduinoGetStarted.com](http://ArduinoGetStarted.com) where the code and  
instruction were (their wiring on the limit switches  
seemed weird, i still tried it, it didn't work)

 ![arduino-stepper-motor-two-limit-switch-wiring-diagram](https://europe1.discourse-cdn.com/arduino/original/4X/2/e/e/2ee6710b259344ebd8674ce99e7603363d8b9253.jpeg)

---

<div class="post-metadata">

**Author:** ![cncnotes](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cncnotes/32/1098836_2.png) [@cncnotes](https://forum.arduino.cc/u/cncnotes)\
**Post date:** [June 28, 2022, 3:37pm UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/7 "2022-06-28T15:37:21Z")

</div>

Print out _targetPos_ after this line and see what you are getting:

```cpp
    case STATE_MOVE:
      targetPos = direction * MAX_POSITION;

```

It seems like your connections are good since you are seeing the correct values reported earlier in the program.

---

<div class="post-metadata">

**Author:** ![gasPhase](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@gasPhase](https://forum.arduino.cc/u/gasPhase)\
**Post date:** [June 28, 2022, 4:14pm UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/8 "2022-06-28T16:14:23Z")

</div>

ok i see the target position you show, what does  
"Print out targetPos" mean, preferably show me  
what it means? 😊 i like this, feels exciting

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [June 28, 2022, 4:27pm UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/9 "2022-06-28T16:27:05Z")

</div>

> [@gasPhase](#):
>
> what does  
> "Print out targetPos"

Maybe like this:

```arduino
 case STATE_MOVE:
      targetPos = direction * MAX_POSITION;
      Serial.print("case STATE_MOVE, current target position = ");
      Serial.println(targetPos);
      stepper.setCurrentPosition(0); // set position
      stepper.moveTo(targetPos);
      //delay(50); // just in case the serial port gets flooded
      stepperState = STATE_MOVING; // after moving, go to the next state to keep the motor moving infinity
      break;

```

You may need to slow execution down (with a delay(), just for development) to avoid flooding the serial port.

---

<div class="post-metadata">

**Author:** ![gasPhase](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@gasPhase](https://forum.arduino.cc/u/gasPhase)\
**Post date:** [June 29, 2022, 10:21am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/10 "2022-06-29T10:21:52Z")

</div>

thanks, this sadly didn't work, nothing changed,  
the delay(50) wasn't needed

---

<div class="post-metadata">

**Author:** ![cncnotes](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cncnotes/32/1098836_2.png) [@cncnotes](https://forum.arduino.cc/u/cncnotes)\
**Post date:** [June 29, 2022, 12:28pm UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/11 "2022-06-29T12:28:07Z")

</div>

> [@groundFungus](#):
>
> `Serial.println(targetPos);`

Can you please let us know what you are seeing printed out?

---

<div class="post-metadata">

**Author:** ![gasPhase](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@gasPhase](https://forum.arduino.cc/u/gasPhase)\
**Post date:** [June 29, 2022, 1:19pm UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/13 "2022-06-29T13:19:15Z")

</div>

can i just use Serial Monitor?  
if not, where do i put this Serial.println(targetPos); in the code?

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [June 29, 2022, 2:15pm UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/14 "2022-06-29T14:15:06Z")

</div>

> [@gasPhase](#):
>
> if not, where do i put this Serial.println(targetPos); in the code?

See post #9. I actually put the prints in the STATE\_MOVE case and you will see the results printed in serial monitor. To copy for us to see, highlight some of the output and hit ctrl-c to copy and then paste the copied text into a post.

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 30, 2022, 1:54am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/15 "2022-06-30T01:54:43Z")

</div>

Can someone explain to me the logic behind debouncing a limit switch.  
My pea-brain tells me that once the switch opens it must be at the limit.  
So why debounce. Do you expect another limit behind the limit?  
That needless debounce code might also interfere with stepper speed.  
Leo..

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [June 30, 2022, 2:14am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/16 "2022-06-30T02:14:05Z")

</div>

I don't get the obsession with debounce, either.

If the first action is the important one, debounce is not necessary. Another function of software debounce is to verify that the closure was a valid switch action and not noise, but for a limit switch I guess you would want to stop the motor first and ask questions later.

I am a fan of hardware debounce. I usually get flamed for saying this but I don't much care. That is how I have been doing it for near half a century and I don't plan to stop. I have found that if a 0.1uF cap won't debounce a switch then it is garbage.

---

<div class="post-metadata">

**Author:** ![cncnotes](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cncnotes/32/1098836_2.png) [@cncnotes](https://forum.arduino.cc/u/cncnotes)\
**Post date:** [June 30, 2022, 2:20am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/17 "2022-06-30T02:20:23Z")

</div>

I am a fan of hardware be-bounce as well. Very easy to do and reduces one source of gremlins. 👍

Though may not matter much for a limit switch, except if using it to zero out your axes, may get minute errors. Which again may or may not matter depending on how much your program is relying on machine zero.

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [June 30, 2022, 2:22am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/18 "2022-06-30T02:22:45Z")

</div>

@cncnotes, doing the 2 step home\* like grbl, Marlin and others do would seem to minimize those errors.

\*fast to the limit switch, back up and slow back to limit switch then stand off.

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 30, 2022, 5:02am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/19 "2022-06-30T05:02:59Z")

</div>

> [@groundFungus](#):
>
> Another function of software debounce is to verify that the closure was a valid switch action and not noise...

But this is a limit switch, and you should use an opening contact, for safety.  
An opening contact is more reliable than a closing one.  
Leo..

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [June 30, 2022, 5:31am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/20 "2022-06-30T05:31:44Z")

</div>

I don't like running hot wires all over the machine. Seems, to me, running ground to the switches is somewhat safer. Every 3D printer that I have worked on uses grounded switches so there must be something to it.

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 30, 2022, 5:43am UTC](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343/21 "2022-06-30T05:43:27Z")

</div>

Yes, IMHO the switch should be between pin and ground,  
with internal pull up, and a 10n cap across the switch to kill RF.  
Normally closed, and opening when the limit is reached.  
Leo..

[Next page](https://forum.arduino.cc/t/arduino-2x-limit-switches-stepper-motor-not-changing-direction/1007343.md?page=2)
