Lesson plan

Scheduling and RTOS lesson plan

From a scheduler written by hand to FreeRTOS on the Uno: tasks and priorities, time slicing on the logic analyzer, queues, mutexes and priority inversion, starvation and the watchdog, and a four-task device.

4 chapters, 7 lessons, about 6 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Many jobs in one loop, and where that stops working.
  • Tasks, priorities, and a tick that takes the processor back.
  • Queues, mutexes, priority inversion and starvation.
  • A temperature alarm built from four tasks.

Materials for the whole unit

  • Arduino Uno R3
  • Character LCD with I2C backpack
  • Full-size breadboard
  • LED x 3
  • Logic analyzer, four channels
  • Potentiometer
  • Pushbutton
  • Resistor x 3

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 55 min

Scheduling by hand

Many jobs in one loop, and where that stops working.

LessonTimeWhat students do
A scheduler by handThree jobs at three rates in one loop with millis() and no delay(): a table of jobs, a loop that runs whichever is due, and the one rule that makes it work.55 min
  1. Run it: three jobs, one loop
  2. A job is a function and a clock
  3. Two rates at once
  4. Press the button
  5. Speed up the yellow job
  6. The rule, and where it breaks

Materials

  • Arduino Uno R3
  • Character LCD with I2C backpack
  • Full-size breadboard
  • LED x 3
  • Potentiometer
  • Pushbutton
  • Resistor x 3

Check for understanding

Each student finishes "A scheduler by hand" 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: "The rule, and where it breaks".

Chapter 2. About 1 h 50 min

Tasks and the scheduler

Tasks, priorities, and a tick that takes the processor back.

LessonTimeWhat students do
Tasks and prioritiesThe same lamps as FreeRTOS tasks: each one a loop of its own with a stack and a priority, vTaskDelay() counted in 16 ms ticks, and a report that outranks them.55 min
  1. Run it: three tasks
  2. vTaskDelay is not delay
  3. Priorities
  4. Every task has its own stack
  5. Change a task
  6. What you gave up
Time slicing and preemptionTwo tasks that never yield still share the processor a tick at a time, seen on the logic analyzer; a higher priority task cuts in on time; and one priority change starves a task completely.55 min
  1. Run it: two badly behaved tasks
  2. The tick takes it back
  3. See the slices
  4. Preemption from above
  5. Starve one
  6. The rule for priorities

Materials

  • Arduino Uno R3
  • Character LCD with I2C backpack
  • Full-size breadboard
  • LED x 3
  • Logic analyzer, four channels
  • Potentiometer
  • Pushbutton
  • Resistor x 3

Check for understanding

Each student finishes "Time slicing and preemption" 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: "The rule for priorities".

Chapter 3. About 2 h 25 min

Tasks that work together

Queues, mutexes, priority inversion and starvation.

LessonTimeWhat students do
QueuesA sensor task hands readings to a logger task through a FreeRTOS queue: no shared variable, a receiver that sleeps until data arrives, and what happens when the queue fills up.45 min
  1. Run it: a sensor and a logger
  2. Why not a global?
  3. Turn the knob
  4. Fill it up
  5. Choosing what a full queue does
Mutexes and priority inversionThree tasks and one shared bus: a mutex guards it, priority inversion makes the most important task wait for the least, and priority inheritance fixes it. Then break the fix and watch it happen.55 min
  1. Run it: one bus, three tasks
  2. What a mutex is
  3. The trap
  4. Priority inheritance
  5. Break it
  6. Locks without regrets
Starvation and the watchdogA busy worker starves a heartbeat task, a watchdog task at the top catches it in the act, and splitting the work into chunks lets every task check in on time.45 min
  1. Run it: a heartbeat and a referee
  2. The heartbeat
  3. Press the button
  4. Share the processor
  5. The hardware watchdog

Materials

  • Arduino Uno R3
  • Character LCD with I2C backpack
  • Full-size breadboard
  • LED x 3
  • Potentiometer
  • Pushbutton
  • Resistor x 3

Check for understanding

Each student finishes "Starvation and the watchdog" 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: "The hardware watchdog".

Chapter 4. About 55 min

Boss project

A temperature alarm built from four tasks.

LessonTimeWhat students do
Boss: a multi-task deviceA temperature alarm built from four tasks: a sensor overwrites a one-slot queue, a display owns the LCD, a button notifies the alarm, and a mutex keeps every serial line whole.55 min
  1. Run it: four tasks, one device
  2. The display has one owner
  3. Make it hot
  4. It blinks
  5. Acknowledge it
  6. Set your own limit

Materials

  • Arduino Uno R3
  • Character LCD with I2C backpack
  • Full-size breadboard
  • LED x 3
  • Potentiometer
  • Pushbutton
  • Resistor x 3

Check for understanding

Each student finishes "Boss: a multi-task device" 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: "Set your own limit".

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.