MicroPython async/await on Pico, sensor logger example

MicroPython on ESP32, Pi Pico, and other supported boards.
Post Reply
sam_python
Posts: 5
Joined: Tue May 05, 2026 11:00 am

After yung shock ng REPL discovery (other thread), I went down the MicroPython rabbit hole. asyncio works on Pico, and once you wrap your head around it, much cleaner than blocking sleep() loops.

Quick example I wrote, logs a DHT22 every 30 seconds AND blinks an LED every 500ms, in parallel without blocking:

Code: Select all

import uasyncio as asyncio
from machine import Pin
import dht

led = Pin(25, Pin.OUT)  # onboard LED on Pico
sensor = dht.DHT22(Pin(15))

async def blink():
    while True:
        led.toggle()
        await asyncio.sleep_ms(500)

async def log_sensor():
    while True:
        sensor.measure()
        print(f't={sensor.temperature()}C h={sensor.humidity()}%')
        await asyncio.sleep(30)

async def main():
    asyncio.create_task(blink())
    asyncio.create_task(log_sensor())
    while True:
        await asyncio.sleep(60)  # keep main loop alive

asyncio.run(main())
What blew my mind: yung

Code: Select all

await asyncio.sleep()
yields back control to the event loop. The LED keeps blinking smoothly even while sensor reads (which takes ~250ms in DHT22's case) are happening.

May nakapag-port na ba ng more complex stuff (web server, MQTT) sa MicroPython using asyncio? Curious how it scales.
Post Reply