0

I'm new to Micropython and microcontrollers in general. I'm trying to create a script to run on a Raspberry Pi Pico that takes two time variables time1 = utime.time_ns() and time2 = utime.time_ns() and then subtracts time2 from time1 to give me the difference between the two times with nanosecond precision. When attempting to do this it prints out the value in nanoseconds rounded up to the second... for example, If there is 5 seconds between the two times the value returned is 5000000000... Is there a way that I can get a more accurate time? Am I going about this the wrong way? Thank you!!!

larsks
  • 277,717
  • 41
  • 399
  • 399
Cam32
  • 9
  • 1
  • 2
  • 1
    I'm not sure I understand the question...5 seconds *is* 5000000000 nanoseconds. What results were you expecting? – larsks Feb 07 '21 at 04:35
  • You're right, but the value is always rounded up to the second just represented in nanoseconds. I'm wanting to get an accurate time, so a better example would be if there were 5.3456 seconds between the two times the result would still equal 5000000000... It is rounding instead of showing me the exact time. Does that make more sense? Thanks! – Cam32 Feb 07 '21 at 06:29
  • use the timer directly not some library. – old_timer Feb 07 '21 at 06:43
  • 1
    if interested in accuracy and performance then dont use micropython. the bigger question is what are you trying to do with this measurement and is there a feature in the chip that does this for you? – old_timer Feb 07 '21 at 06:44
  • 1
    nanosecond precision requires at least a GHz clock, the pico maxes out at 133MHz. – old_timer Feb 07 '21 at 07:02
  • at least the arm core, the peripheral clock 125Mhz? 8ns – old_timer Feb 07 '21 at 07:11
  • I'm aiming to measure the time it takes to quick draw and shoot a balloon. The time it takes it's much less than one second... The time is then output to a display. I may not need nanosecond precision, but as much precision as possible with the given hardware. So I should use Timer instead of Utime? – Cam32 Feb 07 '21 at 07:25

2 Answers2

4

The MicroPython utime page explains how there is a difference between absolute time from time_ns() and relative time from ticks_us(). They are best used for different purposes, and probably use different resources. From the examples there, you could try something like

start = time.ticks_us()
...
end = time.ticks_us()
usecs = time.ticks_diff(end, start)

As the page explains, this cannot be used to measure long times, such as more than 1 or 2 seconds, depending on implementation, and the resolution will not be nanoseconds, but at best microseconds.

meuh
  • 11,500
  • 2
  • 29
  • 45
  • Awesome, thank you so much. That pointed me in the right direction and I was able to accomplish what I was trying to do. – Cam32 Feb 08 '21 at 04:05
  • Does the uf2 loaded onto the Pico make a difference to the version of time that get's used? Sorry if that's a dumb question – Quintin Balsdon Feb 19 '21 at 08:47
1

The processor crystal is not going to be accurate enough to get nanosecond precision. You would have to replace it with a TCXO/OCXO crystal to get microsecond precision. Another problem is crystal drift with temperatures. The OCXO is a heated crystal. A TCXO is a temperature compensated crystal. As long as the temperature change a small a TCXO will likely get you in the microsecond ballpark. Then comes the firmware issues. Python is too slow for precise timing. you would have to pick a compiled language to minimize the jitter issues. I hope this helped.

Jango2
  • 11
  • 1