Moving Stepper Motor at a X speed for Y time.

1,158 views
Skip to first unread message

Juan Manjarres

unread,
Oct 12, 2015, 10:23:21 AM10/12/15
to accelstepper
Hi guys! I'm hoping you guys can help me out with a programming issue I cant get past. I am trying to move this stepper motor at a certain speed for a given time I give it. (Ive tried the delay function and somehow it just ignores it. Can you guys help me out with the coding necessary for the timing issue I am having?

Thanks!

#include "DualMC33926MotorShield.h"
#include <AccelStepper.h>

int motorDirPin = 2;
int motorStepPin = 3;

unsigned long time_since_last_reset = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(2, OUTPUT);    
  pinMode(3, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  spin(150000,100);
  delay(5000);
  exit(0);
}

void spin(unsigned long steps, int sspeed)
{
  while(steps>0)
  {
    digitalWrite(3, HIGH);
    delayMicroseconds(40);
    digitalWrite(3, LOW);
    delayMicroseconds(40);
    //delayMicroseconds(sspeed);
    steps--;
  }
}

gregor

unread,
Oct 13, 2015, 4:48:15 AM10/13/15
to accelstepper
To clarify, do you want to move the stepper for a specific time or for a specific number of steps?

if you were using AccelStepper your spin() function would look like this:
void spin(unsigned long steps, int sspeed)
{

  stepper
.setSpeed(sspeed);   //sspeed must be given in steps / second

  stepper.move(
steps);

 
while(stepper.distanceToGo() != 0);
    stepper
.runSpeedToPosition();
 

}

Juan Manjarres

unread,
Oct 13, 2015, 10:51:40 AM10/13/15
to accelstepper
Gregor,

I needed the stepper for a specific time that I set. I was able to use another code. Just for kicks. Would would be the max speed that can be set with this code?

gregor

unread,
Oct 13, 2015, 10:59:18 AM10/13/15
to accelstepper
That depends mostly on the hardware you use. I do not have any exact figures, and I'm not sure anyone has ever conducted a series of tests with different hardware.

Juan Manjarres

unread,
Oct 13, 2015, 11:42:23 AM10/13/15
to accelstepper
I see. Makes sense. From what I know my stepper is a bipolar 200 step 12vdc 20w. Thats as much as I know.

Brian Schmalz

unread,
Oct 13, 2015, 11:53:22 AM10/13/15
to accels...@googlegroups.com
Juan,

There are a couple of maximum speeds to consider:

1) The maximum speed of step pulses that your microcontroller can generate. With AccelStepper on a normal (AVR) Arduino, this is about 4000 steps/second.
2) The maximum speed that your motor driver can properly generate the step waveform. For drivers like the Easy Driver and Big Easy Driver, this is very high - like probably 100K microsteps/s.
3) The maximum speed that your motor can spin with no load (i.e, nothing connected to the shaft). This varies widely, and depends on motor type, coil resistance, impedance, driving voltage, and type of driver circuit. Typical values for small NEMA 12 or 17 motors are usually between 5 and 20 Revolutions/second.
4) The maximum speed that your motor can spin with the maximum load your system will be applying to the shaft. This of course is completely dependent on your mechanical system.

The maximum speed you can get out of your system will be limited to the slowest of these four things. For most people, #4 is the lowest, and thus most limiting factor. #1 is the second most common limit.

Switching to full steps rather than microsteps can allow you to run your motor faster (this is done on Easy Driver and Big Easy Driver by tying MS1, MS2, and MS3 low). Using a higher input voltage to the driver can increase torque, and thus high end motor speed before it poops out. Using smooth acceleration rather than jumping between speeds can also help reach higher speeds (this is where Accelstepper really shines).

If #1 ends up being a problem, you can move to faster Arduino compatible boards. If #3 is the limit, then you can go to bigger/better motors. If #4 is the limit, then working on decreasing the mechanical load is the solution to running faster.

Hope this helps-

*Brian

--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Brian Schmalz

unread,
Oct 13, 2015, 12:23:25 PM10/13/15
to accels...@googlegroups.com
Right, but what about with acceleration? I think that makes a difference.

*Brian

On Tue, Oct 13, 2015 at 11:22 AM, gregor <chris...@gmail.com> wrote:
you made me curious, so I hokked up my Arduino Mega to my 2560 and tried to max the steps/second with the code below (modified ConstantSpeed example):
#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::DRIVER, 8, 9); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{  
   stepper.setMaxSpeed(999999);
   stepper.setSpeed(999999);
}

void loop()
{  
   stepper.runSpeed();
}

1 step takes about 90µs ~> 11,111 steps / second.

Brian Schmalz

unread,
Oct 13, 2015, 12:24:48 PM10/13/15
to accels...@googlegroups.com
I also wonder if it matters if you use a 2-wire interface (like to a STEP/DIR drive). That's how I've done all of my testing because that's what I've always had around. I'll bet it does, since with a 2-wire design you have to set the STEP pin high, wait a bit, then set it low. With a four-wire interface like your code used, all you have to do is set the state of the four lines.

*Brian

gregor

unread,
Oct 13, 2015, 12:32:29 PM10/13/15
to accelstepper
repost because of typos, sorry.

you made me curious, so I hooked up my Arduino Mega 2560 to my oscilloscope and tried to max the steps/second with the code below (modified ConstantSpeed example):
#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::DRIVER, 8, 9); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{  
   stepper.setMaxSpeed(999999);
   stepper.setSpeed(999999);
}

void loop()
{  
   stepper.runSpeed();
}


This test was just to show the theoretical limit on my Arduino. 

On Tuesday, October 13, 2015 at 6:23:25 PM UTC+2, EmbeddedMan wrote:
Right, but what about with acceleration? I think that makes a difference.

*Brian

Message has been deleted

gregor

unread,
Oct 13, 2015, 12:44:02 PM10/13/15
to accelstepper
The same test with acceleration:
#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::DRIVER, 8, 9); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{  
   stepper.setMaxSpeed(999999);
   stepper.setAcceleration(999999);

   stepper.moveTo(2147483647);
}

void loop()
{  
   stepper.run();
}

1 step takes about 260µs ~> 3840 steps / second.

On Tuesday, October 13, 2015 at 6:23:25 PM UTC+2, EmbeddedMan wrote:

Juan Manjarres

unread,
Oct 13, 2015, 12:47:00 PM10/13/15
to accelstepper
The only reason I'm not using that code is because even at max speed my stepper doesnt move fast

Brian Schmalz

unread,
Oct 13, 2015, 12:48:28 PM10/13/15
to accels...@googlegroups.com
Yeah, right on! This is great data Gregor - I've always used the "maximum 4K steps/s for normal Arduinos" as a sort of constant, but your data shows that it also matters if you use acceleration or not, and what type of interface you choose. 

*Brian

--

Brian Schmalz

unread,
Oct 13, 2015, 12:49:36 PM10/13/15
to accels...@googlegroups.com
Juan,

Well, fast is somewhat relative. How fast does your motor go (in RPS) and how fast do you need it to go?

*Brian

--

Juan Manjarres

unread,
Oct 13, 2015, 12:53:26 PM10/13/15
to accelstepper
Well I'm not set a certain speed. However, I did notice that with that code it just didnt go as fast as I know I was able to get it with just switching low and high on the step pin.

Does that code you guys are trying to run only use a step and direction pin?

I am trying to run that code and see what happens but it gives me an error:

Arduino: 1.6.5 (Mac OS X), Board: "Arduino/Genuino Uno"

Build options changed, rebuilding all
sketch_oct13b:3: error: 'DRIVER' is not a member of 'AccelStepper'
'DRIVER' is not a member of 'AccelStepper'

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

Brian Schmalz

unread,
Oct 13, 2015, 12:59:26 PM10/13/15
to accels...@googlegroups.com
Juan,

You have to be careful about comparing versions of code - some of the code Gregor posted uses a 2-wire interface (like yours) and some uses a 4-wire interface, which is different. If you use the 4-wire interface with a STEP/DIR drive (2-wire) you will probably see the motor run, but at a slower speed than the Accelstepper library is actually trying to run the motor at.

Make sure that you convert any examples over into using the 2-wire interface. See the AccelStepper examples here : http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html

*Brian

Juan Manjarres

unread,
Oct 13, 2015, 1:01:18 PM10/13/15
to accelstepper
Well, Ive added the comparisons.

Ive added the first video (slow) which is using the code you guys set for max speed with the accell library. The second video is full speed without the accel library.
Video-1.MOV
Video.MOV

Juan Manjarres

unread,
Oct 13, 2015, 1:02:59 PM10/13/15
to accelstepper
Brian:

I have it set up in the way that you have it in your first example. I can'f afford to use the four other pins that are required from the arduino.

Juan Manjarres

unread,
Oct 13, 2015, 1:05:56 PM10/13/15
to accelstepper
#include <AccelStepper.h>

AccelStepper stepper(1, 3, 2); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5


void setup()
{  
   stepper
.setMaxSpeed(999999);
   stepper
.setAcceleration(999999);  

   stepper
.moveTo(2147483647);
}

void loop()
{  
   stepper
.run();
}
That is the code is used to run that video I uploaded with the accel library.

Brian Schmalz

unread,
Oct 13, 2015, 1:09:39 PM10/13/15
to accels...@googlegroups.com
Right - that makes some sense. If you are using an Uno, then your max step rate (with Accelstepper) will be about 3600 steps/second (as Gregor demonstrated with his test), and if you're using a Big Easy Driver with 1/16th microstep mode, then there are 3200 microsteps/revolution, so you should be getting about 1 rev/second. In  your video, it looks to be about 1/2 rev/second, so at least we're in the right ballpark explaining the speed difference.

Switching to Gregor's example without the acceleration would help up your speed. Or switching to not using AccelStepper at all. Or switching to full-step mode. Any of those would help get your top speed up.

*Brian

Juan Manjarres

unread,
Oct 28, 2015, 11:57:11 AM10/28/15
to accelstepper
Brian:

How can I switch to full step mode?

Brian Schmalz

unread,
Oct 28, 2015, 12:03:21 PM10/28/15
to accels...@googlegroups.com
Juan,

Which driver are you using again?

*Brian

Juan Manjarres

unread,
Oct 28, 2015, 12:25:47 PM10/28/15
to accelstepper
I am using your big easy driver.

Brian Schmalz

unread,
Oct 28, 2015, 12:29:04 PM10/28/15
to accels...@googlegroups.com
OK, in that case, simply tie the MS1, MS2, and MS3 lines to ground, and the BED will operate in full step mode.

*Brian
Reply all
Reply to author
Forward
0 new messages