Using the Pi Pico timers in a MicroPython script
In this article, we will explore the use of timers on the Pi Pico with MicroPython.
How a timer works on the Raspberry Pi Pico
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.
Using a MicroPython timer on the RPi Pi Pico
With MicroPython, all the complexity of the timer is hidden: you just have to enter the desired timer period. That is, the length of time the counter waits before an interrupt is triggered. It’s even easier than with Arduino code 😊.
In fact, with the current version of MicroPython for the Pi Pico, it is not possible to use the hardware timers individually . You can create an almost infinite number of “software” timers that are based on a single hardware timer.
Script squellete minimal
Here is a minimal skeleton Python script to use a Pico timer. It allows to trigger the interrupt interruption_handler()
at the end of each period of the timer.
from machine import Timer
def interruption_handler(timer):
...
if __name__ == "__main__":
soft_timer = Timer(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 :
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 orTimer.ONE_SHOT
to have a single triggeringThe desired timer period in milliseconds
The interrupt routine (the function) that will be invoked via the interrupt triggered by the timer, here
interruption_handler
Note
Since the timer is “software”, it is not necessary to specify the number of the hardware timer that you want to use, as might be the case on ESP32 boards.
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: Blink script only with a timer
With this script, the Pico’s built-in blue LED starts flashing every second, without using loops: you can write other functions without the flashing being disturbed.
from machine import Timer, Pin
pin_led = Pin(25, mode=Pin.OUT)
def interruption_handler(timer):
pin_led.value(not pin_led.value())
if __name__ == "__main__":
soft_timer = Timer(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 at each interruption of the timer
. In the main loop, when the variable is 10, a specific task that may take several seconds to execute is performed.
from machine import Timer, Pin
timer_count = 0 # global variable
def interruption_handler(pin):
global timer_count
timer_count += 1
if __name__ == "__main__":
timer_count_old = 0
soft_timer = Timer(mode=Timer.PERIODIC, period=100, callback=interruption_handler)
while True:
if timer_count > 10:
timer_count = 0
print("10x")
# heavy task here