Generate Delay with Raspberry Pi Pico Timers using MicroPython

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. In this tutorial, we will focus learn to configure Raspberry Pi Pico Timer and generate delays with them using MicroPython.

Generate Delay with Raspberry Pi Pico Timers using MicroPython

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.

Prerequisites

Before we start this lesson, make sure you are familiar with and have the latest version of Python3 installed in your system and set up MicroPython in your Raspberry Pi Pico. Additionally, you should have a running Integrated Development Environment(IDE) to do the programming. We will be using the same Thonny IDE as we have done previously when we learned how to blink and chase LEDs in MicroPython here:

If you are using uPyCraft IDE, you can check this getting started guide:

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

After that create an instance of a timer class with an object name. We can give any descriptive name to the timer class object such as “timer”. The input argument to the timer() method is the ID of the timer which we want to use. For example, if we want to use virtual timer, the ID will be -1. Otherwise, you can leave the ID empty to use the single hardware timer of Raspberry Pi Pico.

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 will 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.

Raspberry Pi Pico generate delay with timers

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.

Raspberry Pi Pico with LED GPIO14 connection diagram
Raspberry Pi Pico with LED connection diagram

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 with LED
Raspberry Pi Pico with LED

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.

You should observe the LED blinking once per second continuously. Using a timer frees processing time because you do not have to run the endless loop.

Raspberry Pi Pico generate delay with timers demo

Demo Video

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 time 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

In this tutorial, we saw how timers work in Raspberry Pi Pico using a MicroPython script by simply blinking an LED light.

You may also like to read:

Leave a Comment