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.
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:
Timer.PERIODICmode so that the interrupt is triggered each time the timer period is reached or
Timer.ONE_SHOTto 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
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: Blink script only with a timer
With this script, the built-in blue LED of the ESP32 starts blinking every second, without using loops: you can write other functions without the blinking being disturbed.
from machine import Timer, Pin pin_led = Pin(2, mode=Pin.OUT) timer_0 = Timer(0) # Between 0-3 for ESP32 def interruption_handler(timer): pin_led.value(not pin_led.value()) if __name__ == "__main__": timer_0.init(mode=Timer.PERIODIC, period=1000, callback=interruption_handler)
Here, I use a little trick to reverse the state of the LED. We get the current state of the LED with
pin_led.value() which is 0 or 1, and invert the value with the operator
not . We use the same function
pin_led.value() to update the new value.
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