1

I would like to make a program which will run on a raspberry pi, that will switch on a circuit, and switch another circuit on a few nano seconds afterwards. Is it possible to create a delay that is so small?

colidyre
  • 4,170
  • 12
  • 37
  • 53
  • 1
    No, not directly in python. You may struggle to do that in C++ on an rpi. – quamrana Mar 20 '20 at 12:12
  • If you need that kind of precision, I wonder why you're using python in the first place – Minn Mar 20 '20 at 12:14
  • Measure how fast Python is on that Raspberry when running normal code. Is it accurate in a nanoseconds range? – Jongware Mar 20 '20 at 12:15
  • See this: https://raspberrypi.stackexchange.com/questions/3898/nanosleep-wont-sleep-short-time?newreg=09230bb9224b48c4b3ab8d3f4c5765f0. – CypherX Mar 20 '20 at 12:18
  • OK, but is it possible to get a delay smaller than a millisecond? (0.001 seconds)? – Stevovoness Mar 20 '20 at 13:18
  • Just a side info: https://stackoverflow.com/q/1133857/2648551 and https://www.python.org/dev/peps/pep-0564/ – colidyre Mar 20 '20 at 17:06
  • You'll have to test if your Raspberry supports [getting nanosecond timings](https://docs.python.org/3/library/time.html#time.perf_counter). – Jongware Mar 20 '20 at 21:34

2 Answers2

-1

You can try something like this -

start = time.time()
while time.time() - start < 0.15:
     pass

you can create a delay and use the same.

dper
  • 884
  • 1
  • 8
  • 31
-1
import time
delay = 0.001 #seconds
time.sleep(delay)
bonifacio_kid
  • 633
  • 5
  • 8