Lesson plan

Concurrency: tasks, races and async lesson plan

Concurrency on one core in MicroPython on a Pico: superloops, interrupt races measured for real, critical sections, coroutines, asyncio, queues, and a responsive multi-sensor box.

4 chapters, 7 lessons, about 5 hours, on the Raspberry Pi Pico. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • From a blocking loop to a superloop, then interrupts and the race they bring.
  • Critical sections, the no-memory rule of hard interrupts, and micropython.schedule.
  • Generators as coroutines, a scheduler by hand, asyncio, and queues with backpressure.
  • A multi-sensor box that never stops answering, with its lag measured.

Materials for the whole unit

  • Clock
  • LED
  • Potentiometer
  • Pushbutton
  • Raspberry Pi Pico
  • Resistor

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 20 min

Two things at once

From a blocking loop to a superloop, then interrupts and the race they bring.

LessonTimeWhat students do
Doing two thingsWhy one sleep() ruins a two-job program, and the superloop that fixes it: timestamps, ticks_diff and a debounced button.40 min
  1. Run it
  2. The button that does not listen
  3. The superloop
  4. A third job
  5. Why ticks_diff and ticks_add
Interrupt racesA hard interrupt counts a 1 kHz wheel sensor. Put a little work in the wrong place and ticks vanish: a real race, measured.40 min
  1. An interrupt counts
  2. Spin it faster
  3. A little work in between
  4. Count what went missing
  5. Read, modify, write

Materials

  • Clock
  • LED
  • Potentiometer
  • Pushbutton
  • Raspberry Pi Pico
  • Resistor

Check for understanding

Each student finishes "Interrupt races" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Read, modify, write".

Chapter 2. About 40 min

Sharing safely

Critical sections, the no-memory rule of hard interrupts, and micropython.schedule.

LessonTimeWhat students do
Critical sectionsClose the race for good with disable_irq, measure how long interrupts stay off, then meet the rule hard interrupts live by: no new memory.40 min
  1. The race again
  2. Shrink the window
  3. A critical section
  4. No new memory in a hard IRQ
  5. Schedule the heavy part

Materials

  • Clock
  • LED
  • Potentiometer
  • Pushbutton
  • Raspberry Pi Pico
  • Resistor

Check for understanding

Each student finishes "Critical sections" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Schedule the heavy part".

Chapter 3. About 2 h

Cooperative tasks

Generators as coroutines, a scheduler by hand, asyncio, and queues with backpressure.

LessonTimeWhat students do
CoroutinesGenerators are functions that pause. Build a round-robin scheduler, then one that sleeps: the engine inside every async library.40 min
  1. Functions that pause
  2. A third task
  3. Tasks that sleep
  4. And the heartbeat beats
  5. The hog
asyncio in MicroPythonTasks with async and await, the blocking mistake that freezes them all (measured), and a hard IRQ waking a task through ThreadSafeFlag.40 min
  1. Two tasks, built in
  2. The button task
  3. A third task
  4. The blocking mistake
  5. An interrupt wakes a task
QueuesA ring buffer an interrupt can fill without allocating, what happens when the consumer is too slow, and a queue class with backpressure for tasks.40 min
  1. A ring buffer
  2. One press, many edges
  3. Flood it
  4. Work in batches
  5. A queue for tasks

Materials

  • Clock
  • LED
  • Potentiometer
  • Pushbutton
  • Raspberry Pi Pico
  • Resistor

Check for understanding

Each student finishes "Queues" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "A queue for tasks".

Chapter 4. About 50 min

Boss

A multi-sensor box that never stops answering, with its lag measured.

LessonTimeWhat students do
Boss: a responsive multi-sensor boxEverything at once: an IRQ-counted wheel with a critical section, a smoothed knob in slices, a flag-driven button, a heartbeat, an alarm and a lag watchdog.50 min
  1. Power it up
  2. Overspeed
  3. Turn the knob
  4. Change mode
  5. Measure the lag
  6. Your design review

Materials

  • Clock
  • LED
  • Potentiometer
  • Pushbutton
  • Raspberry Pi Pico
  • Resistor

Check for understanding

Each student finishes "Boss: a responsive multi-sensor box" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Your design review".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.