Timing Functions
We often need to calculate how much time has elapsed since an event occurred. To to this we can use the ticks
functions in the MicroPython utime library.
Microsecond Timer
There are one million microseconds in a single second. The utime
library allows us to count the number of microseconds that have elapsed since the processor was powered up.
The following example times the sleep function and measures the difference in the number of clock ticks in microseconds between the two events.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
results:
1 2 3 4 5 6 7 |
|
You will note that the difference between the start and end time should be one million microseconds. However, the run-time libraries on the pico have some variability, so you will see the actual time vary by a few microseconds. Most of the time you can use milliseconds to compare time intervals.
Millisecond Timer
1 2 3 4 5 6 7 8 9 10 11 |
|
results:
1 2 3 4 5 6 7 |
|
These results are almost always 1000 with an occasional 1001 value.