C12 · Lesson 53 · Project
Sensing, deciding and switching a heater, on the board
A thermostat is the classic embedded control system: read a temperature, compare it with a setpoint, switch a heater, repeat, forever, unattended. Doing it well takes most of this course: an I²C sensor, signed fixed-point readings, hysteresis so the heater doesn't chatter, a steady sampling rate, a status report over the UART, and sleep between readings. This project builds it twice, in MicroPython and in C, against a simulated room that warms when the heater is on and cools toward the outside temperature when it's off.
- The MicroPython thermostat reads the room's sensor every 500 ms and switches the heater on P5 (and its lamp on P0): on below 19.5 °C, off above 20.5 °C. Every 5 s it reports over the UART.
- Run it: the room starts at 16 °C, warms to the band, then cycles inside it. The temp lane shows the sawtooth, and P5 the heater's on and off times.
- Set
BANDto 0 and run again: the heater now switches nearly every reading, chattering, which wears out relays and contactors. Hysteresis trades a slightly wider swing for far fewer switchings.
Predict
The heater warms the room at 0.375 °C/s near 20 °C, and it cools at the same rate when off. With a band from 19.5 to 20.5 °C, about how long is one on-off cycle?
About 5.3 s: crossing the 1 °C band takes s each way. A wider band means longer, fewer cycles and a bigger temperature swing; the sampling interval adds a little overshoot at each end.
Name it
The room
Heat flows in from the heater, P watts when on, and out through the walls, , into a heat capacity C:
Here C = 2000 J/°C, R = 0.02 °C/W, P = 1500 W and = 5 °C, so the room would settle at 35 °C with the heater always on and 5 °C with it off; the time constant is = 40 s. (Real rooms are hundreds of times slower; this one is fast so a run takes seconds.)
On-off control with hysteresis
The band 2b sets the swing and the cycle time; with heating and cooling rates and , a cycle lasts about .
The firmware's jobs
- Sample the sensor at a steady rate (a timer, or a deadline loop).
- Convert the reading, minding the sign.
- Decide with hysteresis; drive the heater and a status light.
- Report now and then.
- Sleep in between, and fail safe: if the sensor stops answering, turn the heater off.
Worked example
With the heater on at 20 °C, how fast does the room warm? And with it off?
Show the solution
- Heater on.
- Heater off.
- Why they match. The setpoint was chosen where they're equal, so the cycle is symmetric.
Answer it warms at 0.375 °C/s with the heater on and cools at 0.375 °C/s with it off.
Worked example
The sensor stops answering (its I²C reads return NACK). What should the thermostat do?
Show the solution
- Act safely. Turn the heater off and report the fault.
- Why off. A heater left on because the controller can't see the temperature is the dangerous failure; a heater off is merely uncomfortable.
- A second line of defense. Real thermostats also have an independent over-temperature cutout, so a firmware bug can't cause a fire either.
Answer turn the heater off and report the fault.
Check
The room (2000 J/°C, R = 0.02 °C/W, outside 5 °C) is at 18 °C with a 1500 W heater on. How fast is it warming?
Check
A room with R = 0.01 °C/W, outside temperature 0 °C, and a 1500 W heater always on: where does it settle?
Check
A thermostat's band is ±0.5 °C; the room warms at 0.2 °C/s with the heater on and cools at 0.375 °C/s with it off. How long is one cycle?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
What's the time constant of a room with R = 0.02 °C/W and C = 2000 J/°C?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
A heater cycles every 5.3 s. About how many times does it switch on in 40 s, once the room is in its band?
Check
The thermostat's sensor stops answering. What's the safest thing for the firmware to do?
Check
Write the thermostat in C: read the sensor every 500 ms, heat below 19.5 °C, stop above 20.5 °C (heater on P5).
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
The thermostat in MicroPython with the setpoint SP (19 °C) and a ±0.5 °C band; sample every 500 ms.
MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.
Explain why
Why does an on-off thermostat need hysteresis?
With a single threshold, the heater turns off the moment the temperature crosses the setpoint, the room cools a hair, and it turns back on at the next reading; near the setpoint the temperature hovers and the heater switches on and off every sample. That chattering wears out relays and contactors, which are rated for a limited number of operations, and can make noise and electrical interference. Two thresholds, on below the band and off above it, mean the room must travel across the whole band between switchings, so the heater cycles slowly, a few times a minute here, at the cost of a swing of about the band's width.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.