Lesson plan

Software that must not fail lesson plan

Write firmware for a heater the way safety engineers do: failure modes, defensive input, the real watchdog, asserts that stay, redundancy and reviews, all on an Uno.

4 chapters, 7 lessons, about 5 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

  • What can break, how bad it is, and code that refuses to believe bad input.
  • The chip's watchdog for hangs, and asserts that fail safe in the field.
  • Two sensors, a setpoint stored twice, and a review that finds three bugs.
  • One controller with every layer, attacked until it gives.

Materials for the whole unit

  • Arduino Uno R3
  • Filament lamp x 2
  • Full-size breadboard
  • LED x 3
  • Pushbutton
  • Resistor x 4
  • Signal generator
  • Solid-state relay, SSR-25DA / G3MB-202P
  • Temperature sensor, DS18B20 x 2
  • Thermistor, 10k NTC

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

Think in failures

What can break, how bad it is, and code that refuses to believe bad input.

LessonTimeWhat students do
Failure modesA naive thermostat that trusts every reading, and an FMEA table that ranks what can go wrong by severity, occurrence and detection. Break the sensor wire and watch it heat a tank it thinks is colder than space.45 min
  1. Run it: a heater that works
  2. A hot tank
  3. Break the wire
  4. Rank what can go wrong
  5. Lower the RPN: detect it
  6. Add your own row
  7. Name the safe state
Defensive codeThe heater takes its setpoint over serial and trusts none of it: a line buffer that cannot overflow, strtol with an end pointer, a range check before the cast, and a clear REJECTED for everything else.45 min
  1. Run it: orders over serial
  2. A good order
  3. Out of range
  4. Not a number
  5. Too long to fit
  6. Close the gap
  7. Predict, then run: the wrap

Materials

  • Arduino Uno R3
  • Filament lamp x 2
  • Full-size breadboard
  • LED x 3
  • Pushbutton
  • Resistor x 4
  • Signal generator
  • Solid-state relay, SSR-25DA / G3MB-202P
  • Thermistor, 10k NTC

Check for understanding

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

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Predict, then run: the wrap".

Chapter 2. About 1 h 30 min

Catch it while it runs

The chip's watchdog for hangs, and asserts that fail safe in the field.

LessonTimeWhat students do
WatchdogsThe ATmega328P's real watchdog at 250 ms: a hang it catches, a slow job that trips it, the reset cause in MCUSR, and a bad setting that hangs every boot until a counter in EEPROM drops it into safe mode.45 min
  1. Run it: a dog on a 250 ms leash
  2. Hang it
  3. A slow job trips it too
  4. Feed it from the work
  5. A boot loop
  6. A person clears it
  7. Quick check: where to feed it
Asserts in productionA SAFE_ASSERT macro that never compiles away: on a broken invariant it cuts the heater, records the line number in EEPROM, resets through the watchdog and stays latched until a person presses RESET.45 min
  1. Run it: time-proportioning
  2. Warm tank, small duty
  3. A cold morning
  4. The note survives the reset
  5. A person presses RESET
  6. Fix the bug, keep the assert
  7. Assert, handle, or validate?

Materials

  • Arduino Uno R3
  • Filament lamp x 2
  • Full-size breadboard
  • LED x 2
  • Pushbutton
  • Resistor x 3
  • Signal generator
  • Solid-state relay, SSR-25DA / G3MB-202P
  • Thermistor, 10k NTC

Check for understanding

Each student finishes "Asserts in production" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Assert, handle, or validate?".

Chapter 3. About 1 h 20 min

Trust nothing once

Two sensors, a setpoint stored twice, and a review that finds three bugs.

LessonTimeWhat students do
RedundancyTwo DS18B20s watch one tank and must agree within 2 C, the hotter one wins, and the setpoint is stored twice, once inverted, so a flipped bit in RAM is caught before it is used.45 min
  1. Run it: two probes disagree
  2. Make them agree
  3. The safe guess
  4. A flipped bit
  5. Tighten the limit
  6. Predict, then run: a hot tank
  7. When both fail together
ReviewsA heater controller with three planted bugs, found the way a reviewer finds them: a stray semicolon that trips at room temperature, a 16-bit int that makes the dry-fire cutout impossible, and a wait loop with no way out.35 min
  1. Run it: something is wrong
  2. Bug 1: a semicolon
  3. Bug 2: watch the clock
  4. Fix the int
  5. Bug 3: a loop with no exit
  6. Your review checklist

Materials

  • Arduino Uno R3
  • Filament lamp x 2
  • Full-size breadboard
  • LED x 3
  • Pushbutton
  • Resistor x 3
  • Signal generator
  • Solid-state relay, SSR-25DA / G3MB-202P
  • Temperature sensor, DS18B20 x 2
  • Thermistor, 10k NTC

Check for understanding

Each student finishes "Reviews" 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 review checklist".

Chapter 4. About 50 min

Boss

One controller with every layer, attacked until it gives.

LessonTimeWhat students do
Boss: a fail-safe heaterEvery layer in one controller: a sensor plausibility check, a latched over-temperature trip only a person can reset, a guarded setpoint, SAFE_ASSERT and the real watchdog. Attack it, then find the one failure mode it still misses.50 min
  1. Run it: the whole controller
  2. Normal service
  3. Attack 1: overheat
  4. Only a person resets it
  5. Attack 2: cut the wire
  6. Attack 3: hang it
  7. Attack 4: the one it misses
  8. Sign it off

Materials

  • Arduino Uno R3
  • Filament lamp x 2
  • Full-size breadboard
  • LED x 3
  • Pushbutton
  • Resistor x 4
  • Signal generator
  • Solid-state relay, SSR-25DA / G3MB-202P
  • Thermistor, 10k NTC

Check for understanding

Each student finishes "Boss: a fail-safe heater" on their own, without opening the hints. The simulator checks 8 of its 8 steps as they go.

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

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.