Using AccelStepper to move motor desired amount of steps and hold for desired amount of time

Per my last post, I have gotten rid of the Adafruit motorshield and have been working on the circuit seen in the picture below. I am now attempting to code with the AccelStepper library to create the desired behavior for my stepper motor. I have attempted to piece together some code from examples, other projects etc. and I have gotten it to successfully turn at different speeds but that is about as far as I got. My desired stepper motor behavior is for the stepper to turn an [X] amount of steps and then hold for an [X] amount of time. The closest I have gotten is with the code pasted below. I was attempting to make the stepper move 8 steps and then delay for 1 second. The code, however, seems to make the delay between every step. I am not sure how to make the delay to be between a certain amount of steps. An additional benefit would be if I could get the stepper motor to move to a predetermined position at start up, however that is not necessary for my application, only the aforementioned behavior. I apologize in advance for any obvious mistakes as I have zero experience with coding.

#include <AccelStepper.h>

AccelStepper stepper1(1,2,3);



void setup() {

stepper1.setMaxSpeed(1000);

}

void loop() {
stepper1.setSpeed(75);

stepper1.moveTo(8);
delay(1000);

stepper1.runSpeed();
}

Your code used the moveTo function. The moveTo function uses absolute coordinates. If you issue moveTo(8) the stepper will go to 8. If you issue moveTo(8) again, nothing will happen as the stepper is already there. The move function is relative so it will move x steps for each move(x).

You must call run, as often as possible (or any of the non-blocking run functions). The run function will return 0 when the motor has stopped moving at the commanded position.

Make the code non-blocking with millis() timing (instead of using delay()).
Some non-blocking timing tutorials:
Blink without delay().
Blink without delay detailed explanation
Beginner's guide to millis().
Several things at a time.

I find the MobaToolls stepper library easier to use.

A simple blocking code using MobaTools stepper to move 8 steps pause 1 second and repeat endlessly.

#include <MobaTools.h>

const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;

int stepsToMove = 8;
unsigned long stepDelay = 1000; // milliseconds

MoToStepper stepper( 200, STEPDIR );

void setup()
{
   Serial.begin(115200);

   stepper.attach( stepPin, dirPin );   
   stepper.setSpeedSteps(750);  // = 75 steps/second (steps in 10 seconds)
   stepper.setRampLen(10);
   stepper.setZero();
}

void loop()
{
    stepper.move(stepsToMove);
    while(stepper.distanceToGo() > 0);
    delay(stepDelay);
}

Install MobaTools using the IDE library manager.

1 Like

Appreciate the response. I will try using this library tomorrow. Frankly, I’m only using AccelStepper because it was used in the tutorial I was following.

Here is a link to the documentation. MobaTools/MobaTools-243-en.pdf at master · MicroBahner/MobaTools · GitHub

Then I recommend to
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

writing code will become much easier for you and you will proceed faster if you have learned these basiscs.

I second to use the MobaTools library. The big advantage of the mobatools is that the step-pulses are created "in the background"
This enables to run code in parallel to the step-pulse-creation without the need to keep the loop looping fast.
do a single call to the stepper-motor-function and forget

Anyway learning to write non-blocking code is still essential to keep your code responsive to button-presses etc.

Here is a version of a stepper-demo with serial printing that makes visible what the code is doing.
The code has some very useful features like only print if a value has changed.

Such serial printing is essential to analyse the codes behaviour
this is what you see printed

Setup-Start
top of loop will rotate stepper for 8 steps
 entering while-loop
distanceToGo()=8
distanceToGo()=7
distanceToGo()=6
distanceToGo()=5
distanceToGo()=4
distanceToGo()=3
distanceToGo()=2
distanceToGo()=1
distanceToGo()=0
while-loop done now waiting
waiting OVER
top of loop will rotate stepper for 8 steps
 entering while-loop
distanceToGo()=8
distanceToGo()=7
distanceToGo()=6
distanceToGo()=5
distanceToGo()=4
distanceToGo()=3
distanceToGo()=2
distanceToGo()=1
distanceToGo()=0
while-loop done now waiting

best regards Stefan

I recommend you to take a look at the basics of how stepper motors work,if you are new to programming, so you understand what the libraries do.

//Need to nitialize stepPin as output. 
 for (int x = 0; x < steps; x++) {
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(tdelay); //Time between pulses(steps), determines the speed.
      }

The code works as intended however I am encountering an issue. I have tested the code for over 30 mins and it worked fine moving the stepper 8 steps and then pausing for 1 second before repeating. However, it seems that upon connecting and disconnecting the com port the stepper motor has now become stuck. Whenever I plug in the arduino the stepper motor attempts to execute the code but the behavior displayed suggests the motor is "stuck" in a spot. It will wait 1 second then attempt to move but all I see is the stepper jiggle in place and then repeat. I have tried resetting the board as well as changing steps/delay but to no avail. Any ideas what might be causing this?

I have nothing off the top of my head.

What are the part numbers of the stepper motor and the stepper driver? Please post data sheets or links to where you got them.

What is the stepper motor's power supply?

The stepper motor is a NEMA 11 bipolar pancake stepper motor. I have provided the link from where I purchased it. They have a datasheet as well.

I am using the USB-COM port on my laptop to power the board and upload the code.

Did you change the step frequency (motor speed)?

And the stepper driver data, please.

Rated Current/phase: 0.5 A

That is on the ragged edge for USB to supply. I really would suggest an external supply that can provide the necessary voltage and at least 1A current.

I hate delay and blocking code so I wrote a none blocking version. Tested on real hardware.

#include <MobaTools.h>

#define HOLDING true
#define ON_MOVE false 

const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;

unsigned long timer = 0;

MoToStepper stepper( 200, STEPDIR );

bool mode = false;
bool moving = false;

void setup()
{
   Serial.begin(115200);

   stepper.attach( stepPin, dirPin );
   stepper.setSpeedSteps(750);  // = 75 steps/second (steps in 10 seconds)
   stepper.setRampLen(10);
   stepper.setZero();
}

void loop()
{
   // how many steps, speed (steps/sec * 10, 2000 = 200 sps)
   moveStepper(200, 2000); 
   // time between move in millis() 
   holdup(5000);  
}

void moveStepper(long stepsToMove, long stepSpeed)
{
   if (mode == ON_MOVE) // time to go?  
   {
      if (moving == false) // stopped, set speed and steps. set to move
      {
         stepper.setSpeedSteps(stepSpeed);
         stepper.move(stepsToMove);
         moving = true;
      }
      else // in motion, check for ending position.  mode to hold on end
      {
         if (stepper.moving() == 0)
         {
            mode = HOLDING;
            moving = false;  
            timer = millis(); // record current time
         }
      }
   }
}

void holdup(unsigned long stepDelay) // dwell between moves
{
   if(mode == HOLDING)
   {
      if(millis() - timer >= stepDelay)
      {
         mode = ON_MOVE; // time to move again
      }
   }
}

I believe I have figured it out. Hardware problem on my end. The stepper motor shaft is not completely free to turn on one of my prototype designs leading to the clicking stuck motion when attempting to turn. This suggests I lost some torque from my previous circuit design but I have a different prototype with more clearance. I have changed the speed to 150 steps/s. I have set delay to 30 seconds and will see how the stepper behaves >30 minutes run time.

Does your stepper driver have an adjustment for coil current limit (many modern drivers do)? If so, have you, properly, adjusted the coil current? It is important that you do so.

Stepper motors are effected, in varying degrees, by resonance effects. At certain step frequencies, the motor will jitter and miss steps and even completely stall. I have motors that, running unloaded, will just stop and buzz at 200 steps/sec with no microstepping, but run fine at other frequencies. Using microstepping can mitigate the resonance effects. I usually use at least x4 microstepping.

Also the mechanical drive system can help to damp out resonance. A motor running unloaded is more prone to resonance. Stiff drives like lead screws and gear trains provide less damping and belt drives provide better damping.

I have set the current limit for my driver using the formula (Current = Vref*5). My stepper draws .5A therefore my Vref should be .1 V correct? I am using the STSPIN220 low voltage stepper motor driver. Here is the link.

Yes. I checked the Pololu page for your driver to confirm.

Vref(V) = Current Limit(A) / 5 = Vref = 0.5A / 5 = 0.1V

Ok, that is what I set it to when completing the circuit. As of right now, everything is working as intended. The board is running well over the desired time for my application. Thank you very much for your help, I can now continue with my research.

You are welcome. Good luck with your research and have fun.

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