Stepper Motor - Serial. How to rotate CCW?

Hello, I have the code below:

Basically when i type the number into serial monitor it will rotate the stepper motor "X" degrees.
At the moment, if i type 100 in serial monitor it will rotate 100 degrees forward.
but when i type -100 it wont turn it 100 degrees backwards.

Any Suggestions, I have tried Stepper Library and it seemed to have a mind of its own.

<//define pin numbers
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11

int ang = 0;
//for the delay
int mils = 1;

void setup() {

pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
Serial.begin(9600);

}

void loop() {
// Read input from serial Monitor if available
if (Serial.available()) {
ang = Serial.parseInt();
Serial.println("Read " + String(ang) + " degrees");
rotate(ang);
}

}

void rotate(int angle)
{
int numberOfRounds = (int) angle * 1.42222; // angle*512/360
for (int i = 0; i < numberOfRounds; i++) {

//1000
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(mils);

//1100
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(mils);

//0100
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(mils);

//0110
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(mils);

//0011
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
delay(mils);

//0010
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(mils);

//0001
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(mils);

//1001
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(mils);

}
// Switch off all phases
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}>

No code has a "mind on its own" not even chatGPT

The bug has his hands on the keyboard and is staring at the screen (Yes you yourself)

If a library does not behave like you want you haven't yet fully understood how the library works.
Same thing with rotation counter-clockwise for rotating ccw you have to switch the IOpins in the reversed order.

Again: Knew knowledge makes live easier.
With stepper-motor-drivers like the DRV8825 or TMC2209 you have a simple two wire interface step and direction
change DIR-pin from low to HIGH and the stepper-motor runs the opposite direction by simply creating step-pulses on a single IO-pin.

best regards Stefan

1 Like

would you know how to reverse the motor with the current code? without changing the driver board?

This is what I use:
FORWARD
write(1, 0, 0, 0);
write(1, 1, 0, 0);
write(0, 1, 0, 0);
write(0, 1, 1, 0);
write(0, 0, 1, 0);
write(0, 0, 1, 1);
write(0, 0, 0, 1);
write(1, 0, 0, 1);

REVERSE
write(0, 0, 0, 1);
write(0, 0, 1, 1);
write(0, 0, 1, 0);
write(0, 1, 1, 0);
write(0, 1, 0, 0);
write(1, 1, 0, 0);
write(1, 0, 0, 0);
write(1, 0, 0, 1);

Would you happen to have an example? im still learning code so it takes me ages to figure out without seeing the code.

Just take the code you already have to move it forward and use it again in reverse order to make it move in the opposite direction.

Example FORWARD > 1000, 1100, 0100, 0110, 0010, 0011, 0001, 1001
Example REVERSE > 1001, 0001, 0011, 0010, 0110, 0100, 1100, 1000

Thank you, ill Try this

that is what user @essejcds posted
simply switching the IP-pins in reversed order

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