1

hardware: raspberry pi pico

language: adafruit-circuitpython

TL;DR: 0.001 sec is the maximum precision that time.sleep() can perform. how can we get over it, using circuitpython? The 'utime' library works only on micropython...

The long story: look at the following code:

import time

x = time.monotonic_ns()

for i in range(10):
    time.sleep(1.0005)
    print (time.monotonic_ns()-x)

You would expect the output to be something like

  • 1000500000
  • 2001000000
  • 3001500000
  • 4002000000
  • ...

But the output is:

  • 1000976563 (almost 1001000000 )
  • 2000976563 (almost 2001000000 )
  • 3000976563 (almost 3001000000 )
  • 4000976563 (almost 4001000000 )
  • 5000976563 (almost 5001000000 )

I believe somewhere along the way there is an issue of floats representation using not enough bits, because:

1,000,000,000 / 1024 = 976562.5

But I'm really new to all of this stuff so I'll be glad to get help from you all.

Ofir Gaash
  • 11
  • 2
  • the difference between your expected output and your actual output is roughly the factor for seconds to nanoseconds. I would expect a number in the billions of nanoseconds for a delay of seconds. So maybe you want to update your question and get rid of that factor to point out the real difference between expected and actual seen? – cyberbrain Jul 26 '22 at 16:42
  • Not sure I understand your suggestion. Do you suggest that I'll edit the post such that the output would be 1, 2, 3 ect.? – Ofir Gaash Jul 26 '22 at 16:58
  • _I_ would have expected `time.monotonic_ns()-x` to give back values like `1000500000`, `2001000000` and so on... 1 s = 1000 ms, 1 ms = 1000 μs, 1 μs = 1000 ns, so 1s = 1000000000 ns. – cyberbrain Jul 26 '22 at 20:21

1 Answers1

0

According to https://docs.micropython.org/en/latest/library/time.html Micropython has multiple sleep functions (sleep, sleep_ms, and sleep_us) providing more accurate timings. See if they exist in your library.

It also says that Floating point times may be accepted only on some boards.

https://docs.circuitpython.org/en/latest/shared-bindings/time/ This site says circuit python has a different set of time functions

time.monotonic_ns(), supervisor.ticks_ms(), and time.monotonic_ns() availability of these function also depends on the board.