Using the Pi Pico timers in a MicroPython script
(Updated at 02/02/2023)
This article will look at implementing timers on the Raspberry Pi Pico using MicroPython.
How a timer works on the Raspberry Pi Pico
This article doesn’t go into the details of how a timer works, so if you are a beginner and want to know more about it, I suggest you read the article on the theoretical workings of the timer . This will help you choose the suitable parameters for your Micro Python programs.
Using a MicroPython timer on the RPi Pi Pico
MicroPython makes it easy to use a timer; no complex coding is required. You must enter the time you want the counter to wait before triggering an interrupt. It’s even easier than doing it with Arduino code! 😊
The current version of MicroPython for the Pi Pico does not allow the hardware timers to be used separately . Instead, it is possible to create an almost unlimited number of “software” timers, all of which rely on a single hardware timer.
Minimal script skeleton
This simple Python script shows how to use the Pico timer. It can call interruption_handler()
at the end of each period set on 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 it up at its initialization with the function timer_0.init(mode=, period=, callback=)
which contains the following arguments:
The
Timer.PERIODIC
mode triggers an interrupt every time the timer period is reached, while the t``imer.ONE_SHOT`` mode starts only once.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
Note
As the timer is “software,” it is not necessary to specify the number of the hardware timer to be used, as is the case with ESP32 boards.
Warning
Using MicroPython, the shortest timer period is one millisecond, while this can be reduced to one microsecond when writing code for Arduino. However, the speed of timer functions is limited to keep MicroPython responsive.
Example: Blink script with timer only
This script makes Pico’s built-in blue LED flash every second without using loops. This allows other functions to be written without the LED flashing.
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)
To reverse the state of the LED, I use a simple trick. We can get the current state of the LED with pin_led.value()
, which will return either 0 or 1. Then we invert the value using the not
operator. Finally, we update the new value with the same function, pin_led.value()
.
Example: Incrementing a variable
This Python script has a variable that increases by one unit each time the timer
is interrupted. In the main loop, when this variable is 10, a task is performed that may take several seconds to complete.
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