C1253 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 BAND to 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?

Name it

The room

Heat flows in from the heater, P watts when on, and out through the walls, (T−Tout)/R, into a heat capacity C:

CdTdt=P⋅on−T−ToutR

Here C = 2000 J/°C, R = 0.02 °C/W, P = 1500 W and Tout = 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 RC = 40 s. (Real rooms are hundreds of times slower; this one is fast so a run takes seconds.)

On-off control with hysteresis

heater={onT<Tset−boffT>Tset+bunchangedotherwise

The band 2b sets the swing and the cycle time; with heating and cooling rates rh and rc, a cycle lasts about 2b/rh+2b/rc.

The firmware's jobs

  1. Sample the sensor at a steady rate (a timer, or a deadline loop).
  2. Convert the reading, minding the sign.
  3. Decide with hysteresis; drive the heater and a status light.
  4. Report now and then.
  5. 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
  1. Heater on. rate=1500−(20−5)/0.022000=1500−7502000=0.375°C/s
  2. Heater off. −7502000=−0.375°C/s
  3. 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
  1. Act safely. Turn the heater off and report the fault.
  2. Why off. A heater left on because the controller can't see the temperature is the dangerous failure; a heater off is merely uncomfortable.
  3. 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?

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.