Go Down

Topic: Stepper PWM and unexpected results (Read 818 times) previous topic - next topic


I have a stepper motor in my project that I want to drive at a number of different user defined speeds across a predefined distance. The speeds I want to achieve range from a 15 second trip, to 24 hours across this distance. I am having success with the trips from 15 seconds all the way up to 25 minutes, however when I get to 30 minutes I start to get unexpected results.

The user defined speeds are as follows:

Seconds Minutes Hours
15 1 1
20 2 2
25 3 3
30 4 4
35 5 5
40 10 6
45 15 9
50 20 12
55 25 18
30 24

To implement the stepper speed, I used a microsecond delay in the 'for' loop below to generate the pulse and define the stepper speed. I calibrated the distance traveled based on the 15 second trip, and then did a linear extrapolation to find the speedValue for the rest of the trips. As stated above, it works great up until the 30 minute trip.

Code: [Select]

for(long i = 0; i < maxStepsForTrip; i++){
     digitalWrite(pinStep, HIGH);
     digitalWrite(pinStep, LOW);

I'm having trouble trying to understand why the motor is acting up when the microsecond delay gets too large. Shouldn't the longer delay simply cause the pulse to be larger and the step to be slower? I haven't been able to find any data anywhere that says a stepper should have a minimum speed. I looked at the values of speedValue at 25 minutes (which is 15500 microseconds) and at 30 minutes (which was 18600) and in my mind these shouldn't produce terribly different results. I mean it's not like we're changing orders of magnitude or anything.

To try and remedy this situation, I've tried a 'for' loop with a millisecond delay and the stepper doesn't like that at all.

Can someone please help with this?

Thanks in advance!  :smiley-eek:


I think I found the answer to the problem.



Pauses the program for the amount of time (in microseconds) specified as parameter. There are a thousand microseconds in a millisecond, and a million microseconds in a second.
Currently, the largest value that will produce an accurate delay is 16383. This could change in future Arduino releases. For delays longer than a few thousand microseconds, you should use delay() instead.

But I'm still wondering why a regular delay() won't work for stepper PWM?


It is not PWM you are using.

There is no reason why the normal delay will not work but your top speed is limited.


So if it's not PWM what is it? I thought PWM was changing the pulse frequency to the coils...ie adjusting the delay between pulses. And I'm not worried about max speed...it's minimum speed or very low speeds I need.

Do you have any suggestions as to why I'm experiencing these problems?


PWM stands for Pulse Width Modulation so the ratio of the on to the off time controls the average value of the waveform. This is nothing to do with what you are doing.
From the very littl you have posted all you are doing is producing a variable period square wave. This will be the same no matter how you generate the delay.
So how is your stepping motor being driven? What driver are you using? How is it wired up? What sort of stepping motor do you have? What is the full code?

Go Up