Lessons and the learning game

Concurrency: tasks, races and async

How one small chip does many things at once, taught on a Raspberry Pi Pico in MicroPython, with every race measured for real.

Start the course. It is an advanced course: you should be comfortable with Python functions, lists and globals.

The box

Every lesson uses one circuit:

  • A Raspberry Pi Pico running the official MicroPython firmware.
  • A button on GP15 and an LED on GP14.
  • A potentiometer on GP26, the first ADC pin.
  • A clock part on GP16 that ticks 1,000 times a second, standing in for a wheel sensor, so interrupts arrive steadily while the program does other work.

Lesson by lesson

  1. Doing two things. Why one sleep() makes a button deaf, and the superloop that fixes it: timestamps, ticks_diff, deadlines that never drift, and a debounced button.
  2. Interrupt races. A hard pin interrupt counts the wheel. Put 1.8 ms of knob smoothing between reading and zeroing the count, and a referee counter shows ticks vanishing, while the speed readings look perfectly fine.
  3. Critical sections. Shrinking the window, and why that only makes the bug rarer. machine.disable_irq() and enable_irq() close it for good. Then the rule hard handlers live by: no new memory, shown by a real MemoryError, and micropython.schedule to move the heavy part out.
  4. Coroutines. Generators as tasks that pause, a round-robin scheduler by hand, then one where tasks yield how long to sleep: the engine inside every event loop.
  5. asyncio in MicroPython. Tasks with async and await, the blocking mistake that freezes them all (measured as a late heartbeat), and a hard interrupt waking a task through ThreadSafeFlag.
  6. Queues. A ring buffer an interrupt can fill without allocating, contact bounce recorded edge by edge, a consumer too slow to keep up, batching, and a queue class with backpressure, since MicroPython's asyncio has no Queue.
  7. Boss: a responsive multi-sensor box.

The finale

The box watches the wheel, the knob and the button at once. A hard interrupt counts ticks and a task collects them in a critical section, the knob is smoothed in slices that never hog the chip, the button wakes its task through a flag, an alarm and a fast heartbeat fire on overspeed, and a watchdog task measures the worst lag. The last step is a design review: for every shared variable, say who writes it, who reads it and why it is safe.

How the Pico engine differs

The Pico boots the real MicroPython v1.29 firmware on a simulated RP2040, so interrupts, the locked heap and asyncio behave as they do on a real board. What is different:

  • It is exact. The simulator is deterministic, so a race loses the same number of ticks every run. On hardware the count wanders, which is what makes races so hard to catch. The lessons say so where it matters.
  • One core. Core 1 is not simulated, so _thread stops the board. The course covers concurrency on one core, interrupts and cooperative tasks, which is what most firmware uses.
  • No PIO or DMA in the lessons. Everything is done by the CPU and its interrupts.
  • The course is Pico only. The same MicroPython code runs on an ESP32 with different pin numbers.

Where next

MicroPython games on a tiny screen is the gentle way into the same board. RTOS and timing takes preemptive multitasking further.