Sleep Comand Questin

Hello. :grinning: :grinning:
What is the limits of the sleep command? I need a 0.000666666665 delay ) but it does not seem to work. Sometimes it is perfect but most times it all over the page. It not constant.

Possibly, what you’re experiencing, is that it’s taking longer to execute sleep(0.000666666665) than the delay of the time period.

From what I understand, execution times [of Python scripts] will vary and are dependent on what else your system is having to do, but on the whole, the sleep function is quite accurate.

1 Like

I’m no expert, but I suspect that sleep() will pause for at least the time you request. If there’s something slowing the system down, it could be quite a bit longer. You’re not likely to notice with sleep times of O(one second) but you’re asking for less than a microsecond – no non real-time system is going to be consistent with that level of time precision.

If you really, really need such a fine-grained pause, you might want to read up on the topic of real-time operating systems. “Retail” OSes like Windows, MacOS and many flavors of Linux generally don’t have real-time guarantees as part of their feature set.

I don’t see any mention of the timeit module so far, so I’ll point out that it can be useful in this context. Something like this command

python -m timeit -s 'from time import sleep' 'sleep(0)'

should give you a good idea of the overhead of calling time.sleep. On my Mac it’s about 340ns, so about 0.00034s. Add your desired sleep time and you’ll see you’ll likely be way over budget on your naps. That’s without any operating system delays.

1 Like

You’re off by a factor of 1000. 0.00034s is 340µs. 340ns is 0.00000034s.

You’re off by a factor of 1000. 0.00034s is 340µs. 340ns is 0.00000034s.

Thanks. That’s what I get for asking Google for “340ns in seconds” then not starting harder at the answer. Damn AI…

No, that’s wrong too. I did ask Python for 340*1e-6. Stupid, stupid, stupid.

When you ask a computer to sleep for a certain amount of time, you will often not get control back at precisely that time, and actually there are occasionally reasons that you’ll get control sooner than the time requested. What are you attempting to do here? Are you attempting to do something every 2/3 ms? If so, I would recommend having some sort of loop with a high resolution timer in it; effectively, call sleep, and then see how many times you need to tick your counter.

But it’s worth keeping in mind that this sort of precision is HARD. Have you ever played a video game at 1500 FPS? I don’t think I’ve ever seen anything that runs at that kind of frame rate with any consistency.

That delay is correct. I take the required Hz pulse width and then take that value and divided it by 2.
For example, 720 Hz has a pulse width of 0.0013333 seconds. Then I divide that by 2 resulting pulse width is 0.0006667 seconds That result is the value of the sleep() command.

Looking at the following code this sleep value will produce a 50% duty cycle.

	while True:
		GPIO.output(Step_Pin), not(GPIO.input(Steop_Pin)))
		sleep(Delay)  

Hope that clears up the confusion about sleep values

Ah, yeah, that’s not going to be nearly reliable enough. The time taken to run through the Python code for twiddling the GPIO pin is going to affect your output frequency.

Are you able to utilize a dedicated timing chip?

Yes, I am thinking about using an Adafruit ItsyBitsy 32u4 to produce the required timing. pulses using the delayMicro() function. According to the info about it, the shortest delay is 3 microseconds. ( 333.333 KHz ).

I am hoping once the code is correct I can find someone to burn the code to the unit so I do not have to reload the code every time I boot it up.