Using 'minus' sign as negative for Stepper motor not working


// Stepper - Version: 1.1.3
#include <Stepper.h>

/*This is using the stepper motor from the kit.
It is limited in use. Need to add stepper library. Library added from the Web Editor.
*/
int stepsPerRevolution=2048;
int motSpeed=10;
int dt=500;
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
void setup() {
Serial.begin(9600);
myStepper.setSpeed(motSpeed);
}

void loop() {
myStepper.step(stepsPerRevolution);
delay(dt);
myStepper.step(-stepsPerRevolution);
delay(dt);
exit(0);

    
    
}

Is your setup like https://docs.arduino.cc/learn/electronics/stepper-motors ?

How is it not working?

Does it step properly in the forward direction?

And if so, with the myStepper.step(-stepsPerRevolution); does it continue to step in the forward direction ?

Try this:

Stepper myStepper(stepsPerRevolution, 8,10,9,11);

This is a common mistake to make with steppers: assuming they can accelerate instantly to a given speed. Here you've set the speed to 10rpm at 2048 steps/rev, so that you are jumping from stationary to 341 steps per second without any ramping of the velocity - this may or may not be physically possible with the motor you have.

Normally Stepper is limited to low speeds because of this, and the AccelStepper library is much more likely to work well.

However this is not the only possible issue that might be affecting you - mis-wiring of the motor could have such a symptom too.

Rest assured the Stepper.step() call does work in both directions - the issue lies somewhere else.

I did try to reposition the pin list to (8,10,9,11) without success to reverse the motor. I then used pins (2,3,4,5) without success. Checked connections and all seemed tight. Still not reversing. I'll probably take it all apart and reinstall the pins and connections. There is something I'm not seeing.

What happens if you use the negative value first ?

What is the purpose of the exit(0) ?

I used the setup that DaveX suggested. The motor steps properly in a clockwise direction. Then it delays and steps again in the SAME clockwise direction. So, even with the myStepper.step(-stepsPerRevolution) it continues in the SAME direction. There must be something in the way I have my motor connected to the Uno board. I'll take it apart and try again. I don't know what else to try.

I tried the negative value before the positive. No change. Both had the motor going in the same direction.

I inserted the exit function to stop the loop. I wasn't sure if I could just close the editor and then turn off the usb to the board.

It seems you only want to execute that block once.
If so, then move those lines to setup(), and keep loop() empty.
Leo..

Do you have a way of measuring the current? You might check if the current though the coils is as expected--full current through each coil, but the the signs change depending on step 1,2,3,4

delay() should be unsigned long.
Not sure if that's a problem.

12-15RPM is absolute max for those motors, but without ramping up..?
Try a lower speed first.
Leo..

Please show exactly how you wired it up. I tested your code with my setup and it worked that way. Does it turn really smooth in the one direction or does it 'stutter'? And is there any load on the motor?

Mine worked fine with 10RPM and no ramp up ( and no load ).

This is now working. I looked at the datasheet to find the color wire that matched the coil. Then I tried a different pin sequence. Below is what works with my particular stepper. It now runs both clockwise and counterclockwise smoothly. Below is the setup.

Stepper myStepper(stepsPerRevolution, 10,9,11,8)

Code setup:

#include <Stepper.h>
int stepsPerRevolution=2048;
int motSpeed=5;
int dt=500;
Stepper myStepper(stepsPerRevolution, 10,9,11,8);

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
myStepper.setSpeed(motSpeed);
}

void loop() {
  // put your main code here, to run repeatedly:
myStepper.step(stepsPerRevolution);
delay(dt);
myStepper.step(-stepsPerRevolution);
delay(dt);

}

Thanks to everyone who offered solutions. I have learned about steppers and coding.
Jim

Thank you for that solution. I had the same problem, and your permutation helped me.

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