Slow utime reading

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
pequnio3
Posts: 5
Joined: Sun Feb 14, 2021 7:10 pm

Slow utime reading

Post by pequnio3 » Sun Mar 14, 2021 7:17 pm

Hi I'm trying to use utime.time_ns() to time some very quick pin interactions (measuring interrupts on a pin when it goes low->high and then high->low). I need to be able to record intervals on the order of 80 microseconds.

When I'm testing the utime.time_ns() code in isolation, I realize I can't measure faster than 130 microseconds. I've done this both on an esp8266 and esp32 board and get similarly slow results.

Is this to be expected? Is there a better way to measure quick intervals?

Code: Select all

a=utime.time_ns()
b=utime.time_ns()
print(b-a)
# 134000
# ie 134 microseconds

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Slow utime reading

Post by Roberthh » Sun Mar 14, 2021 9:07 pm

There is machine.time_pulse_us() to time the duration of a pulse.

pequnio3
Posts: 5
Joined: Sun Feb 14, 2021 7:10 pm

Re: Slow utime reading

Post by pequnio3 » Mon Mar 15, 2021 3:39 am

Unfortunately I'm trying to use an mcp23017, and that functionality isn't available for individual pins. Any other ideas?

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Slow utime reading

Post by OlivierLenoir » Mon Mar 15, 2021 6:28 am

You can cache function time_ns(). In my test this improve the measure.

Code: Select all

from utime import time_ns

# Caching
t_ns = time_ns

# Measure
a = t_ns()
b = t_ns()
print(b - a)

Post Reply