{"id":163417,"date":"2024-11-21T13:42:30","date_gmt":"2024-11-21T13:42:30","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=163417"},"modified":"2024-11-22T10:35:02","modified_gmt":"2024-11-22T10:35:02","slug":"micropython-raspberry-pi-pico-asynchronous-programming","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-raspberry-pi-pico-asynchronous-programming\/","title":{"rendered":"Raspberry Pi Pico Asynchronous Programming &#8211; Run Multiple Tasks (MicroPython)"},"content":{"rendered":"\n<p>In this guide, we\u2019ll take a look at the basics of MicroPython asynchronous programming with the Raspberry Pi Pico using the <span class=\"rnthl rntliteral\">asyncio<\/span> module. You\u2019ll learn to run multiple tasks concurrently, making the illusion of multitasking and avoiding blocking your code on long-running tasks.<\/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\/2024\/10\/Raspberry-Pi-Pico-Asynchronous-programming.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Asynchronous Programming \u2013 Run Multiple Tasks (MicroPython) \" class=\"wp-image-163427\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Asynchronous-programming.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Asynchronous-programming.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Asynchronous-programming.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Asynchronous-programming.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Asynchronous-programming.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 Pico?<\/strong> Check out our eBook: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico\/Pico W with MicroPython<\/a><\/p>\n\n\n\n<p>For example, your program can be waiting for the response of a server and still be able to do other tasks like checking if a button was pressed or blinking an LED at the same time.<\/p>\n\n\n\n<p>Asynchronous programming can be useful in projects that include: interacting with databases, communicating over networks (like when requesting data from a server, or when the Pico acts as a web server), reading sensor data, displaying output to a screen, receiving inputs from users, and much more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Table of Contents:<\/h2>\n\n\n\n<p>In this tutorial, we&#8217;ll cover the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#intro-async-programming\" title=\"\">Introducing Asynchronous Programming<\/a><\/li>\n\n\n\n<li><a href=\"#asyncio-module\" title=\"\">The <em>asyncio<\/em> MicroPython Module<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#event-loop\" title=\"\">Event Loop<\/a><\/li>\n\n\n\n<li><a href=\"#create-tasks\" title=\"\">Creating Tasks<\/a><\/li>\n\n\n\n<li><a href=\"#running-event-loop\" title=\"\">Running the Event Loop<\/a><\/li>\n\n\n\n<li><a href=\"#async-def\" title=\"\">Asynchronous Functions &#8211; <em>async def<\/em><\/a><\/li>\n\n\n\n<li><a href=\"#async-delay\" title=\"\">Asynchronous Delay &#8211; <em>acyncio.sleep()<\/em><\/a><\/li>\n\n\n\n<li><em><a href=\"#await\" title=\"\">await<\/a><\/em><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><a href=\"#async-program-example\" title=\"\">Basic Example of an Asynchronous Program<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before proceeding, make sure you check the following prerequisites:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MicroPython Firmware<\/h3>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-thumbnail\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"150\" height=\"150\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/micropython-logo.png?resize=150%2C150&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"micorpython logo\" class=\"wp-image-148797\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/micropython-logo.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/micropython-logo.png?w=250&amp;quality=100&amp;strip=all&amp;ssl=1 250w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/figure><\/div>\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<p>Alternatively, if you like programming using VS Code, you can start with the following tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-vs-code-micropython\/\">Programming Raspberry Pi Pico with VS Code and MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"intro-async-programming\">Introducing Asynchronous Programming<\/h2>\n\n\n\n<p>Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be responsive to other events while that task runs, rather than having to wait until that task has finished.<\/p>\n\n\n\n<p>This is achieved by executing tasks in a non-blocking manner and using callback functions to handle the results. This way, the program can continue executing other tasks while waiting for the results of the asynchronous task. On the other hand, in synchronous programming, each task must wait for the previous task to complete before starting.<\/p>\n\n\n\n<p><strong>In summary\u2026<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Asynchronous<\/strong> is a non-blocking architecture, so the execution of one task isn\u2019t dependent on another. Tasks can run simultaneously.<\/li>\n\n\n\n<li><strong>Synchronous<\/strong> is a blocking architecture, so the execution of each operation depends on completing the one before it.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"560\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/async-vs-sync-programming.png?resize=750%2C560&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Synchronous vs Asynchronous Programming\" class=\"wp-image-163420\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/async-vs-sync-programming.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/async-vs-sync-programming.png?resize=300%2C224&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\" id=\"asyncio-module\">The <em>asyncio<\/em> MicroPython Module<\/h2>\n\n\n\n<p>MicroPython provides the <span class=\"rnthl rntliteral\">asyncio<\/span> module, which is a lightweight asynchronous I\/O framework inspired by Python&#8217;s <span class=\"rnthl rntliteral\">asyncio<\/span> module. You can check all the details about this MicroPython module on the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.micropython.org\/en\/latest\/library\/asyncio.html\" target=\"_blank\" rel=\"noopener\" title=\"\">Documentation asyncio MicroPython module<\/a><\/li>\n<\/ul>\n\n\n\n<p>Let\u2019s take a quick look at some basic concepts you need to know to start using <span class=\"rnthl rntliteral\">asyncio<\/span> for asynchronous programming:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Event Loop:<\/strong> it&#8217;s a loop that continually checks for events (tasks or coroutines) and executes them.<\/li>\n\n\n\n<li><strong>Tasks:<\/strong> individual units of work or coroutines that are scheduled to run concurrently within the event loop<\/li>\n\n\n\n<li><strong>Asynchronous<\/strong> <strong>Functions:<\/strong> also known as <em>coroutines<\/em>, these are functions that can be paused and resumed without blocking other operations, enabling concurrent execution of multiple tasks.<\/li>\n\n\n\n<li><strong><em>await<\/em>:<\/strong> is a keyword used inside coroutines to pause the execution of the current coroutine until a specific event or operation completes, allowing other coroutines to run in the meantime.<\/li>\n<\/ul>\n\n\n\n<p>Now, let\u2019s take a more detailed look at each of those concepts and the workflow and methods from the <span class=\"rnthl rntliteral\">asyncio<\/span> module to write an asynchronous program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"event-loop\">Event Loop<\/h3>\n\n\n\n<p>An event loop is the core of asynchronous programming. It&#8217;s a loop that continually checks for events (tasks or coroutines) and executes them. In <span class=\"rnthl rntliteral\">asyncio<\/span>, you create an event loop using <span class=\"rnthl rntliteral\">asyncio.get_event_loop()<\/span>.<\/p>\n\n\n\n<p>The following line creates an instance of the event loop called <span class=\"rnthl rntliteral\">loop<\/span>. You can use that loop to manage and schedule asynchronous tasks.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>loop = asyncio.get_event_loop()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"create-tasks\">Creating Tasks<\/h3>\n\n\n\n<p>In asynchronous programming, tasks represent units of work. You create tasks to execute coroutines concurrently. Coroutines are functions defined with the <span class=\"rnthl rntliteral\">async<\/span> keyword, that can be paused and resumed, allowing asynchronous programming.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>async def blink_led():\n&nbsp; &nbsp; #Code to blink an LED<\/code><\/pre>\n\n\n\n<p>Then, we can use <span class=\"rnthl rntliteral\">loop.create_task()<\/span> to schedule this coroutine as a task to be executed by the event loop.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>loop.create_task(blink_led())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"running-event-loop\">Running the Event Loop<\/h3>\n\n\n\n<p>Once you&#8217;ve created tasks, you start the event loop to execute them. The loop will continually check for scheduled tasks and execute them. The following line initiates the event loop, as we\u2019ve seen previously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>loop = asyncio.get_event_loop()<\/code><\/pre>\n\n\n\n<p>Then, <span class=\"rnthl rntliteral\">loop.run_forever()<\/span> makes it run indefinitely, constantly checking for tasks to execute.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>loop.run_forever()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"async-def\">Asynchronous Functions &#8211; <em>async def<\/em><\/h3>\n\n\n\n<p>An asynchronous function is defined using the <span class=\"rnthl rntliteral\">async def<\/span> syntax. These functions, also known as coroutines, can be paused with the <span class=\"rnthl rntliteral\">await<\/span> keyword, allowing other coroutines to run in the meantime. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>async def blink_led():\n    while True:\n        led.toggle()  # Toggle LED state\n        await asyncio.sleep(1)<\/code><\/pre>\n\n\n\n<p>Here, <span class=\"rnthl rntliteral\">blink_led()<\/span> is an asynchronous function. It toggles an LED and then pauses for 1 second using <span class=\"rnthl rntliteral\">await asyncio.sleep(1)<\/span>. During this pause, other tasks can run.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>await asyncio.sleep(1)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"async-delay\">Asynchronous Delay &#8211; <em>acyncio.sleep()<\/em><\/h3>\n\n\n\n<p><span class=\"rnthl rntliteral\">asyncio.sleep()<\/span> is a coroutine provided by the <span class=\"rnthl rntliteral\">asyncio<\/span> module and it is used to introduce a delay in the execution of a coroutine for a specified duration without blocking the entire event loop. So, to create an asynchronous function, you must replace all your <span class=\"rnthl rntliteral\">time.sleep()<\/span> with <span class=\"rnthl rntliteral\">asyncio.sleep()<\/span>.<\/p>\n\n\n\n<p>When <span class=\"rnthl rntliteral\">asyncio.sleep()<\/span> is called within a coroutine, it temporarily suspends the execution of that coroutine, allowing other coroutines to run in the meantime.<\/p>\n\n\n\n<p>The event loop continues to run while the coroutine is paused, checking for other tasks and events.<\/p>\n\n\n\n<p>After the specified duration (seconds), the paused coroutine resumes execution from the point where <span class=\"rnthl rntliteral\">asyncio.sleep()<\/span> was called.<\/p>\n\n\n\n<p><span class=\"rnthl rntliteral\">await asyncio.sleep()<\/span> is a non-blocking way to yield control to other coroutines in the event loop without introducing any actual delay. It effectively allows other coroutines to run immediately.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"await\"><em>await<\/em><\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">await<\/span> keyword is used inside coroutines to indicate a point where the coroutine can be temporarily suspended until a specific event is completed.<\/p>\n\n\n\n<p>In the following example, the coroutine pauses for 1 second without blocking the entire event loop.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>await asyncio.sleep(1)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">In Summary\u2026<\/h3>\n\n\n\n<p>When you run an asynchronous program, the event loop runs continuously, executing the scheduled tasks concurrently. Each coroutine gets a turn to run, and the <span class=\"rnthl rntliteral\">await<\/span> statements allow the event loop to switch between tasks, creating the appearance of parallelism.<\/p>\n\n\n\n<p>As a result, you can perform multiple tasks concurrently without having to wait for each one to complete before moving on to the next.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"async-program-example\">Basic Example of an Asynchronous Program<\/h2>\n\n\n\n<p>Now that you know the basic concepts of asynchronous programming and the basic methods of the <span class=\"rnthl rntliteral\">asyncio<\/span> module let\u2019s create a simple example to apply the concepts learned.<\/p>\n\n\n\n<p>We\u2019ll create two different coroutines. Each coroutine will blink a different LED at a different rate.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Green LED: <span class=\"rnthl rntclgreen\">GPIO 20<\/span> &gt;&gt; blinks every two seconds;<\/li>\n\n\n\n<li>Blue LED: <span class=\"rnthl rntclblue\">GPIO 19<\/span> &gt;&gt; blinks every half a second.<\/li>\n<\/ul>\n\n\n\n<p>To visually check the final result,  wire two LEDs to the Raspberry Pi Pico, one to <span class=\"rnthl rntclgreen\">GPIO 20<\/span> and other to <span class=\"rnthl rntclblue\">GPIO 19<\/span>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Blink-Two-LEDs-Different-Rates-Multiple-Tasks.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"683\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Blink-Two-LEDs-Different-Rates-Multiple-Tasks.png?resize=969%2C683&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Blinking Two LEDs Asynchronously - Circuit\" class=\"wp-image-163421\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Blink-Two-LEDs-Different-Rates-Multiple-Tasks.png?w=969&amp;quality=100&amp;strip=all&amp;ssl=1 969w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Blink-Two-LEDs-Different-Rates-Multiple-Tasks.png?resize=300%2C211&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Blink-Two-LEDs-Different-Rates-Multiple-Tasks.png?resize=768%2C541&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 969px) 100vw, 969px\" \/><\/a><\/figure><\/div>\n\n\n<p>Here\u2019s our example code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-raspberry-pi-pico-asynchronous-programming\/\n\nimport asyncio\nfrom machine import Pin\n\ngreen_led_pin = 20\ngreen_led = Pin(green_led_pin, Pin.OUT)\nblue_led_pin = 19\nblue_led = Pin(blue_led_pin, Pin.OUT)\n\n# Define coroutine function\nasync def blink_green_led():\n    while True:\n        green_led.toggle()\n        await asyncio.sleep(2) \n\n# Define coroutine function\nasync def blink_blue_led():\n    while True:\n        blue_led.toggle()\n        await asyncio.sleep(0.5)\n\n# Define the main function to run the event loop\nasync def main():\n    # Create tasks for blinking two LEDs concurrently\n    asyncio.create_task(blink_green_led())\n    asyncio.create_task(blink_blue_led())\n\n# Create and run the event loop\nloop = asyncio.get_event_loop()  \nloop.create_task(main())  # Create a task to run the main function\nloop.run_forever()  # Run the event loop indefinitely\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\/Pico_Asynchronous_Programming.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>Let\u2019s take a quick look at the code.<\/p>\n\n\n\n<p>We create two coroutines. One for each LED:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Define coroutine function\nasync def blink_green_led():\n    while True:\n        green_led.toggle()\n        await asyncio.sleep(2) \n\n# Define coroutine function\nasync def blink_blue_led():\n    while True:\n        blue_led.toggle()\n        await asyncio.sleep(0.5)<\/code><\/pre>\n\n\n\n<p>Inside each coroutine, we toggle the LED state in a loop and await (asynchronous waiting) for a specific interval <span class=\"rnthl rntliteral\">await asyncio.sleep(0.5)<\/span>.<\/p>\n\n\n\n<p>We create another coroutine called <span class=\"rnthl rntliteral\">main()<\/span> that serves as a central point where you can organize and coordinate the execution of those tasks.<\/p>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">main()<\/span> coroutine, we create tasks for both <span class=\"rnthl rntliteral\">blink_green_led()<\/span> and <span class=\"rnthl rntliteral\">blink_blue_led()<\/span> functions to run concurrently.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Define the main function to run the event loop\nasync def main():\n    # Create tasks for blinking two LEDs concurrently\n    asyncio.create_task(blink_green_led())\n    asyncio.create_task(blink_blue_led())<\/code><\/pre>\n\n\n\n<p>Then, we create an event loop.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>loop = asyncio.get_event_loop() &nbsp;<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">loop.create_task(main())<\/span> creates a task to run the <span class=\"rnthl rntliteral\">main()<\/span> coroutine function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>loop.create_task(main()) &nbsp;# Create a task to run the main function<\/code><\/pre>\n\n\n\n<p>Finally, the <span class=\"rnthl rntliteral\">run_forever()<\/span> method will start running the event loop indefinitely. The event loop continually checks for tasks to execute, runs them concurrently, and manages their execution.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>loop.run_forever() &nbsp;# Run the event loop indefinitely<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the Code<\/h2>\n\n\n\n<p>Run the previous code on your Raspberry Pi Pico. The result will be two blinking LEDs at different rates.<\/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\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-4.jpg?strip=info&#038;w=600&#038;ssl=1 600w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-4.jpg?strip=info&#038;w=714&#038;ssl=1 714w\" alt=\"Raspberry Pi Pico Asynchronous Programming - Blink Multiple LEDs - Demonstration\" data-height=\"402\" data-id=\"163425\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=163425#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-4.jpg\" data-width=\"714\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-4.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\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-2.jpg?strip=info&#038;w=600&#038;ssl=1 600w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-2.jpg?strip=info&#038;w=714&#038;ssl=1 714w\" alt=\"Raspberry Pi Pico Asynchronous Programming - Blink Multiple LEDs - Demonstration\" data-height=\"402\" data-id=\"163423\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=163423#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-2.jpg\" data-width=\"714\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-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\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-1.jpg?strip=info&#038;w=600&#038;ssl=1 600w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-1.jpg?strip=info&#038;w=714&#038;ssl=1 714w\" alt=\"Raspberry Pi Pico Asynchronous Programming - Blink Multiple LEDs - Demonstration\" data-height=\"402\" data-id=\"163422\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=163422#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-1.jpg\" data-width=\"714\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-1.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\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-3.jpg?strip=info&#038;w=600&#038;ssl=1 600w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-3.jpg?strip=info&#038;w=714&#038;ssl=1 714w\" alt=\"Raspberry Pi Pico Asynchronous Programming - Blink Multiple LEDs - Demonstration\" data-height=\"402\" data-id=\"163424\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=163424#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-3.jpg\" data-width=\"714\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/RPi-Pico-Asynchronous-Blinking-Multiple-LEDs-3.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<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This is just a simple example to show you how to write an asynchronous program. Now, you should understand that instead of blinking an LED, you can do more complex tasks like reading a file, requesting data from the internet, handle a web server with multiple clients, and more. We have an example of an asynchronous web server that you can check on the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-asynchronous-web-server-micropython\/\">Raspberry Pi Pico W: Asynchronous Web Server (MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<p>We hope you find this guide useful. To learn more about the Raspberry Pi Pico, make sure to take a look at our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\" title=\"\">All our Raspberry Pi Pico Tutorials and Guides<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico with MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we\u2019ll take a look at the basics of MicroPython asynchronous programming with the Raspberry Pi Pico using the asyncio module. You\u2019ll learn to run multiple tasks concurrently, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico Asynchronous Programming &#8211; Run Multiple Tasks (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-raspberry-pi-pico-asynchronous-programming\/#more-163417\" aria-label=\"Read more about Raspberry Pi Pico Asynchronous Programming &#8211; Run Multiple Tasks (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":163427,"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-163417","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\/2024\/10\/Raspberry-Pi-Pico-Asynchronous-programming.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\/163417","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=163417"}],"version-history":[{"count":7,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163417\/revisions"}],"predecessor-version":[{"id":164162,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163417\/revisions\/164162"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/163427"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=163417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=163417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=163417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}