How to Use Asyncio in MicroPython (Raspberry Pi Pico) | Digi-Key Electronics
DigiKey
@digikeyAbout
DigiKey Corporation is an industry leader in the distribution of quality electronic components, including Electromechanical, Interconnect, Optoelectronics, Passives, Power, RF & Wireless, Semiconductors, Sensors, Thermal Products and Test Tools. Whether small quantities for prototype design or large volumes for production, the company is committed to product availability, service and performance. Search, find, buy from a manufacturer authorized distributor. Same-day shipment, next-day delivery on orders entered by 8:00pm Central Time (USA). Access product information, datasheets, application notes, product photos, catalog, industry links can be found at http://www.digikey.com
Video Description
At this time, MicroPython does not support full multithreading (with the threading library). However, we can use uasyncio (the MicroPython version of the larger Python asyncio library) to create a cooperative multitasking program. Uasyncio contains a subset of functions found in the asyncio library. Some parts (like queues) are left out (at the time this video was released). In the video, we go over how cooperative multitasking compares to preemptive multitasking and how you can use uasyncio to create several tasks in a single program. Specifically, we will use the Raspberry Pi Pico to demonstrate how to read button presses in one loop and blink an LED in another loop. We will also show how to use a queue to pass messages between the tasks. The full code for this tutorial can be found here: https://www.digikey.com/en/maker/projects/getting-started-with-asyncio-in-micropython-raspberry-pi-pico/110b4243a2f544b6af60411a85f0437c Asyncio is based around the idea of “coroutines,” which behave like normal functions but have the ability to yield the processor. While a coroutine is paused, the processor is free to perform other activities. This is extremely useful to optimize processor utilization while waiting for things like a response from a sensor, website, etc. or while performing periodic tasks. We define coroutines using the “async” keyword, which lets the scheduler (a task that runs in the background in charge of telling other tasks to run) know that the containing function is capable of yielding. We can wait for a coroutine to finish executing with the “await” keyword. Finally, we can run a coroutine as a task (e.g. return immediately while letting the coroutine execute) using the create_task() function. Note that coroutines must be run as part of an “event loop” that includes a scheduler. The scheduler, unlike in preemptive multitasking, only runs when a coroutine explicitly yields (e.g. using the “await” keyword). As a result, the programmer must take great care to ensure that none of the coroutines hog the processor and starve the other coroutines/tasks. This method of “cooperative multitasking” makes it more difficult for (seemingly random) bugs to occur (such as race conditions when working with a preemptive scheduler). If you would like to learn more about preemptive multitasking, you can check out my Introduction to RTOS series here: https://www.youtube.com/watch?v=F321087yYy4 Product Links: Raspberry Pi Pico - https://www.digikey.com/en/products/detail/raspberry-pi/SC0915/13624793 Related Videos: Raspberry Pi Pico and RP2040 playlist - https://www.youtube.com/watch?v=B5rQSoOmR5w&list=PLEBQazB0HUyQO6rJxKr2umPCgmfAU-cqR Related Project Links: Getting Started with MicroPython (Raspberry Pi Pico) Part 1: Blink - https://www.digikey.com/en/maker/projects/raspberry-pi-pico-and-rp2040-cc-part-1-blink-and-vs-code/7102fb8bca95452e9df6150f39ae8422 Related Articles: What is a Real-Time Operating System (RTOS) - https://www.digikey.com/en/maker/projects/what-is-a-realtime-operating-system-rtos/28d8087f53844decafa5000d89608016 Introduction to MicroPython - https://www.digikey.com/en/maker/blogs/2018/introduction-to-micropython Maker.io - https://www.digikey.com/en/maker Digi-Key’s Blog – TheCircuit https://www.digikey.com/en/blog Connect with Digi-Key on Facebook https://www.facebook.com/digikey.electronics/ And follow us on Twitter https://twitter.com/digikey
Pico Programming Essentials
AI-recommended products based on this video

10.1 Inch Touch Portable Monitor IPS Screen 1366x768P 60Hz 400 Brightness 99% sRGB HDMI USB-C Monitors Switch for Xbox PS3/4/5 Laptop Compatible with Raspberry Pi, Mini Touch Screen

ELECROW 8 Inch Portable Monitor, 1280x800 Mini HD Display with Built-in Speakers, USB Powered, Non-Touch LCD Screen for Raspberry Pi, PC, Laptop, Jetson Nano, Game Consoles

7 Inch Portable Monitor Touchscreen HD 1024x600 LED Display Dual HDMI Port Small Monitor for PC Raspberry Pi Laptop Computer Xbox PS4/5 Switch Built-in Speakers

BrosTrend 1800Mbps WiFi 6 Linux WiFi Adapter for PC and Raspberry Pi 2+, Long Range USB WiFi Dongle Linux for Ubuntu, Mint, Debian, Kubuntu, Lubuntu, Zorin, Windows 11/10, Dual Band Wireless Antenna

Microcontroller Board for Pico RP2040, Dual Core ARM Cortex M0+Processor Flexible Microcontroller Module (Transparent White)

Microcontroller Board for Pico RP2040, Dual Core ARM Cortex M0+Processor Flexible Microcontroller Module (Black)

RasPi Pico Boot, ARM Cortex M0+ Cores, 256kb RAM, 30 GPIO Pins, 16MB QSPI Flash, 512GB SD2SP2 Pro Micro Storage, for Game Console, with Swiss Adapter

Magnetic Nasal Strips Starter Kit: Comfortable Nasal Breathing Support for Sleep, Helps Reduce Snoring Noise, Includes 60 Tabs (30 Uses) with 4 Sizes

Environet Hydroponic Growing Kit, Self-Watering Mason Jar Herb Garden Starter Kit Indoor, Windowsill Herb Garden, Grow Your Own Herbs from Organic Seeds (Basil)

Herb Garden Planter Indoor Kit 21Pcs Kitchen Herb Garden Starter Kit Growing Kit Including Wooden Box Burlap Pots Soil Discs Gardening Tools Unique Easter Birthday Christmas Gift Ideas for Women Mom

Bonsai Starter Kit – 1x Bonsai Tree | Complete Indoor Starter Kit for Growing Plants with Bonsai Seeds, Tools & Planters – Gardening Gifts for Women & Men

Cmokifuly GMK67 65% Mechanical Keyboard Kit Tri-Mode South-Facing RGB LED for 3/5pin Switches,66 Keys+1 Knob Hotswap Socket PCB Gasket Mounted Plate Gaming DIY Keyboard Kit (White)
![GEEKERA Charging Dock for Meta Quest 3/Oculus Quest 3, VR Headset & Controllers Charging Stand with RGB LED Light [On/Off], VR Accessories, Fast Charging Station](https://m.media-amazon.com/images/I/61H7KCfGg-L._AC_UL960_FMwebp_QL65_.jpg)
GEEKERA Charging Dock for Meta Quest 3/Oculus Quest 3, VR Headset & Controllers Charging Stand with RGB LED Light [On/Off], VR Accessories, Fast Charging Station

Turtle Beach Stealth Ultra High-Performance Wireless Gaming Controller Licensed for Xbox Series X|S, Xbox One, Windows PC & Android – LED Dashboard, Charge Dock, RGB Lighting, 30-Hr Battery, Bluetooth

Ksipze led Lights Strip for Bedroom 100FT,Smart Music Sync RGB Color Changing with App and Remote Control, Power Supply Led Strip Lights Lumiere LED for Room Home Party Decoration

2 Pack Controller Battery Pack Rechargeable for Xbox One/Xbox Series X|S with 4 Battery Cover Xbox Play and Charge Kit with Micro USB Charging Cable for Xbox One S/X/Elite Controller

Portable Monitor, 18in 2.5K 2560x1600P IPS 144Hz Laptop Monitor, HDMI, Type C, Micro USB, 3.5mm Aux Interface, Portable Display Screen Extender for Laptop PC

2 Pack Rechargeable Controller Battery Pack for Xbox Series X|S and Xbox One with 10FT USB C Charging Cable and Micro USB Adapter 6amLifestyle Battery Pack Xbox Play and Charge Kit

Multi Charging Cable, Multi Charger Cable 3 in 1 Charger Fast Charge Multiple Ends 4FT Braided Universal Charging Cord with USB C Micro Lightning Cord for iPhone iPad Samsung Android Phone Tablet Global Recycled Standard

Digital Multimeter Voltmeter Battery Voltage Multi Tester AC DC Volt OHM Amp Current Meter Circuit Continuity Resistance Diode Electrical Tester with Test Leads Backlight LCD Display

Thermal Brush,10 Temperatures LCD Display Upgrade to 400°F 1.5 in Heated Round Brush Curling Iron Heated Ceramic Tourmaline Ionic Comb 110-240V Volum

Thermal Brush,10 Temperatures LCD Display Upgrade to 400°F 1.5 in Heated Round Brush Curling Iron Heated Ceramic Tourmaline Ionic Comb 110-240V Volum

4K HD Digital Camera, 18X Zoom Selfie Vlogging Camera Autofocus with 2.8 Inch LCD Display, Portable 64MP Compact Video Camcorder Anti Shake for Travel (Black)

MSI MAG A550BN, Non-Modular Compact 550W Power Supply, 80+ Bronze, Low-Noise Fan, Active PFC Design, 5 Year Warranty

MSI MAG A850GL PCIE5 White, Fully Modular Compact Gaming 850W Power Supply, 80+ Gold, ATX 3.1 & PCIe 5.1 Ready, Native Dual-Color 12V-2x6 Cable, 10 Year Warranty

MSI MPG A1000GS PCIE5, Fully Modular Gaming 1000W Power Supply, 80+ Gold, Dual 12V-2x6 Cables, Server-Grade, ATX 3.1 & PCIe 5.1, Low-Noise, Semi Digital, Embossed Jacket Cables, 10 Year Warranty

MSI MPG A850GS PCIE5, Fully Modular Gaming 850W Power Supply, 80+ Gold, Dual 12V-2x6 Cables, Server-Grade, ATX 3.1 & PCIe 5.1 Ready, Low-Noise, Semi Digital, Embossed Jacket Cables, 10 Year Warranty

10.1 Inch Touch Portable Monitor IPS Screen 1366x768P 60Hz 400 Brightness 99% sRGB HDMI USB-C Monitors Switch for Xbox PS3/4/5 Laptop Compatible with Raspberry Pi, Mini Touch Screen

ELECROW 8 Inch Portable Monitor, 1280x800 Mini HD Display with Built-in Speakers, USB Powered, Non-Touch LCD Screen for Raspberry Pi, PC, Laptop, Jetson Nano, Game Consoles

7 Inch Portable Monitor Touchscreen HD 1024x600 LED Display Dual HDMI Port Small Monitor for PC Raspberry Pi Laptop Computer Xbox PS4/5 Switch Built-in Speakers














