# Stepper motor direction

**URL:** <https://forum.arduino.cc/t/stepper-motor-direction/432490>\
**Category:** Programming\
**Created:** [January 21, 2017, 12:53pm UTC](https://forum.arduino.cc/t/stepper-motor-direction/432490 "2017-01-21T12:53:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![djgorandj](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@djgorandj](https://forum.arduino.cc/u/djgorandj)\
**Post date:** [January 21, 2017, 12:53pm UTC](https://forum.arduino.cc/t/stepper-motor-direction/432490/1 "2017-01-21T12:53:05Z")

</div>

Im using this code:

```cpp
#include <Stepper.h>
int current_position = 0;
const char direction_pin = 4;
const char step_pin = 5;
const char pot_pin = 0; //analog input 1
#define Limit01 7  
#define Limit02 8   
void setup()
{
pinMode(Limit01, INPUT);
pinMode(Limit02, INPUT);
pinMode(direction_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(pot_pin, INPUT);
}

void loop()
{
int  readvalue = analogRead(pot_pin);
readvalue = map(readvalue,0,1023,0,1151);
if (readvalue > current_position+20)
{
 step_motor_forward();
 current_position += 1;
}
else if (readvalue < current_position-20)
{
 step_motor_back();
 current_position -= 1;
}
}//end of loop

void step_motor_forward()
{
if (digitalRead(Limit02)) {}  
    
else {
digitalWrite(direction_pin, LOW);
digitalWrite(step_pin, HIGH);
digitalWrite(step_pin, LOW);
}
}

void step_motor_back()
{
if (digitalRead(Limit01)) {} 
    
else {
digitalWrite(direction_pin, LOW);
digitalWrite(step_pin, LOW);
digitalWrite(step_pin, HIGH);
}
}

```

How revers direction on stepper motor. Turn potentiometer to left motor direction left, turn potentiometer right stepper motor to left, need me to right. A4988 driver and arduino and nema 17. potentiometer is 10k.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 21, 2017, 1:14pm UTC](https://forum.arduino.cc/t/stepper-motor-direction/432490/2 "2017-01-21T13:14:37Z")

</div>

The first thing to do is to determine whether the pot has been turned to the left or right. Start by doing that and printing a message to indicate which way the motor should turn and nothing else.

---

<div class="post-metadata">

**Author:** ![djgorandj](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@djgorandj](https://forum.arduino.cc/u/djgorandj)\
**Post date:** [January 21, 2017, 1:34pm UTC](https://forum.arduino.cc/t/stepper-motor-direction/432490/3 "2017-01-21T13:34:19Z")

</div>

![](https://europe1.discourse-cdn.com/arduino/original/4X/c/c/9/cc9c7c756644289ca85d1537fa3ca7e79f2cbd9f.jpeg)  
This schematich, and to A0 connected potentiometer 10k.

---

<div class="post-metadata">

**Author:** ![Dan95](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Dan95](https://forum.arduino.cc/u/Dan95)\
**Post date:** [January 21, 2017, 3:01pm UTC](https://forum.arduino.cc/t/stepper-motor-direction/432490/4 "2017-01-21T15:01:30Z")

</div>

Don't you think that one of the two direction\_pin codes need to change??  
Normally in a step and direction setup direction CONTROLS the direction of the stepper.

they BOTH set direction\_pin LOW:

```cpp
void step_motor_forward()
  {
    if (digitalRead(Limit02)) {}  
    else {
    digitalWrite(direction_pin, LOW);  // SEE?? THIS IS SET LOW
    digitalWrite(step_pin, HIGH);
    digitalWrite(step_pin, LOW);
    }
  }

void step_motor_back()
  {
  if (digitalRead(Limit01)) {} 
  else {
    digitalWrite(direction_pin, LOW);  // THIS -ALSO- SETS 
    digitalWrite(step_pin, LOW);
    digitalWrite(step_pin, HIGH);
    }
  }

```

---

<div class="post-metadata">

**Author:** ![djgorandj](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@djgorandj](https://forum.arduino.cc/u/djgorandj)\
**Post date:** [January 21, 2017, 3:12pm UTC](https://forum.arduino.cc/t/stepper-motor-direction/432490/5 "2017-01-21T15:12:45Z")

</div>

Agen motor turn to left, to another direction. Change stepper and agen test, no another direction, only left or right.

---

<div class="post-metadata">

**Author:** ![djgorandj](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@djgorandj](https://forum.arduino.cc/u/djgorandj)\
**Post date:** [January 21, 2017, 3:53pm UTC](https://forum.arduino.cc/t/stepper-motor-direction/432490/6 "2017-01-21T15:53:51Z")

</div>

> **[Arduino and Two Stepper Motors](https://arduining.com/2015/05/30/arduino-and-two-stepper-motors/)**
>
>   This project consist in moving two stepper motors “simultaneously”. Really there is a loop where both motors are moved “one step at a time” to reach their final posit…

need me like this for one stepper motor and pot, with a4988 driver.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [January 21, 2017, 4:53pm UTC](https://forum.arduino.cc/t/stepper-motor-direction/432490/7 "2017-01-21T16:53:33Z")

</div>

If you are using a specialized stepper driver like an A4988 do NOT use the standard stepper library as it is not intended for them. Use the AccelStepper library or just write your own code,

The examples in this [Simple Stepper Code](http://forum.arduino.cc/index.php?topic=277692.0) can be used for testing and for understanding how the control works.

...R  
[Stepper Motor Basics](http://forum.arduino.cc/index.php?topic=284828.0)

---

<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:** [May 5, 2021, 10:17pm UTC](https://forum.arduino.cc/t/stepper-motor-direction/432490/8 "2021-05-05T22:17:39Z")

</div>

Closed on May 5, 2021, 10:17pm UTC


