Skip to content

uPesy now ships to the entire European Union

Livraison à partir de seulement 2.50€ !

Contents Menu Expand Light mode Dark mode Auto light/dark mode

Using ESP32 timers with MicroPython

(Updated at 01/23/2023)

In this article, we will explore the use of timers on the ESP32 with MicroPython. We’ll discover the steps necessary to set up a timer on the ESP32, as well as the important parameters for optimal operation. Let’s go ! 😊

The operation of a timer on the ESP32

The theoretical functioning of the timer is not discussed in this article in order to keep it as clear as possible. If you are a beginner and you don’t know the inner workings of a timer, I strongly invite you to read the theoretical article on the functioning of a timer . This will allow you to better understand how to select the right parameter values to use it in your Micro Python scripts.

Configuring and using an ESP32 timer in MicroPython

With MicroPython, all the complexity of the timer is hidden: you just have to enter the desired timer period. That is to say, the length of time the counter waits before an interrupt is triggered.

Note

It’s even easier than with Arduino code, because you don’t have to worry about the values of the prescaler and the``autoreload`` values, to have the right period. MicroPython takes care of that.

Script squellete minimal

Here is a minimal skeleton Python script to use an ESP32 timer. It allows to trigger the interrupt interruption_handler() at the end of each timer period.

from machine import Timer

timer_0 = Timer(0) # Between 0-3 for ESP32

def interruption_handler(timer):
    ...

if __name__ == "__main__":
    timer_0.init(mode=Timer.PERIODIC, period=1000, callback=interruption_handler)

In our example, the period of the timer is set to 1000ms or 1 second. To use a timer, you must :

  • Choose the hardware timer to be used with Timer(id) . In general we create the object at the beginning of the script.

  • Set up the timer at its initialization with the function timer_0.init(mode=, period=, callback=) which contains the following arguments:

    • The mode Timer.PERIODIC mode so that the interrupt is triggered each time the timer period is reached or Timer.ONE_SHOT to have a single triggering

    • The desired timer period in milliseconds

    • The interrupt routine (the function) that will be invoked via the interrupt triggered by the timer, here interruption_handler

Warning

The minimum timer period is one millisecond in MicroPython, whereas with Arduino code, it can easily be reduced to one microsecond. Timer performances are limited so that MicroPython can keep up with the pace..

Example : Increment a variable

Here is a Python script that increments a variable each time the timer triggers an interrupt. In the main loop, when the variable is 10, a specific task is performed.

from machine import Timer, Pin

timer_0 = Timer(0) # Between 0-3 for ESP32
timer_count = 0 # global variable

def interruption_handler(pin):
    global timer_count
    timer_count += 1


if __name__ == "__main__":
    timer_count_old = 0
    timer_0.init(mode=Timer.PERIODIC, period=100, callback=interruption_handler)

    while True:
        if timer_count > 10:
            timer_count = 0
            print("10x")
            # heavy task here