{"id":169636,"date":"2025-05-29T12:51:30","date_gmt":"2025-05-29T12:51:30","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=169636"},"modified":"2025-05-29T13:21:57","modified_gmt":"2025-05-29T13:21:57","slug":"raspberry-pi-pico-interrupts-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-interrupts-micropython\/","title":{"rendered":"Raspberry Pi Pico with Interrupts: External and Timer Interrupts (MicroPython)"},"content":{"rendered":"\n<p>In this guide, we\u2019ll show you how to handle interrupts with the Raspberry Pi Pico using MicroPython and build some project examples with a pushbutton and a PIR motion sensor. We&#8217;ll cover external interrupts and time interrupts.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Raspberry-Pi-Pico-Interrupts.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with Interrupts: External and Timer Interrupts (MicroPython)\" class=\"wp-image-169676\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Raspberry-Pi-Pico-Interrupts.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Raspberry-Pi-Pico-Interrupts.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Raspberry-Pi-Pico-Interrupts.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Raspberry-Pi-Pico-Interrupts.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Raspberry-Pi-Pico-Interrupts.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\"><strong>New to the Raspberry Pi Pico? <\/strong><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Check out our eBook: Learn Raspberry Pi Pico\/Pico W with MicroPython<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Table of Contents<\/h2>\n\n\n\n<p>In this guide, we&#8217;ll cover the following subjects:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#interrupts-introduction\" title=\"\">Introducing Interrupts<\/a><\/li>\n\n\n\n<li><a href=\"#external-interrupts-micropython\" title=\"\">External Interrupts in MicroPython<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#detect-pushbutton-press\" title=\"\">Example 1: Detecting a Pushbutton Press<\/a><\/li>\n\n\n\n<li><a href=\"#count-number-of-presses\" title=\"\">Example 2: Counting the Number of Button Presses<\/a><\/li>\n\n\n\n<li><a href=\"#toggle-led-button-press\" title=\"\">Example 3: Toggling an LED on each Button Press<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><a href=\"#timer-interrupts\" title=\"\">Timer Interrupts in MicroPython<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#blink-led-with-timer\" title=\"\">Example 1: Blinking an LED with a Timer<\/a><\/li>\n\n\n\n<li><a href=\"#blink-multiple-leds\" title=\"\">Example 2: Blinking Multiple LEDs at Different Frequencies<\/a><\/li>\n\n\n\n<li><a href=\"#debouncing-pushbutton-timer\" title=\"\">Example 3: Debouncing a Pushbutton with a Timer<\/a><\/li>\n\n\n\n<li><a href=\"#pir-sensor-interrupts-and-timers\" title=\"\">Example 4: Interrupts and Timers with a PIR Motion Sensor<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites \u2013 MicroPython Firmware<\/h2>\n\n\n\n<p>To follow this tutorial, you need MicroPython firmware installed on your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.<\/p>\n\n\n\n<p>The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"interrupts-introduction\">Introducing Interrupts<\/h2>\n\n\n\n<p>Interrupts are useful for making things happen automatically in microcontroller programs and can help solve timing problems. Interrupts and event handling provide mechanisms to respond to external events, enabling your Raspberry Pi Pico to react quickly to changes without continuously polling (continuously checking the current value of a pin or variable).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What are Interrupts?<\/h3>\n\n\n\n<p>Interrupts are signals that pause the normal execution flow of a program to handle a specific event. When an interrupt happens, the processor stops the execution of the main program to execute a task and then gets back to the main program. That task is also referred to as an <em>interrupt handling routine<\/em>, as shown in the figure below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"992\" height=\"291\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupt.png?resize=992%2C291&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"How interrupts work\" class=\"wp-image-81979\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupt.png?w=992&amp;quality=100&amp;strip=all&amp;ssl=1 992w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupt.png?resize=300%2C88&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupt.png?resize=768%2C225&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 992px) 100vw, 992px\" \/><\/figure><\/div>\n\n\n<p>Using interrupts is especially useful to trigger an action whenever motion is detected or whenever a pushbutton is pressed without the need for constantly checking its state; or to make something happen periodically without constantly checking the time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Types of Interrupts<\/h3>\n\n\n\n<p>There are different types of interrupts: external interrupts and timed interrupts.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>External Interrupts<\/strong>: triggered by external signals such as a button press or a sensor reading\u2014this is hardware-based and associated with a specific GPIO pin. When the state of a pin changes, it will trigger a task.<\/li>\n\n\n\n<li><strong>Timed Interrupts (timers)<\/strong>: initiated based on time intervals, enabling periodic actions\u2014 this uses the RP2040\u2019s (or RP2350 depending on the Raspberry Pi Pico version) hardware timer to trigger callbacks at regular intervals.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"external-interrupts-micropython\">External Interrupts in MicroPython<\/h2>\n\n\n\n<p>To set an interrupt in MicroPython, you need to follow the next steps:<\/p>\n\n\n\n<p><strong>1<\/strong>.<strong> Define an interrupt handling function<\/strong><\/p>\n\n\n\n<p>The interrupt handling function should be as simple as possible, so the processor gets back to the execution of the main program quickly. The best approach is to signal the main code that the interrupt has happened by using a global variable. <\/p>\n\n\n\n<p>The interrupt handling function should accept a parameter of type <span class=\"rnthl rntliteral\">Pin<\/span>. This parameter is returned to the callback function and it refers to the GPIO that caused the interrupt. You\u2019ll better understand these concepts when we start testing some examples.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def handle_interrupt(pin):<\/code><\/pre>\n\n\n\n<p><strong>2. <\/strong>Set up the interrupt pin as an input. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>interrupt_pin = Pin(21, Pin.IN)<\/code><\/pre>\n\n\n\n<p><strong>3.<\/strong> Attach an interrupt to that pin by calling the <span class=\"rnthl rntliteral\">irq()<\/span> method:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>interrupt_pin.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">irq()<\/span> method accepts the following arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>trigger<\/strong>: this defines the trigger mode. There are 3 different conditions:<ul><li><span class=\"rnthl rntliteral\">Pin.IRQ_FALLING<\/span>: to trigger the interrupt whenever the pin goes from <span class=\"rnthl rntliteral\">HIGH<\/span> to <span class=\"rnthl rntliteral\">LOW<\/span>;<\/li><\/ul><ul><li><span class=\"rnthl rntliteral\">Pin.IRQ_RISING<\/span>: to trigger the interrupt whenever the pin goes from <span class=\"rnthl rntliteral\">LOW<\/span> to <span class=\"rnthl rntliteral\">HIGH<\/span>.<\/li><\/ul><ul><li><span class=\"rnthl rntliteral\">Pin.IRQ_LOW_LEVEL<\/span>: to trigger the interrupt when the pin is <span class=\"rnthl rntliteral\">LOW<\/span>.<\/li><\/ul>\n\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">Pin.IRQ_HIGH_LEVEL<\/span>: to trigger the interrupt when the pin is <span class=\"rnthl rntliteral\">HIGH<\/span>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>handler<\/strong>: this is the function that is called when an interrupt is detected, in this case, the <span class=\"rnthl rntliteral\">handle_interrupt()<\/span> function.<\/li>\n<\/ul>\n\n\n\n<p>The following picture will help you better understand the different trigger modes.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"438\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/interrupt-modes.png?resize=750%2C438&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Interrupt modes\" class=\"wp-image-169637\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/interrupt-modes.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/interrupt-modes.png?resize=300%2C175&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">External Interrupts with a Pushbutton (Examples)<\/h2>\n\n\n\n<p>To demonstrate how to handle interrupts, we\u2019ll build some simple projects with a pushbutton and an LED. Wire a pushbutton to GPIO 21 and an LED to GPIO 20.<\/p>\n\n\n\n<p>Here\u2019s a list of the parts you need for these examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico<\/a> or <a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-2-w\/\" title=\"\">Raspberry Pi Pico 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener\" title=\"\">5 mm LED<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">220 \u03a9 resistor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/pushbuttons-kit\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Pushbutton<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Breadboard<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Jumper wires<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Circuit Diagram<\/h3>\n\n\n\n<p>For the examples in this section, connect an LED to GPIO20 and a pushbutton to GPIO21. You can use the following diagram as a reference.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"842\" height=\"606\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Pushbutton-LED.png?resize=842%2C606&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Pushbutton LED Circuit\" class=\"wp-image-169638\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Pushbutton-LED.png?w=842&amp;quality=100&amp;strip=all&amp;ssl=1 842w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Pushbutton-LED.png?resize=300%2C216&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Pushbutton-LED.png?resize=768%2C553&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\"><strong>Recommended reading<\/strong>: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\" title=\"\">Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"detect-pushbutton-press\">Example 1: Detecting a Pushbutton Press<\/h3>\n\n\n\n<p>This is one of the simplest examples to demonstrate how interrupts work. Copy the following code to Thonny IDE.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-interrupts-micropython\/\r\n\r\nfrom machine import Pin\r\n\r\nbutton = Pin(21, Pin.IN, Pin.PULL_DOWN)\r\n\r\ndef button_pressed(pin):\r\n    print(&quot;Button Pressed!&quot;)\r\n\r\n# Attach the interrupt to the button's rising edge\r\nbutton.irq(trigger=Pin.IRQ_RISING, handler=button_pressed)<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/interrupts\/Pushbutton_Interrupt.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>In this example, you create an input object called <span class=\"rnthl rntliteral\">button<\/span>. [<a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-outputs-inputs-micropython\/\" title=\"\">Learn more about RPi Pico Inputs here<\/a>]<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button = Pin(21, Pin.IN, Pin.PULL_DOWN)<\/code><\/pre>\n\n\n\n<p>Then, you add an interrupt to that pin as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button.irq(trigger=Pin.IRQ_RISING, handler=button_pressed)<\/code><\/pre>\n\n\n\n<p>This line means that when the button signal goes into <span class=\"rnthl rntliteral\">RISING<\/span> mode (from <span class=\"rnthl rntliteral\">LOW<\/span> to <span class=\"rnthl rntliteral\">HIGH<\/span>), the <span class=\"rnthl rntliteral\">button_pressed<\/span> function will be called.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">button_pressed<\/span> function, simply prints on the Shell that the button was pressed.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def button_pressed(pin):\n&nbsp; &nbsp; print(\"Button Pressed!\")<\/code><\/pre>\n\n\n\n<p>Notice that the definition of the function comes before creating the interrupt. In MicroPython, any user-defined function must be defined before being called.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Testing the Code<\/h4>\n\n\n\n<p>Run the previous code on your Raspberry Pi Pico board. You\u2019ll get a message on the Shell every time you press the pushbutton.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Button-Press-Interrupt.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Detect a Pushbutton Press\" class=\"wp-image-169639\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Button-Press-Interrupt.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Button-Press-Interrupt.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"643\" height=\"205\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Detect-Button-Press-Python-Shell-Demonstration.png?resize=643%2C205&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Detect a Pushbutton Press - Micropython Shell Thonny IDE\" class=\"wp-image-169640\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Detect-Button-Press-Python-Shell-Demonstration.png?w=643&amp;quality=100&amp;strip=all&amp;ssl=1 643w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Detect-Button-Press-Python-Shell-Demonstration.png?resize=300%2C96&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"count-number-of-presses\">Example 2: Counting the Number of Button Presses<\/h3>\n\n\n\n<p>In this example, we\u2019ll count the number of button presses with the help of a global variable. This will help you better understand how global variables work.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-interrupts-micropython\/\r\n\r\nfrom machine import Pin\r\n\r\nbutton = Pin(21, Pin.IN, Pin.PULL_DOWN)\r\ncounter = 0  # Initialize the button press count\r\n\r\ndef button_pressed(pin):\r\n    global counter # Declare variable as global\r\n    counter +=1\r\n    print(&quot;Button Pressed! Count: &quot;, counter)\r\n\r\n# Attach the interrupt to the button's rising edge\r\nbutton.irq(trigger=Pin.IRQ_RISING, handler=button_pressed)\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/interrupts\/Pushbutton_Interrupt_Counter.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>In this example, we create a global variable called <span class=\"rnthl rntliteral\">counter<\/span> \u2014 it is declared and initialized outside the <span class=\"rnthl rntliteral\">button_pressed<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>counter = 0  # Initialize the button press count<\/code><\/pre>\n\n\n\n<p>To use that variable inside the function, we need to use the keyword <span class=\"rnthl rntliteral\">global<\/span> within the function\u2014 this way, the variable <span class=\"rnthl rntliteral\">counter<\/span> is defined globally, making it accessible and modifiable both inside and outside the function.<\/p>\n\n\n\n<p>When the button is pressed and the <span class=\"rnthl rntliteral\">button_pressed<\/span> function is called, the global <span class=\"rnthl rntliteral\">counter<\/span> variable is incremented, and its updated value is printed.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>global counter # Declare variable as global\ncounter +=1\nprint(\"Button Pressed! Count: \", counter)<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclgray\"><strong>Note:<\/strong> counter +=1 is short for counter = counter +1<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Testing the Code<\/h4>\n\n\n\n<p>Run the previous code on your Raspberry Pi Pico. Notice that it will print on the Shell the number of times you pressed the button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"689\" height=\"329\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/pushubtton-counter-testing-Micropython-RPi-Pico-Shell.png?resize=689%2C329&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"pushbutton counter Raspberry Pi Pico Micropython Thonny IDE\" class=\"wp-image-169641\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/pushubtton-counter-testing-Micropython-RPi-Pico-Shell.png?w=689&amp;quality=100&amp;strip=all&amp;ssl=1 689w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/pushubtton-counter-testing-Micropython-RPi-Pico-Shell.png?resize=300%2C143&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 689px) 100vw, 689px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading has-text-align-left\" id=\"toggle-led-button-press\">Example 3: Toggling an LED on each Button Press<\/h3>\n\n\n\n<p>This is similar to the previous example, but we also toggle an LED when you press the pushbutton.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-interrupts-micropython\/\r\n\r\nfrom machine import Pin\r\n\r\nled = Pin(20, Pin.OUT)\r\nbutton = Pin(21, Pin.IN, Pin.PULL_DOWN)\r\ncounter = 0   # Initialize the button press count\r\n\r\ndef button_pressed(pin):\r\n    global counter # Declare variable as global\r\n    counter +=1\r\n    print(&quot;Button Pressed! Count: &quot;, counter)\r\n    \r\n    # Toggle the LED on each button press\r\n    led.value(not led.value())\r\n\r\n# Attach the interrupt to the button's rising edge\r\nbutton.irq(trigger=Pin.IRQ_RISING, handler=button_pressed)<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/interrupts\/Pushbutton_Counter_LED.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>In this example, we just need to declare a new <span class=\"rnthl rntliteral\">Pin<\/span> object for the LED, in this case on GPIO 20.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = Pin(20, Pin.OUT)<\/code><\/pre>\n\n\n\n<p>Then, we add the instruction to toggle the LED inside the <span class=\"rnthl rntliteral\">button_pressed<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.value(not led.value())<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-demonstration.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Pushbutton LED on\" class=\"wp-image-132440\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-demonstration.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-demonstration.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\">Button Bouncing<\/h4>\n\n\n\n<p>You must have noticed that sometimes, the pushbutton will count more than one press when in fact, you just pressed once. This is called mechanical bouncing, and it\u2019s very common in mechanical buttons like pushbuttons.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"476\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/button-bounce.png?resize=750%2C476&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"debounce a pushbutton\" class=\"wp-image-169643\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/button-bounce.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/button-bounce.png?resize=300%2C190&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>This happens because the electrical contacts inside the button connect and disconnect very quickly before reaching a steady state, which will cause the system to register multiple press events, causing an inaccurate count.<\/p>\n\n\n\n<p>To prevent this issue, we can add some debouncing techniques using delays or timer interrupts. We\u2019ll take a look at timer interrupts and events in the following section, where you\u2019ll learn how to debounce a button.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Other Applications<\/h4>\n\n\n\n<p>Besides buttons, external interrupts can be used for a wide variety of applications on the Raspberry Pi Pico, including sensor inputs, rotary encoders, and other real-time event-driven scenarios.<\/p>\n\n\n\n<p>One of the most common applications is for PIR motion sensors. These sensors go to a triggered state (<span class=\"rnthl rntliteral\">HIGH<\/span>) for a few seconds after motion is detected. So, using an interrupt in rising mode is a great way to detect when the sensor detects motion.<\/p>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Recommended reading<\/strong>: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-motion-pir-micropython\/\" title=\"\">Raspberry Pi Pico: Detect Motion using a PIR Sensor (MicroPython)<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-PIR-Motion-Sensor-Detect-Motion-Demonstration.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with a PIR motion sensor\" class=\"wp-image-136749\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-PIR-Motion-Sensor-Detect-Motion-Demonstration.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-PIR-Motion-Sensor-Detect-Motion-Demonstration.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>There are also other sensors like smoke sensors, rain sensors, water level sensors, and sound sensors, among others, that send a digital signal when the readings cross a certain threshold. Detecting when the sensor sends such a signal can also be achieved with interrupts because we can detect that state without having to constantly poll the value on a certain GPIO.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"timer-interrupts\">Timer Interrupts<\/h2>\n\n\n\n<p>Using timer interrupts is especially useful to make something happen periodically or after a predefined period without constantly checking the elapsed time.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"250\" height=\"250\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/timer-interrupts.png?resize=250%2C250&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Timer Interrupts\" class=\"wp-image-169644\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/timer-interrupts.png?w=250&amp;quality=100&amp;strip=all&amp;ssl=1 250w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/timer-interrupts.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w\" sizes=\"(max-width: 250px) 100vw, 250px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">The MicroPython class <em>Timer<\/em><\/h3>\n\n\n\n<p>The Micropython <span class=\"rnthl rntliteral\">machine<\/span> module comes with a class called <span class=\"rnthl rntliteral\">Timer<\/span> that provides methods to execute a callback function periodically within a given period or once after some predefined delay. This is useful to schedule events or to run periodic tasks without the need to constantly check the elapsed time.<\/p>\n\n\n\n<p>Let\u2019s take a quick look at the <span class=\"rnthl rntliteral\">Timer<\/span> constructors.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Creating a Timer<\/h4>\n\n\n\n<p>To create a timer, call the <span class=\"rnthl rntliteral\">Timer()<\/span> constructor as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>my_timer = Timer()<\/code><\/pre>\n\n\n\n<p>Then, you initialize a timer using the <span class=\"rnthl rntliteral\">init()<\/span> method on the <span class=\"rnthl rntliteral\">Timer()<\/span> object and you pass as argument the timer mode, period, and the callback function. Here\u2019s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>my_timer.init(mode=Timer.PERIODIC, period=1000, callback=timer_callback)<\/code><\/pre>\n\n\n\n<p>This initializes a periodic timer that will run the <span class=\"rnthl rntliteral\">timer_callback<\/span> function every 1000 milliseconds (1 second). You can change the period parameter to any desired period.<\/p>\n\n\n\n<p>Instead of calling the callback function periodically, you may also want to run it once after a predefined time. For that, you can use the <span class=\"rnthl rntliteral\">Timer.ONE_SHOT<\/span> mode as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>my_timer.init(mode=Timer.ONE_SHOT, period=1000, callback=timer_callback)<\/code><\/pre>\n\n\n\n<p>This line of code configures a timer (<span class=\"rnthl rntliteral\">my_timer<\/span>) to run in a one-shot mode, triggering the specified callback function (<span class=\"rnthl rntliteral\">timer_callback<\/span>) after 1000 milliseconds.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"blink-led-with-timer\">Example 1: Blinking an LED with a Timer<\/h2>\n\n\n\n<p>In this example, you\u2019ll learn how to blink an LED using a Timer. This will help you understand how periodic timers work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Circuit Diagram<\/h3>\n\n\n\n<p>We\u2019ll blink an LED connected to GPIO 20. So, wire an LED to your Raspberry Pi Pico on that GPIO. You can use the following diagram as a reference.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"681\" height=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-with-LED-circuit-diagram.jpg?resize=681%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico Connected to an LED - schematic diagram\" class=\"wp-image-169645\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-with-LED-circuit-diagram.jpg?w=681&amp;quality=100&amp;strip=all&amp;ssl=1 681w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-with-LED-circuit-diagram.jpg?resize=300%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 681px) 100vw, 681px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<p>The following example makes use of the <span class=\"rnthl rntliteral\">Timer<\/span> class to blink an LED every half a second.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-interrupts-micropython\/\r\n\r\nfrom machine import Pin, Timer\r\nfrom time import sleep\r\n\r\n# LED pin\r\nled_pin = 20\r\nled = Pin(led_pin, Pin.OUT)\r\n\r\n# Callback function for the timer\r\ndef toggle_led(timer):\r\n    led.value(not led.value())  # Toggle the LED state (ON\/OFF)\r\n\r\n# Create a periodic timer\r\nblink_timer = Timer()\r\nblink_timer.init(mode=Timer.PERIODIC, period=500, callback=toggle_led)  # Timer repeats every half second\r\n\r\n# Main loop (optional)\r\nwhile True:\r\n    print('Main Loop is running')\r\n    sleep(2)<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/interrupts\/Blink_LED_Timer.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>In this code, we create a timer called <span class=\"rnthl rntliteral\">blink_timer<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>blink_timer = Timer()<\/code><\/pre>\n\n\n\n<p>Then, we initialize the timer with the following parameters:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>blink_timer.init(mode=Timer.PERIODIC, period=500, callback=toggle_led)<\/code><\/pre>\n\n\n\n<p>This means this timer will call the <span class=\"rnthl rntliteral\">toggle_led<\/span> function every 500 milliseconds, forever (or until you stop the program).<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">toggle_led<\/span> function, as the name suggests, will toggle the LED state:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Callback function for the timer\ndef toggle_led(timer):\n&nbsp; &nbsp; led.value(not led.value()) &nbsp;# Toggle the LED state (ON\/OFF)<\/code><\/pre>\n\n\n\n<p>The callback functions for the timer event must take one argument (<span class=\"rnthl rntliteral\">def toggle_led(<strong>timer<\/strong>)<\/span>:). The <span class=\"rnthl rntliteral\">Timer<\/span> object is passed automatically as an argument to that function when the event is triggered. You must have that argument even if you don\u2019t use it.<\/p>\n\n\n\n<p>With timers, you can also have other tasks running on the main loop without interfering with each other. For example, in our case, in the main loop, we\u2019ll print a message every two seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>while True:\n&nbsp; &nbsp; print('Main Loop is running')\n&nbsp; &nbsp; sleep(2)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>With an LED connected to GPIO 20, run the previous code on your board.<\/p>\n\n\n\n<p>You\u2019ll get the message \u201cMain Loop is running\u201d every two seconds on the Shell, while the LED is blinking every half a second at the same time.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"689\" height=\"239\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-main-loop-running-timer-thonny-ide.png?resize=689%2C239&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico main loop running on Thonny IDE\" class=\"wp-image-169646\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-main-loop-running-timer-thonny-ide.png?w=689&amp;quality=100&amp;strip=all&amp;ssl=1 689w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-main-loop-running-timer-thonny-ide.png?resize=300%2C104&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 689px) 100vw, 689px\" \/><\/figure><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"714\" height=\"402\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Blinking-LED-2.jpg?resize=714%2C402&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico Blinking LED 1\" class=\"wp-image-169647\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Blinking-LED-2.jpg?w=714&amp;quality=100&amp;strip=all&amp;ssl=1 714w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Blinking-LED-2.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"714\" height=\"402\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Blinking-LED-1.jpg?resize=714%2C402&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico Blinking LED 1\" class=\"wp-image-169648\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Blinking-LED-1.jpg?w=714&amp;quality=100&amp;strip=all&amp;ssl=1 714w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Blinking-LED-1.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"blink-multiple-leds\">Example 2: Blinking Multiple LEDs at Different Frequencies<\/h2>\n\n\n\n<p>After testing the previous example, it\u2019s now easy to understand that if you create multiple timers, you can run multiple tasks at different frequencies. In this example, we\u2019ll blink two different LEDs, one every half a second, and another every two seconds.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Circuit Diagram<\/h3>\n\n\n\n<p>Wire two LEDs to the Raspberry Pi Pico (to distinguish between the different LEDs, we\u2019ll use different colors):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Blue LED: GPIO 19 &gt; will blink every half a second.<\/li>\n\n\n\n<li>Green LED: GPIO 20 &gt; will blink every two seconds;<\/li>\n<\/ul>\n\n\n\n<p>You can use the following diagram as a reference to wire the circuit.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"683\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Rpi-Pico-Wiring-two-LEDs.png?resize=969%2C683&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Wiring Two LEDs\" class=\"wp-image-169650\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Rpi-Pico-Wiring-two-LEDs.png?w=969&amp;quality=100&amp;strip=all&amp;ssl=1 969w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Rpi-Pico-Wiring-two-LEDs.png?resize=300%2C211&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Rpi-Pico-Wiring-two-LEDs.png?resize=768%2C541&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 969px) 100vw, 969px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<p>The following code uses Timers to blink two different LEDs at different frequencies.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-interrupts-micropython\/\r\n\r\nfrom machine import Pin, Timer\r\nfrom time import sleep\r\n\r\n# LEDs\r\ngreen_led_pin = 19\r\ngreen_led = Pin(green_led_pin, Pin.OUT)\r\nblue_led_pin = 20\r\nblue_led = Pin(blue_led_pin, Pin.OUT)\r\n\r\n# Callback function for the green timer\r\ndef toggle_green_led(timer):\r\n    green_led.toggle()  # Toggle the LED state (ON\/OFF)\r\n    print('Green LED is: ', green_led.value())\r\n\r\n# Callback function for the blue timer\r\ndef toggle_blue_led(timer):\r\n    blue_led.toggle()  # Toggle the LED state (ON\/OFF)\r\n    print('Blue LED is: ', blue_led.value())\r\n\r\n# Create periodic timers\r\ngreen_timer = Timer()\r\nblue_timer = Timer()\r\n\r\n# Init the timers\r\ngreen_timer.init(mode=Timer.PERIODIC, period=500, callback=toggle_green_led)  # Timer repeats every 0.5 second\r\nblue_timer.init(mode=Timer.PERIODIC, period=2000, callback=toggle_blue_led)  # Timer repeats every 2 seconds\r\n\r\n# Main loop (optional)\r\nwhile True:\r\n    print('Main Loop is running')\r\n    sleep(2)<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/interrupts\/Multiple_LEDs_Timer.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>In this code, we create two different timers, one for each LED:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>green_timer = Timer()\nblue_timer = Timer()<\/code><\/pre>\n\n\n\n<p>Then, we call the corresponding callback functions at different intervals:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Timer repeats every 0.5 second\ngreen_timer.init(mode=Timer.PERIODIC, period=500, callback=toggle_green_led)\n# Timer repeats every 2 seconds\nblue_timer.init(mode=Timer.PERIODIC, period=2000, callback=toggle_blue_led) &nbsp;<\/code><\/pre>\n\n\n\n<p>The callback functions simply toggle the current LED value:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Callback function for the green timer\ndef toggle_green_led(timer):\n&nbsp; &nbsp; green_led.toggle() &nbsp;# Toggle the LED state (ON\/OFF)\n&nbsp; &nbsp; print('Green LED is: ', green_led.value())\n\n# Callback function for the blue timer\ndef toggle_blue_led(timer):\n&nbsp; &nbsp; blue_led.toggle() &nbsp;# Toggle the LED state (ON\/OFF)\n&nbsp; &nbsp; print('Blue LED is: ', blue_led.value())<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclgray\"><strong>The <span class=\"rnthl rntliteral\">toggle()<\/span> function:<\/strong> there\u2019s a method in MicroPython called <span class=\"rnthl rntliteral\">toggle()<\/span> that you can use on output Pin objects like LEDs, for example. This changes the state of a digital pin from its current state to the opposite state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>Run the previous code on the Raspberry Pi Pico. You\u2019ll notice that the two LEDs will blink at different frequencies.<\/p>\n\n\n\n<div class=\"wp-block-jetpack-tiled-gallery aligncenter is-style-rectangular\"><div class=\"\"><div class=\"tiled-gallery__gallery\"><div class=\"tiled-gallery__row\"><div class=\"tiled-gallery__col\" style=\"flex-basis:50.00000%\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-1.jpg?strip=info&#038;w=600&#038;ssl=1 600w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-1.jpg?strip=info&#038;w=714&#038;ssl=1 714w\" alt=\"RPi Pico LEDs blinking at different frequencies\" data-height=\"402\" data-id=\"169652\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=169652#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-1.jpg\" data-width=\"714\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-1.jpg?ssl=1\" data-amp-layout=\"responsive\" aria-label=\"Open image 1 of 4 in full-screen\"\/><\/figure><\/div><div class=\"tiled-gallery__col\" style=\"flex-basis:50.00000%\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-2.jpg?strip=info&#038;w=600&#038;ssl=1 600w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-2.jpg?strip=info&#038;w=714&#038;ssl=1 714w\" alt=\"RPi Pico LEDs blinking at different frequencies\" data-height=\"402\" data-id=\"169653\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=169653#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-2.jpg\" data-width=\"714\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-2.jpg?ssl=1\" data-amp-layout=\"responsive\" aria-label=\"Open image 2 of 4 in full-screen\"\/><\/figure><\/div><\/div><div class=\"tiled-gallery__row\"><div class=\"tiled-gallery__col\" style=\"flex-basis:50.00000%\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-3.jpg?strip=info&#038;w=600&#038;ssl=1 600w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-3.jpg?strip=info&#038;w=714&#038;ssl=1 714w\" alt=\"RPi Pico LEDs blinking at different frequencies\" data-height=\"402\" data-id=\"169655\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=169655#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-3.jpg\" data-width=\"714\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-3.jpg?ssl=1\" data-amp-layout=\"responsive\" aria-label=\"Open image 3 of 4 in full-screen\"\/><\/figure><\/div><div class=\"tiled-gallery__col\" style=\"flex-basis:50.00000%\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-4.jpg?strip=info&#038;w=600&#038;ssl=1 600w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-4.jpg?strip=info&#038;w=714&#038;ssl=1 714w\" alt=\"RPi Pico LEDs blinking at different frequencies\" data-height=\"402\" data-id=\"169654\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=169654#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-4.jpg\" data-width=\"714\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-blink-leds-different-frequencies-4.jpg?ssl=1\" data-amp-layout=\"responsive\" aria-label=\"Open image 4 of 4 in full-screen\"\/><\/figure><\/div><\/div><\/div><\/div><\/div>\n\n\n\n<p>At the same time, you\u2019ll get a message from the <span class=\"rnthl rntliteral\">while<\/span> loop every two seconds. This shows that the other tasks don\u2019t interfere with our loop.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"689\" height=\"237\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-multiple-timers-and-loop-running.png?resize=689%2C237&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico multiple timers and loop running\" class=\"wp-image-169651\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-multiple-timers-and-loop-running.png?w=689&amp;quality=100&amp;strip=all&amp;ssl=1 689w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-multiple-timers-and-loop-running.png?resize=300%2C103&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 689px) 100vw, 689px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"debouncing-pushbutton-timer\">Example 3: Debouncing a Pushbutton with a Timer<\/h2>\n\n\n\n<p>As we\u2019ve mentioned previously, we can use timers to help debounce a pushbutton to ensure we don\u2019t get false button presses. There are many ways to debounce a pushbutton, the example below is just one of many possibilities.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-interrupts-micropython\/\r\n\r\nfrom machine import Pin, Timer\r\n\r\nled = Pin(20, Pin.OUT)\r\nbutton = Pin(21, Pin.IN, Pin.PULL_DOWN)\r\ncounter = 0  # Initialize the button press count\r\ndebounce_timer = None\r\n\r\ndef button_pressed(pin):\r\n    global counter, debounce_timer  # Declare variables as global\r\n\r\n    if debounce_timer is None:\r\n        counter += 1\r\n        print(&quot;Button Pressed! Count: &quot;, counter)\r\n        \r\n        # Toggle the LED on each button press\r\n        led.toggle()\r\n\r\n        # Start a timer for debounce period (e.g., 200 milliseconds)\r\n        debounce_timer = Timer()\r\n        debounce_timer.init(mode=Timer.ONE_SHOT, period=200, callback=debounce_callback)\r\n\r\ndef debounce_callback(timer):\r\n    global debounce_timer\r\n    debounce_timer = None\r\n\r\n# Attach the interrupt to the button's rising edge\r\nbutton.irq(trigger=Pin.IRQ_RISING, handler=button_pressed)<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/interrupts\/Pushbutton_Debounce.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You should already be familiar with most of the code, so we\u2019ll just take a look at the relevant parts for this example.<\/p>\n\n\n\n<p>This example uses a one-shot timer (<span class=\"rnthl rntliteral\">debounce_timer<\/span>) initiated on each button press with a specified debounce period, in this example 200 milliseconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>debounce_timer.init(mode=Timer.ONE_SHOT, period=200, callback=debounce_callback)<\/code><\/pre>\n\n\n\n<p>As with previous examples, we attach an interrupt to our pushbutton pin that will trigger the <span class=\"rnthl rntliteral\">button_pressed<\/span> function in rising mode (when the button is pressed).<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button.irq(trigger=Pin.IRQ_RISING, handler=button_pressed)<\/code><\/pre>\n\n\n\n<p>When the button is pressed, the <span class=\"rnthl rntliteral\">button_pressed<\/span> function is called, incrementing the counter, toggling the LED, and starting the one-shot timer for debounce.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>counter += 1\nprint(\"Button Pressed! Count: \", counter)\n# Toggle the LED on each button press\nled.toggle()\n# Start a timer for debounce period (e.g., 200 milliseconds)\ndebounce_timer = Timer()\ndebounce_timer.init(mode=Timer.ONE_SHOT, period=200, callback=debounce_callback)<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">debounce_callback<\/span> function of the timer is called when the one-shot timer expires, resetting the <span class=\"rnthl rntliteral\">debounce_timer<\/span> to <span class=\"rnthl rntliteral\">None<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def debounce_callback(timer):\n&nbsp; &nbsp; global debounce_timer\n&nbsp; &nbsp; debounce_timer = None<\/code><\/pre>\n\n\n\n<p>If the timer hasn\u2019t yet expired, if another button press is detected, it will not be accounted for because the <span class=\"rnthl rntliteral\">debounce_timer<\/span> hasn\u2019t been reset to <span class=\"rnthl rntliteral\">None<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>if debounce_timer is None:<\/code><\/pre>\n\n\n\n<p>Notice that we make use of global variables so that we can access them throughout all parts of the code, including inside the functions:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>global counter, debounce_timer &nbsp;# Declare variables as global<\/code><\/pre>\n\n\n\n<p>This is just one of the many ways to debounce a pushbutton.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The use of <em>None<\/em><\/h4>\n\n\n\n<p>In Python\/MicroPython, <span class=\"rnthl rntliteral\">None<\/span> is often used as a placeholder or default value to indicate the absence of a meaningful value. In this previous example, <span class=\"rnthl rntliteral\">None<\/span> helps us manage the state of the <span class=\"rnthl rntliteral\">debounce_timer<\/span> variable.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">debounce_timer<\/span> is initially set to <span class=\"rnthl rntliteral\">None<\/span> to indicate that no debounce timer is currently active.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>debounce_timer = None<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">button_pressed<\/span> function, the following condition checks if there is no active debounce timer.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>if debounce_timer is None:<\/code><\/pre>\n\n\n\n<p>If there is no active timer (<span class=\"rnthl rntliteral\">None<\/span>), the button press actions are performed, and a new one-shot timer, <span class=\"rnthl rntliteral\">debounce_timer<\/span> is initiated.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>debounce_timer = Timer()\ndebounce_timer.init(mode=Timer.ONE_SHOT, period=200, callback=debounce_callback)<\/code><\/pre>\n\n\n\n<p>When the one-shot timer expires, the <span class=\"rnthl rntliteral\">debounce_callback<\/span> function is called, and it sets <span class=\"rnthl rntliteral\">debounce_timer<\/span> back to <span class=\"rnthl rntliteral\">None<\/span>, indicating that the debounce period has ended.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def debounce_callback(timer):\n&nbsp; &nbsp; global debounce_timer\n&nbsp; &nbsp; debounce_timer = None<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>Run the code on your Raspberry Pi Pico. Press the push button multiple times. You\u2019ll notice that you won\u2019t get false positives anymore. If you do, you need to increase the 200 milliseconds debounce period on the <span class=\"rnthl rntliteral\">debounce_timer<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>debounce_timer.init(mode=Timer.ONE_SHOT, <strong>period<\/strong><strong>=<\/strong><strong>200<\/strong>, callback=debounce_callback)<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-demonstration.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Pushbutton LED on\" class=\"wp-image-132440\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-demonstration.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-demonstration.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"689\" height=\"233\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-pushbutton-debounce-with-timer.png?resize=689%2C233&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico Pushbutton Debounce with Timer\" class=\"wp-image-169656\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-pushbutton-debounce-with-timer.png?w=689&amp;quality=100&amp;strip=all&amp;ssl=1 689w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/rpi-pico-pushbutton-debounce-with-timer.png?resize=300%2C101&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 689px) 100vw, 689px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"pir-sensor-interrupts-and-timers\">Example 4: Interrupts and Timers with a PIR Motion Sensor<\/h2>\n\n\n\n<p>In this example, we\u2019ll exemplify the use of single-shot timers with a PIR motion sensor. We\u2019ll build a simple project with a PIR motion sensor. Whenever motion is detected, we\u2019ll light up an LED for 20 seconds. After that time, the LED will turn off.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"978\" height=\"181\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Using-PIR-Motion-Sensor-LED-Control.png?resize=978%2C181&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Using a PIR motion sensor for LED control\" class=\"wp-image-169657\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Using-PIR-Motion-Sensor-LED-Control.png?w=978&amp;quality=100&amp;strip=all&amp;ssl=1 978w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Using-PIR-Motion-Sensor-LED-Control.png?resize=300%2C56&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Using-PIR-Motion-Sensor-LED-Control.png?resize=768%2C142&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 978px) 100vw, 978px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">The PIR Motion Sensor<\/h3>\n\n\n\n<p>A Passive Infrared (PIR) motion sensor is designed to detect infrared radiation emitted by objects in its field of view. PIR sensors detect changes in infrared radiation, primarily emitted by living things. When there is motion in the sensor&#8217;s range, there are fluctuations in the detected infrared radiation. The following picture shows the popular HC-SR501 motion sensor.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/HC-SR501-Motion-Sensor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"HC-SR501 PIR Motion Sensor\n\" class=\"wp-image-136744\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/HC-SR501-Motion-Sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/HC-SR501-Motion-Sensor.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><figcaption class=\"wp-element-caption\">HC-SR501 PIR Motion Sensor<br><\/figcaption><\/figure>\n\n\n\n<p>For a more in-depth guide about the PIR motion sensor with the Raspberry Pi Pico, we recommend reading the following guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-motion-pir-micropython\/\" title=\"\">Raspberry Pi Pico: Detect Motion using a PIR Sensor (MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<p>The sensor outputs a HIGH signal if it detects movement, or LOW if it doesn\u2019t detect any movement. The digital output from the PIR sensor can be read by a Raspberry Pi Pico GPIO pin, allowing you to program specific actions based on the detected motion status. In this example, we\u2019ll simply light up an LED, but it can be used for useful applications like sending an email, triggering an alarm, etc.<\/p>\n\n\n\n<p>An important concept about PIR motion sensors is the <em>dwell time <\/em>(reset time or sensor delay)\u2014it is the duration during which a PIR motion sensor&#8217;s output remains HIGH after detecting motion before returning to a LOW state.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"358\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/PIR-Motion-Sensor-How-it-works.png?resize=1024%2C358&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"PIR motion sensor how it works\" class=\"wp-image-136745\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/PIR-Motion-Sensor-How-it-works.png?resize=1024%2C358&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/PIR-Motion-Sensor-How-it-works.png?resize=300%2C105&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/PIR-Motion-Sensor-How-it-works.png?resize=768%2C268&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/PIR-Motion-Sensor-How-it-works.png?resize=1536%2C537&amp;quality=100&amp;strip=all&amp;ssl=1 1536w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/PIR-Motion-Sensor-How-it-works.png?resize=2048%2C716&amp;quality=100&amp;strip=all&amp;ssl=1 2048w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/PIR-Motion-Sensor-How-it-works.png?w=2400&amp;quality=100&amp;strip=all&amp;ssl=1 2400w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>For this example, we\u2019ll use the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico<\/a> or <a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-2-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener\" title=\"\">5 mm LED<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">220 \u03a9 resistor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mini-hc-sr505-pir-motion-sensor\/\">PIR Motion se<\/a><a href=\"https:\/\/makeradvisor.com\/tools\/mini-hc-sr505-pir-motion-sensor\/\" target=\"_blank\" rel=\"noopener\" title=\"\">n<\/a><a href=\"https:\/\/makeradvisor.com\/tools\/mini-hc-sr505-pir-motion-sensor\/\">sor (Mini AM312)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Breadboard<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Jumper wires<\/a><\/li>\n<\/ul>\n\n\n\n<p><strong>Note:&nbsp;<\/strong>the&nbsp;<a href=\"https:\/\/makeradvisor.com\/tools\/mini-hc-sr505-pir-motion-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">Mini AM312 PIR Motion Sensor<\/a>&nbsp;we\u2019re using in this project operates at 3.3V. However, if you\u2019re using another PIR motion sensor like the&nbsp;<a href=\"https:\/\/makeradvisor.com\/tools\/pir-motion-sensor-hc-sr501\/\" target=\"_blank\" rel=\"noreferrer noopener\">HC-SR501<\/a>, it operates at 5V. You can either&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/modifying-cheap-pir-motion-sensor-to-work-at-3-3v\/\" title=\"\">modify it to operate at 3.3V<\/a>&nbsp;or simply power it using the VBUS pin that will output 5V if you\u2019re powering the Raspberry Pi Pico via USB using 5V.<\/p>\n\n\n\n<p>This example can also be made with a pushbutton instead.<\/p>\n\n\n\n<p>In the figure below, we provide the pinout for the Mini AM312 PIR motion sensor. If you\u2019re using another motion sensor, please check its pinout before assembling the circuit.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/mini-pir-motion-sensor-pinout.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Mini pir motion sensor pinout\" class=\"wp-image-169658\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/mini-pir-motion-sensor-pinout.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/mini-pir-motion-sensor-pinout.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The following table shows the connections between the PIR motion sensor and the Raspberry Pi Pico:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>PIR Motion Sensor<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Raspberry Pi Pico<\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">GND<\/td><td class=\"has-text-align-center\" data-align=\"center\">GND<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Data<\/td><td class=\"has-text-align-center\" data-align=\"center\">Any digital pin (example GPIO 21)<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">3.3V<\/td><td class=\"has-text-align-center\" data-align=\"center\">3V3(OUT)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Circuit Diagram<\/h3>\n\n\n\n<p>We\u2019ll wire the PIR motion sensor data pin to GPIO 21 and the LED to GPIO 20.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"1021\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-LED-Pushbutton-Circuit.png?resize=903%2C1021&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico LED Pushbutton Circuit\" class=\"wp-image-169659\" style=\"width:713px;height:auto\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-LED-Pushbutton-Circuit.png?w=903&amp;quality=100&amp;strip=all&amp;ssl=1 903w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-LED-Pushbutton-Circuit.png?resize=265%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 265w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-LED-Pushbutton-Circuit.png?resize=768%2C868&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 903px) 100vw, 903px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<p>The following code will set the PIR motion sensor as an interrupt. When motion is detected, an LED will be turned on for 20 seconds after the last detected movement. After that period, the LED will be turned off, and the motion sensor will be able to detect motion again.<\/p>\n\n\n\n<p>With this example, you\u2019ll see another application of one-shot timers while learning about detecting motion with PIR sensors.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-interrupts-micropython\/\r\n\r\nfrom machine import Pin, Timer\r\nimport time\r\n\r\n# PIR sensor pin\r\npir_pin = 21\r\n# LED pin \r\nled_pin = 20\r\n\r\n# Set up LED and PIR sensor\r\nled = Pin(led_pin, Pin.OUT)\r\npir = Pin(pir_pin, Pin.IN)\r\n\r\n# Create timer\r\nmotion_timer = Timer()\r\n\r\n# Create timer\r\nmotion = False\r\nmotion_printed = False\r\n\r\ndef handle_motion(pin):\r\n    global motion\r\n    motion = True\r\n    motion_timer.init(mode=Timer.ONE_SHOT, period=20000, callback=turn_off_led)\r\n\r\ndef turn_off_led(timer):\r\n    global motion, motion_printed\r\n    led.off()  # Turn off the LED\r\n    print('Motion stopped!')\r\n    motion = False\r\n    motion_printed = False\r\n\r\n# Attach interrupt to the PIR motion sensor\r\npir.irq(trigger=Pin.IRQ_RISING, handler=handle_motion)\r\n\r\nwhile True:\r\n    if motion and not motion_printed:\r\n        print('Motion detected!')\r\n        led.on()  # Turn on the LED\r\n        motion_printed = True\r\n    elif not motion:\r\n        time.sleep(0.1)\r\n        # Other tasks that you might need to do in the loop<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/interrupts\/PIR_Motion_Sensor.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How the Code Works<\/h4>\n\n\n\n<p>We start by declaring the LED and the PIR motion sensor on the respective GPIO pins they are connected to.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># PIR sensor pin\npir_pin = 21\n# LED pin\nled_pin = 20\n# Set up LED and PIR sensor\nled = Pin(led_pin, Pin.OUT)\npir = Pin(pir_pin, Pin.IN)<\/code><\/pre>\n\n\n\n<p>We create a timer that will be responsible for turning off the LED after 20 seconds since motion was detected.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Create timer\nmotion_timer = Timer()<\/code><\/pre>\n\n\n\n<p>We create two global variables <span class=\"rnthl rntliteral\">motion<\/span>, and <span class=\"rnthl rntliteral\">motion_printed<\/span>. The <span class=\"rnthl rntliteral\">motion<\/span> variable is to signal throughout the code whether motion was detected or not. The <span class=\"rnthl rntliteral\">motion_printed<\/span> variable will allow us to check whether the \u2018Motion detected!\u2019 message was already printed on the Shell.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Create global variables\nmotion = False\nmotion_printed = False<\/code><\/pre>\n\n\n\n<p>We attach an interrupt to the PIR motion sensor in rising mode. This means that when motion is detected, the <span class=\"rnthl rntliteral\">handle_motion<\/span> function will run.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pir.irq(trigger=Pin.IRQ_RISING, handler=handle_motion)<\/code><\/pre>\n\n\n\n<div class=\"wp-block-columns rntbox rntclgray is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Things to Consider When Writing ISRs<\/strong><\/p>\n\n\n\n<p>Interrupts are handled in interrupt service routines (ISR), which is the function that runs when the interrupt happens (in our example, it\u2019s the <span class=\"rnthl rntliteral\">handle_motion<\/span> function). When an interrupt happens, the processor stops what it is doing, works temporarily on the ISR, and then gets back to what it was doing before (the main code).<\/p>\n\n\n\n<p>It is a good practice to build ISR as small as possible, so the processor gets back to the execution of the main program quickly. The best approach is to signal the main code that the interrupt has happened by using a flag or a counter, for example. Here, we use the <span class=\"rnthl rntliteral\">motion<\/span> variable as a flag to indicate that motion was detected. Then, the main code should have all the things we want to happen when motion is detected like turning an LED on and printing a message on the Shell.<\/p>\n\n\n\n<p>More details about writing ISRs <a href=\"https:\/\/docs.micropython.org\/en\/latest\/reference\/isr_rules.html#writing-interrupt-handlers\" target=\"_blank\" rel=\"noopener\" title=\"\">can be found here<\/a>.<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>Let\u2019s proceed with the code. Inside the <span class=\"rnthl rntliteral\">handle_motion function<\/span>, we set the global variable motion to <span class=\"rnthl rntliteral\">True<\/span>, so that we know in all parts of the code that motion was detected, and we initialize a one-shot timer that will turn off the LED after 20 seconds by calling the <span class=\"rnthl rntliteral\">turn_off_led()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def handle_motion(pin):\n&nbsp; &nbsp; global motion\n&nbsp; &nbsp; motion = True\n&nbsp; &nbsp; motion_timer.init(mode=Timer.ONE_SHOT, period=20000, callback=turn_off_led)<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">While<\/span> loop, we check whether motion was detected or not. If motion has been detected and if the message hasn\u2019t been printed yet on the Shell, we\u2019ll print a message, turn the LED on, and set the <span class=\"rnthl rntliteral\">motion_printed<\/span> variable to <span class=\"rnthl rntliteral\">True<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>while True:\n&nbsp; &nbsp; if motion and not motion_printed:\n&nbsp; &nbsp; &nbsp; &nbsp; print('Motion detected!')\n&nbsp; &nbsp; &nbsp; &nbsp; led.on() &nbsp;# Turn on the LED\n&nbsp; &nbsp; &nbsp; &nbsp; motion_printed = True<\/code><\/pre>\n\n\n\n<p>After the 20000 milliseconds, the <span class=\"rnthl rntliteral\">turn_off_led<\/span> function will run and turn the LED off and set the <span class=\"rnthl rntliteral\">motion<\/span> and <span class=\"rnthl rntliteral\">motion_printed<\/span> global variables to <span class=\"rnthl rntliteral\">False<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def turn_off_led(timer):\n&nbsp; &nbsp; global motion, motion_printed\n&nbsp; &nbsp; led.off() &nbsp;# Turn off the LED\n&nbsp; &nbsp; print('Motion stopped!')\n&nbsp; &nbsp; motion = False\n&nbsp; &nbsp; motion_printed = False<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">motion<\/span> variable can only become <span class=\"rnthl rntliteral\">True<\/span> again if <span class=\"rnthl rntliteral\">motion<\/span> is detected and the <span class=\"rnthl rntliteral\">handle_motion<\/span> function is called.<\/p>\n\n\n\n<p>There are numerous ways to achieve the same result. This is just one of the examples to show you how single-shot timers can be used in your code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>Run the previous code on your Raspberry Pi Pico. Wave your hand in front of the PIR motion sensor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Testing-PIR-Motion-Sensor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico Testing a PIR Motion Sensor\" class=\"wp-image-169660\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Testing-PIR-Motion-Sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Testing-PIR-Motion-Sensor.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>It will print a message on the Shell and turn on an LED for 20 seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"672\" height=\"274\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Testing-PIR-Motion-Sensor-Shell-Thonny-IDE.jpg.png?resize=672%2C274&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico - Testing the PIR Motion Sensor - Thonny IDE Shell\" class=\"wp-image-169661\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Testing-PIR-Motion-Sensor-Shell-Thonny-IDE.jpg.png?w=672&amp;quality=100&amp;strip=all&amp;ssl=1 672w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Testing-PIR-Motion-Sensor-Shell-Thonny-IDE.jpg.png?resize=300%2C122&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/figure><\/div>\n\n\n<p>After 20 seconds, the LED will turn off.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Testing-PIR-Motion-Sensor-2.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico Testing a PIR Motion Sensor\" class=\"wp-image-169662\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Testing-PIR-Motion-Sensor-2.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/RPi-Pico-Testing-PIR-Motion-Sensor-2.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this guide, you learned about interrupts with the Raspberry Pi Pico programmed with MicroPython. We covered external interrupts and timer interrupts.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this guide and the examples useful and that you can apply the concepts learned to your projects.<\/p>\n\n\n\n<p>If you want to learn more about programming and controlling the Raspberry Pi Pico using MicroPython, check out our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico\/Pico W with MicroPython<\/a> (eBook)<\/strong><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\">Free Raspberry Pi Pico projects and tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we\u2019ll show you how to handle interrupts with the Raspberry Pi Pico using MicroPython and build some project examples with a pushbutton and a PIR motion sensor. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico with Interrupts: External and Timer Interrupts (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-interrupts-micropython\/#more-169636\" aria-label=\"Read more about Raspberry Pi Pico with Interrupts: External and Timer Interrupts (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":169676,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324,326],"tags":[],"class_list":["post-169636","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/Raspberry-Pi-Pico-Interrupts.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/169636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=169636"}],"version-history":[{"count":12,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/169636\/revisions"}],"predecessor-version":[{"id":170604,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/169636\/revisions\/170604"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/169676"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=169636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=169636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=169636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}