{"id":81978,"date":"2019-03-09T12:05:36","date_gmt":"2019-03-09T12:05:36","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=81978"},"modified":"2020-07-30T14:15:45","modified_gmt":"2020-07-30T14:15:45","slug":"micropython-interrupts-esp32-esp8266","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-interrupts-esp32-esp8266\/","title":{"rendered":"MicroPython: Interrupts with ESP32 and ESP8266"},"content":{"rendered":"\n<p>Learn how to configure and handle interrupts using MicroPython firmware with ESP32 and ESP8266 boards. You&#8217;ll also build a project example with a PIR Motion Sensor.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts-micropython-esp32-esp8266.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-82153\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts-micropython-esp32-esp8266.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts-micropython-esp32-esp8266.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts-micropython-esp32-esp8266.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts-micropython-esp32-esp8266.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware flashed in your ESP32 or ESP8266. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Thonny IDE:<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li><\/ul><\/li><li>uPyCraft IDE:<ul><li>Install uPyCraft IDE (<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Windows<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Mac OS X<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Linux<\/a>)<\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">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. With interrupts you don\u2019t need to constantly check the current pin value. When a change is detected, an event is triggered (a function is called). <\/p>\n\n\n\n<p>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 as shown in the figure below.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><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=\"\" 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\n<p>This 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.<\/p>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>ESP32 interrupt pins:<\/strong> you can use all GPIOs as interrupts, except GPIO 6 to GPIO 11.<\/p>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>ESP8266 interrupt pins:<\/strong> you can use all GPIOs, except GPIO 16. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Set Up an Interrupt in MicroPython<\/h2>\n\n\n\n<p>To setup an interrupt in MicroPython, you need to follow the next steps:<\/p>\n\n\n\n<p><strong>1. Define an interrupt handling function<\/strong>. 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, for example. 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.<\/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> Setup the GPIO that will act as an interrupt pin as an input. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pir = Pin(14, 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>pir.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\"><li><strong>trigger:<\/strong> this defines the trigger mode. There are 3 different conditions:<ul><li><strong><span class=\"rnthl rntliteral\">Pin.IRQ_FALLING<\/span>:<\/strong> to trigger the interrupt whenever the pin goes from HIGH to LOW;<\/li><li><strong><span class=\"rnthl rntliteral\">Pin.IRQ_RISING<\/span>:<\/strong> to trigger the interrupt whenever the pin goes from LOW to HIGH.<\/li><li><strong><span class=\"rnthl rntliteral\">3<\/span>:<\/strong> to trigger the interrupt in both edges (this means, when any change is detected)<\/li><\/ul><\/li><li><strong>handler:<\/strong> this is a function that will be called when an interrupt is detected, in this case the <span class=\"rnthl rntliteral\">handle_interrupt()<\/span> function.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Project Example with PIR Motion Sensor<\/h2>\n\n\n\n<p>To demonstrate how to handle interrupts, we\u2019ll build a simple project with a PIR motion sensor. Whenever motion is detected we\u2019ll light up an LED for 20 seconds.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"135\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/pir-motion-sensor-with-micropython-esp32-esp8266.png?resize=750%2C135&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"pir motion sensor micropython esp32 esp8266\" class=\"wp-image-82033\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/pir-motion-sensor-with-micropython-esp32-esp8266.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/pir-motion-sensor-with-micropython-esp32-esp8266.png?resize=300%2C54&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Parts required<\/h3>\n\n\n\n<p>Here\u2019s a list of the parts you need to build the circuit:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a> (read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\">Best ESP32 development boards<\/a>) or <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a> (read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\">Best ESP8266 development boards<\/a>)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\">5mm LED<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">330 Ohm resistor<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mini-hc-sr505-pir-motion-sensor\/\" target=\"_blank\">Mini PIR motion sensor (AM312)<\/a> or <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/pir-motion-sensor-hc-sr501\/\" target=\"_blank\">PIR motion sensor (HC-SR501)<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\"> Jumper wires<\/a><\/li><\/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\">Schematic &#8211; ESP32<\/h3>\n\n\n\n<p>Follow the next schematic diagram if you\u2019re using an ESP32 board:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"641\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp32-bb.png?resize=1200%2C641&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-82034\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp32-bb.png?w=1388&amp;quality=100&amp;strip=all&amp;ssl=1 1388w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp32-bb.png?resize=300%2C160&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp32-bb.png?resize=768%2C411&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp32-bb.png?resize=1024%2C547&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp32-bb.png?resize=280%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 280w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"mce_47\">Schematic &#8211; ESP8266<\/h3>\n\n\n\n<p>Follow the next schematic diagram if you\u2019re using an ESP8266 board:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"541\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp8266_bb.png?resize=1200%2C541&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-82035\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp8266_bb.png?w=1278&amp;quality=100&amp;strip=all&amp;ssl=1 1278w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp8266_bb.png?resize=300%2C135&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp8266_bb.png?resize=768%2C346&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts_esp8266_bb.png?resize=1024%2C462&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<p class=\"rntbox rntcorange\"><strong>Important: <\/strong> the <a href=\"https:\/\/makeradvisor.com\/tools\/mini-hc-sr505-pir-motion-sensor\/\">Mini AM312 PIR Motion Sensor<\/a> we\u2019re using in this project operates at 3.3V. However, if you\u2019re using another PIR motion sensor like the <a href=\"https:\/\/makeradvisor.com\/tools\/pir-motion-sensor-hc-sr501\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">HC-SR501<\/a>, it operates at 5V. You can either <a href=\"https:\/\/randomnerdtutorials.com\/modifying-cheap-pir-motion-sensor-to-work-at-3-3v\/\">modify it to operate at 3.3V<\/a> or simply power it using the Vin pin.<\/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\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"286\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/mini-pir-pinout-am312.png?resize=750%2C286&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"mini-pir-pinout-am312\" class=\"wp-image-82038\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/mini-pir-pinout-am312.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/mini-pir-pinout-am312.png?resize=300%2C114&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<p>Here\u2019s the script that detects motion and lights up an LED whenever motion is detected. This code is compatible with both the ESP32 and ESP8266.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\n\nfrom machine import Pin\nfrom time import sleep\n\nmotion = False\n\ndef handle_interrupt(pin):\n  global motion\n  motion = True\n  global interrupt_pin\n  interrupt_pin = pin \n\nled = Pin(12, Pin.OUT)\npir = Pin(14, Pin.IN)\n\npir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)\n\nwhile True:\n  if motion:\n    print('Motion detected! Interrupt caused by:', interrupt_pin)\n    led.value(1)\n    sleep(20)\n    led.value(0)\n    print('Motion stopped!')\n    motion = False\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/interrupts_esp32_esp8266.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>To use interrupts, import the <span class=\"rnthl rntliteral\">Pin<\/span> class from the <span class=\"rnthl rntliteral\">machine<\/span> module. We also import the <span class=\"rnthl rntliteral\">sleep<\/span> method from the <span class=\"rnthl rntliteral\">time<\/span> module to add a delay in our script.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin\nfrom time import sleep<\/code><\/pre>\n\n\n\n<p>Create a variable called <span class=\"rnthl rntliteral\">motion<\/span> that can be either True of False. This variable will indicate whether motion was detected or not (this is the global variable that will be changed on the interrupt handling function).<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>motion = False<\/code><\/pre>\n\n\n\n<p>Then, create a function called <span class=\"rnthl rntliteral\">handle_interrupt<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def handle_interrupt(pin):\n  global motion\n  motion = True\n  global interrupt_pin\n  interrupt_pin = pin <\/code><\/pre>\n\n\n\n<p>This function will be called every time motion is detected. The <span class=\"rnthl rntliteral\">handle_interrupt<\/span> function has an input parameter (<span class=\"rnthl rntliteral\">pin<\/span>) in which an object of class <span class=\"rnthl rntliteral\">Pin<\/span> will be passed when the interrupt happens (it indicates which pin caused the interrupt). <\/p>\n\n\n\n<p>Here we&#8217;re saving the pin that caused the interrupt in the <span class=\"rnthl rntliteral\">interrupt_pin<\/span> variable. In this case, it is not very useful because we only have one interrupt pin. However, this can be useful if we have several interrupts that trigger the same interrupt handling function and we want to know which GPIO caused the interrupt.<\/p>\n\n\n\n<p>In our example, the <span class=\"rnthl rntliteral\">handle_interrupt<\/span> function simply changes the <span class=\"rnthl rntliteral\">motion<\/span> variable to <span class=\"rnthl rntliteral\">True<\/span> and saves the interrupt pin. You should keep your handling interrupt functions as short as possible and avoid using the <span class=\"rnthl rntliteral\">print()<\/span> function inside. Then, the main code should have all the things we want to happen when the interrupt happens.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> as you want <span class=\"rnthl rntliteral\">motion <\/span>to be usable both inside the function and throughout the code, it needs to be declared as global. Otherwise, when motion is detected nothing would happen, because the <span class=\"rnthl rntliteral\">motion<\/span> variable would be changing inside the function and not in the main body of the code.<\/p>\n\n\n\n<p>Proceeding with the code, we need to create two Pin objects. One for the LED on <span class=\"rnthl rntcblue\">GPIO 12<\/span>, and another for the PIR motion sensor on <span class=\"rnthl rntcyellow\">GPIO 14<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = Pin(12, Pin.OUT)\npir = Pin(14, Pin.IN)<\/code><\/pre>\n\n\n\n<p>Then, set an interrupt on the <span class=\"rnthl rntliteral\">pir<\/span> by calling the <span class=\"rnthl rntliteral\">irq()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, when the <span class=\"rnthl rntliteral\">motion<\/span>variable is True, we turn the LED on for 20 seconds and print a message that indicates that motion was detected and which pin caused the interrupt.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>if motion:\n  print('Motion detected! Interrupt caused by:', interrupt_pin)\n  led.value(1)\n  sleep(20)<\/code><\/pre>\n\n\n\n<p>After 20 seconds, turn the LED off, and print a message to indicate that motion stopped.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.value(0)\nprint('Motion stopped!')<\/code><\/pre>\n\n\n\n<p> Finally, set the <span class=\"rnthl rntliteral\">motion<\/span> variable to False:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>motion = False<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">motion<\/span> variable can only become True again, if motion is detected and the <span class=\"rnthl rntliteral\">handle_interrupt<\/span> function is called.<\/p>\n\n\n\n<p>For simplicity, in this example we use a delay to keep the LED on for 20 seconds. Ideally, you should use timers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Upload the code to your ESP32\/ESP8266 board. The LED should turn on for 20 seconds when motion is detected, and a message should be printed in the Shell.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"425\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/detect-motion-micropython.jpg?resize=750%2C425&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-82054\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/detect-motion-micropython.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/detect-motion-micropython.jpg?resize=300%2C170&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>After 20 seconds the LED turns off.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> the AM312 PIR motion sensor has a default delay time of 8 seconds. This means that it won\u2019t be triggered before 8 seconds have passed since the last trigger.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>We hope you&#8217;ve found this article interesting. We&#8217;ve learned how to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>setup a pin as an interrupt<\/li><li>handle that interrupt in your code<\/li><li>detect which GPIO pin caused the interrupt<\/li><\/ul>\n\n\n\n<p>In our example, we&#8217;ve used a PIR motion sensor to trigger the interrupt. But the example presented can also be used to detect a button press, for example.<\/p>\n\n\n\n<p>If you like programming the ESP32 and ESP8266 boards with MicroPython, and you want to learn more, please take a look at the following resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">[eBook] MicroPython Programming with ESP32 and ESP8266<\/a><\/strong> <\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-ws2812b-addressable-rgb-leds-neopixel-esp32-esp8266\/\">MicroPython: WS2812B Addressable RGB LEDs with ESP32 and ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/low-power-weather-station-datalogger-using-esp8266-bme280-micropython\/\">Low Power Weather Station Datalogger using ESP8266 and BME280 with MicroPython<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-mqtt-esp32-esp8266\/\">MicroPython \u2013 Getting Started with MQTT on ESP32\/ESP8266<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to configure and handle interrupts using MicroPython firmware with ESP32 and ESP8266 boards. You&#8217;ll also build a project example with a PIR Motion Sensor. <\/p>\n<p class=\"read-more-container\"><a href=\"https:\/\/randomnerdtutorials.com\/micropython-interrupts-esp32-esp8266\/\" class=\"read-more button\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":82153,"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":[276,281,265,309,264],"tags":[],"class_list":["post-81978","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp8266-project","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/interrupts-micropython-esp32-esp8266.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/81978","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=81978"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/81978\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/82153"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=81978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=81978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=81978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}