Lessons and the learning game

Testing firmware

Test a thermostat the way firmware teams do: pure logic, fakes, tests on the board, a second board as the test rig, and PASS or FAIL on serial.

The bench

Most lessons open one Uno with a DS18B20 temperature sensor on pin 4 (its own pull-up switched on) and a relay on pin 7 as the heater. The relay's COM is on ground and its NO contact goes back to pin 8, so a sketch can check that the contact really moved. The hardware-in-the-loop lesson adds a second Uno, the test rig, which sends setpoints over a serial link (rig pin 11 to the thermostat's pin 10) and reads the relay contact on its own pin 8.

Take the course at Testing firmware on /learn.

What each sketch does

  • fwtpure tests the thermostat's decision function at boot (five tests), then uses it to run the heater.
  • fwtfakes puts the sensor, relay and clock behind a small interface, and tests the controller's 60-second rest time with fakes and a fake clock.
  • fwttarget tests the real parts: the DS18B20's presence, family code, conversion time and reading, and the relay contact closing and opening.
  • fwthildut is the thermostat as it ships, taking SET <degrees> over a serial link. fwthilrig is the test rig that drives it and times the relay.
  • fwtregress has one change that breaks a test written weeks earlier: the hysteresis band set to 0.
  • fwtreport reports the same tests for people or in TAP, the Test Anything Protocol, with a to-do test for frost protection.
  • fwtboss is the shipping thermostat: a power-on self-test of its logic, its sensor and its relay contact, a heater locked off if anything fails, and a 60-second rest time.

Why the tests mean something here

The DS18B20 converts in the datasheet's time for its resolution and goes silent without its pull-up, the relay's contact takes about 10 ms to move, and the two Unos run the firmware a real board would. So a test that passes here passed against the same behavior, and a wiring fault fails the same tests.

Honest limits

  • The DS18B20 reads its temperature setting exactly, with no noise and no +/-0.5 C error. The noisy readings in the fakes lesson are scripted.
  • The tests run on the Uno itself. On a real project the logic tests would also run on a PC on every save, and the on-target and HIL runs would be driven by a CI server; the course shows the pieces, not the server.
  • The test rig controls the setpoint but not the room. A production rig would also control the sensor, and the HIL lesson shows why.

Software design and testing for the same ideas on the desktop, and Firmware architecture for designing code with seams.