In this tutorial, we learn to use the system timer peripheral of Raspberry Pi Pico. As we mentioned earlier, This development board comes with RP2040’s microcontrollers. This RP2040 MCU has a system timer peripheral that provides a global microsecond timebase and generates interrupts for it. 

Timers Introduction

Previously we learned how to blink an LED using delays infinitely but this time we will be performing this function using timers instead. Timers are more efficient than using the sleep function because they are not blocking functions as opposed to the latter. A blocking function stops the program from performing any task until the previous one is completed.

For example, the sleep() function, when we call sleep(2), our program stops at this line for 2 seconds, and thus it acts as a blocking function. This is not the case for Timers. To perform multiple tasks at once we prefer to use timers and should avoid using delays because they slow down the process.

Where to use Timers?

Timers available in Raspberry Pi Pico can be used to perform a certain task periodically after a certain amount of time. For example, we can use a timer interrupt to toggle the LED after every one second. Like interrupt handler routines, when a timer interrupts occurs, it stops the sequential execution of the program and performs a task that is attached to a respective time interrupt. After completing the interrupt service routine, the program returns to the next instruction from where it left off.

How to use Raspberry Pi Pico Timers MicroPython Library

Firstly, we have to import the machine module and from that module, we have to import the Timer class:

from machine import Timer

Create a timer class instance with an object name after that. The timer class object can be given any illustrative name, such as “timer.” The ID of the timer that we want to use is the input argument to the timer() method. For instance, the ID will be -1 if we want to use a virtual timer. If not, you can use the Raspberry Pi Pico’s lone hardware timer by leaving the ID field empty.

MicroPython Initialize Timers

The timer is then initialized in the following way. The Timer() function has three arguments namely:

  1. Period: The first argument is that of the period of the interrupt signal in milliseconds. This is the total time until the callback is called.
  2. Mode: The second argument is the mode of the signal. We can choose between two mode types: ‘Timer.PERIODIC’ or ‘Timer.ONE_SHOT.’ This means whether we want to configure our Timer as periodic or in one shot. In a periodic timer, the callback is called after every period continuously and in a one-shot timer, it is not continuous but only runs once after one period is up. In this lesson, we will be using the timer in periodic mode.
  3. Callback: The third argument is the callback which is executed whenever a timer is triggered. When we use the timer in periodic mode, the callback function will be called after every period which we will specify.

Initializing Timer Interrupt in Raspberry Pi Pico

We have set the period as 5000ms which means 5 seconds. The mode chosen is periodic and the callback function is the print command so this timer will print ‘Welcome to Microcontrollerslab’ after every 5 seconds.

timer = Timer(period=5000, mode=Timer.PERIODIC, callback=lambda t:print(“Welcome to Microcontrollerslab”))

Raspberry Pi Pico Generate Delay using Timers

Previously we had to use an infinite loop to delay the led or check for a button press. Through a timer, the same result will be achieved while also freeing room for other processes to run. Now we will see a demonstration of how to blink a Led using timer interrupt.

You will need the following components:

  1. Raspberry Pi Pico
  2. One 5 mm LED
  3. One 220 ohm resistor
  4. Connecting Wires
  5. Breadboard

We will connect all the components as we did before in our blinking-led tutorial. Below is a connection diagram for our board.

An LED is connected between GPIO14 through a resistor at the anode pin and ground at the cathode pin. As seen above we have chosen GPIO14 as output for Raspberry Pi Pico although any suitable output GPIO pin can be connected as well. It is your own choice.

Raspberry Pi Pico Timers Delay MicroPython Script

from machine import Pin, Timer        #importing pin, and timer class

led= Pin(14, Pin.OUT)              # GPIO14 as led output

led.value(0)              #LED is off

timer=Timer(-1)

timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:led.value(not led.value()))   #initializing the timer

Copy and Save the following code in your main.py to your Raspberry Pi Pico board.

 

How Code Works?

To use the Timer class we will import it from the machine module. As we want to use the pin class to configure our led as an output we will import it as well.

from machine import Pin, Timer        #importing pin, and timer class

Next, we initialize the ‘led’ as an output on GPIO14 and set its value to zero meaning it is OFF.

led= Pin(14, Pin.OUT)              #GPIO14 as led output

led.value(0)              #LED is off

Then we create a timer object and set its id to -1. This indicates that it is a virtual timer.

timer=Timer(-1) #create an instance of Timer method

The next line initializes the timer’s 3 parameters:

timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:led.value(not led.value()))

The first parameter is the period which the timer takes in executing the call back function. This is set to 1000ms.

period=1000

The second parameter is set to ‘Periodic’ which means the timer will execute the callback continuously once every period.

mode=Timer.PERIODIC

The third parameter callback is set to a function that will run after every period. In this case, we set it to a lambda function that toggles the led.

led.value(not led.value())   //function which is toggling the led

Author

Write A Comment