# Accelstepper swap direction in the code

**URL:** <https://forum.arduino.cc/t/accelstepper-swap-direction-in-the-code/1121663>\
**Category:** Programming\
**Created:** [May 1, 2023, 3:27am UTC](https://forum.arduino.cc/t/accelstepper-swap-direction-in-the-code/1121663 "2023-05-01T03:27:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterbuilt](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@peterbuilt](https://forum.arduino.cc/u/peterbuilt)\
**Post date:** [May 1, 2023, 3:27am UTC](https://forum.arduino.cc/t/accelstepper-swap-direction-in-the-code/1121663/1 "2023-05-01T03:27:32Z")

</div>

I have written a program that moves a stepper motor. I have used the accelstepper library. I have now found out I have the hardware upside down and when properly installed the motor moves the wrong way. My programming is all good but I just need the motor to go CW instead of CCW and vice versa. Is there a quick code or pin change I can do to save me going all through my code?

---

<div class="post-metadata">

**Author:** ![dlloyd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dlloyd/32/751911_2.png) [@dlloyd](https://forum.arduino.cc/u/dlloyd)\
**Post date:** [May 1, 2023, 3:41am UTC](https://forum.arduino.cc/t/accelstepper-swap-direction-in-the-code/1121663/2 "2023-05-01T03:41:13Z")

</div>

I think just using negative speed, something like:

```arduino
stepper.setSpeed(-motorSpeed);

```

> [@peterbuilt](#):
>
> I have used the accelstepper library

The -'ve sign works here with the the accelstepper library ...

> **[Arduino-Controlling-Stepper-B.ino - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/360447750612611073)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

---

<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:** [May 1, 2023, 4:01am UTC](https://forum.arduino.cc/t/accelstepper-swap-direction-in-the-code/1121663/3 "2023-05-01T04:01:58Z")

</div>

What stepper and stepper driver are you using?

For the A4988, DRV8825 drivers, disconnect the motor 4 pin connector at the driver output, flip it 180 degrees and re-connect. That will reverse the motor response.

**Make sure that the driver is not powered** when disconnecting or connecting the motor from the driver.

---

<div class="post-metadata">

**Author:** ![peterbuilt](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@peterbuilt](https://forum.arduino.cc/u/peterbuilt)\
**Post date:** [May 1, 2023, 4:19am UTC](https://forum.arduino.cc/t/accelstepper-swap-direction-in-the-code/1121663/4 "2023-05-01T04:19:03Z")

</div>

It is a stepper online 34IP65-85  
It is a DM860T driver

---

<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:** [October 28, 2023, 4:19am UTC](https://forum.arduino.cc/t/accelstepper-swap-direction-in-the-code/1121663/5 "2023-10-28T04:19:33Z")

</div>

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