# How can I achieve a square pulse train while sending pulses one pulse at a time to a stepper motor?

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?

• 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. – Chuck May 12 at 14:39
• pulses are sequential ... you cannot send more than one at a time – jsotola May 12 at 20:55

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

set_output(1)
sleep(0.5)
set_output(0)
sleep(0.5)


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
set_output(1)
while (clock() < next1) do_nothing;
set_output(0)
while (clock() < next2) do_nothing;
now = next2