I am trying to control Stepper motors which has a pulse input driver.

I have a speed of 1000pps, and I want to move 1 pulse at a time. So that means 1 pulse will take 1ms.

After I send a command to move 1pulse, I sleep for 1ms so the pulse can be completed before I can send another pulse

Below is my pseudocode

while(1)// loop forever
      speed = 1000pps
      pulses = 1;
      move(pulses) //move the motor 1 pulse
      sleep(1 millisecond) //sleep for 1ms

How ever when when I specify a distance of say 10000 pulses and instruct the motor to move at once, I get a perfect square pulse train with a period of 1.

Expectation I am expecting to see a square pulse train on the oscilloscope, where the on time will be equal to the off time. But instead the on time is 0.5ms and the off time is becoming 1ms. Is it possible to have the off time and off time both at 0.5ms while moving one pulse at a time?

  • $\begingroup$ Your pseudocode isn't helpful because you're abstracting away where the issue would lie. In your pseudocode, if you wanted to move say 10000 pulses, then... you move(10000) and sleep 1 millisecond? Please edit your question to include the actual code. Please also report what happens when you move 1000 pulses, or just 1 pulse. $\endgroup$
    – Chuck
    May 12, 2021 at 14:39
  • $\begingroup$ pulses are sequential ... you cannot send more than one at a time $\endgroup$
    – jsotola
    May 12, 2021 at 20:55

1 Answer 1


Split the sleep() into two halves if you want your software to control both halves of the pulse. Your loop body would then look like


Your 'scope might show a period slightly greater than 1.0 msec -- besides the sleeps, those other statements take time too. If that's a problem, you could manually tune the sleeps, which is both tedious and fragile. Or change the code to consult a clock:

now = clock()
while 1:
    next1 = now + 0.5
    next2 = now + 1.0
    while (clock() < next1) do_nothing;
    while (clock() < next2) do_nothing;
    now = next2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.