pythonesp32micropython

Measuring times less than one second in micropython with esp32


I have project and I need to measure speed of a wheel and i need to know how much time has passed from last press of a button. currently I'm using time.time() which measures seconds. this is my code:

speeds=[]
break_btn = Pin(33,Pin.IN,Pin.PULL_DOWN)
for i in range(1000):
    time.sleep(0.07)
    if break_btn.value():
        time_passed=time.time() - start_time   # measuring time passed from last press of button
        start_time = time.time() # resetting time of last button press
        print(time_passed) # only for debug
        if time_passed != 0: # preventing devide by zero error
            speeds.append(0.335/time_passed) # using time to measure speed
print(speeds)

Solution

  • I found the answer. I should use time.ticks_ms() instead of time.time() and also time.ticks_diff(time.ticks_ms(), start_time) / 1000 instead of time.time() - start_time. The second part is optional and also I needed to divide it by 1000 to turn milliseconds into seconds.