Anyone familiar with Accelstepper library

Hi All

I am using a pololu high current stepper driver to drive a nema 23 stepper motor via arduino mega. I am using only two outputs for driving stepper motor i.e. step and direction.
I want to know how to reverse the direction of the motor. So far I could not find anything. The way I want it to work the that the stepper motor is driving a linear slide. Both ends of the linear slide has limit switches. I want to reverse the motor direction once it hits one of the limit switch.

I am just checking this code to run the motor. what should I do just to test the motor to rotate in reverse direction.

#include <AccelStepper.h>

AccelStepper myStepper(1, 2, 3); // pin 2 = step, pin 3 = direction


void setup() {
  myStepper.setMaxSpeed(800);
 
  myStepper.setSpeed(500);
  
}

void loop() 
{  
  myStepper.runSpeed();

}

Thanks for the help

I want to know how to reverse the direction of the motor.

The direction is determined automatically. If you are at position 200, and want to go to 400, direction is one thing. If you are at 400, and want to go to 200, direction is something else. In either case, the library determines the direction based on the current position and the moveTo() position.

The runSpeed() method only makes the motor move if it is not currently at the last commanded position. I don’t see that you’ve ever told the stepper where to go, so I can’t see how it is moving at all.

Thanks very much for the reply. Frankly I understood what you are implying but I cannot put those thoughts into a code. I am novice as far as programming goes.
And as far as moving the motor to a position goes, I dont know the position. I just want to run the stepper in one direction until it hits the limit switch. From there on I want it go reverse direction until it hits another limit switch at the opposite end. I want to use the stepper motor to drive a linear actuator/ slide.

And as far as moving the motor to a position goes, I dont know the position. I just want to run the stepper in one direction until it hits the limit switch.

So, assume it starts at 0. Call moveTo() to move to position. Call run() or runSpeed() to make that happen. Check the switch. If you are not at the limit, increment the position, and repeat.

When the limit is reached, begin decrementing the position, and calling run() and checking the other switch.

When the “dir” pin is high, the motor goes one direction. When the “dir” pin is low, it goes the other.

@jremington: Yes i pulled the direction pin wire from the arduino board and attached it to the vcc and ground to see the effect. It does reverse the direction depending on if the direction pin is high or low. Thanks for that advise.
But that is physical way. Can i accomplish the same in programming. ?

digitalWrite (directionPin, HIGH); digitalWrite (directionPin, LOW);

Shpaget: digitalWrite (directionPin, HIGH); digitalWrite (directionPin, LOW);

Don't do that. The library handles setting the pin high or low,. depending on where the stepper is relative to where you are telling it to go.

If you don't like the way AccelStepper works, don't use it. Use the regular Stepper library.

@Paul:

I really want to use the regular stepper library but the max RPM I can set for the stepper motor using regular stepper library is 300 RPM, which is too slow. Anything larger than 300, the stepper wont even turn and gives continuous beep . For my application I want to run it faster. That is why I am looking for other ways to drive the stepper.

PaulS: Don't do that. The library handles setting the pin high or low,. depending on where the stepper is relative to where you are telling it to go.

Oh, right. I misunderstood the issue. Anyway, recently I used this:

  potVal = analogRead (potPin);
  stepperVelocity = map (potVal, 0, 1023, 900, -900);  
  stepper1.setSpeed(stepperVelocity);
  stepper1.runSpeed();

It's a WIP for a stepper that would be controlled via a joystick. Works OK.

I still don't see what the problem is with using the AccelStepper library the way it is intended. Set the initial position to 0 on start up. If the right limit switch isn't pressed, increment the position, call moveTo() and run() or runSpeed(). Repeat until the limit switch IS pressed. Then, decrement the position, call moveTo() and run() or runSpeed(), and check the left limit switch. Repeat until the switch is pressed. If increment and decrement values of 1 are too small, increment or decrement by larger values.

Once you know the number of steps it takes to get from the start position to the right limit, and the number of steps it takes to get from the right limit to the left limit, you really don't even need to check the switches again, accelerate and run from left to right and right to left, simply telling the stepper to moveTo() whatever position means the right limit, and then moveTo() whatever position means the left limit. The library will figure out which direction it needs to rotate the stepper to get from where it is to where it needs to be, and will set the direction pin appropriately.

@PaulS Yes, I guess that's the right way to do it. I will try it today and will share my results. Thanks everyone for pitching in and sharing your opinions.

I tried the way PaulS mentioned but I dont like the way the stepper rotates. I don’t want any acceleration in the equation but it does nothing if I do not set the acceleration part inside the setup. It makes weird noises and the stepper vibrates while doing so. It just does not feels right.

Is there any other way or any other library which used 2 inputs, STEP and DIRECTION and i can go beyond 300rpm for the stepper. All I really want is to move the motor continuously at a set speed until it hits the limit switch and from there on move it in opposite direction.

Constant speed example is bang on and exactly what I want but it lacks the direction control.
Please advise. :frowning:

Anything larger than 300, the stepper wont even turn and gives continuous beep .

If you want the motor to rotate more quickly than that, you MUST accelerate to the desired speed. That is one of the main purposes of the AccelStepper library.

If I use the accelstepper library it can go upto 890 rpm with the code I posted earlier and which happens to be the same as constant speed example without any acceleration set.

If I use default stepper library it cannot go beyond 300 rpm. It just stops rotating at 301 rpm and give me a continuous beep sound.

I cannot use custom stepper library because it always take minimum of 4 inputs on the arduino to connect the stepper but I will run out of digital pwm outputs if I use 4 inputs just for stepper specially when I am using a dedicated driver to drive the stepper motor.

Accelstepper and custom stepper were the only two libraries so far I could find but each one has its own limitation.

If you have a stepper driver that takes step and direction signals from the Arduino it is very easy to control it without any library.

Set the direction pin HIGH or LOW as required and use this code to produce a single step

digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);

Repeat as often as you want and as fast as the motor will accept.

The motor will go faster if there is a higher supply voltage - but make sure the current limit is set correctly.

If you are looking for very high speeds you will almost certainly need to start slower and “accelerate”. If it doesn’t matter to the load on the motor I don’t think the acceleration would have to be smooth. Two or three speed steps may be enough.

As with other Arduino stuff - learn-by-doing.

…R