Automation and Control
Making a circuit sense something and decide what to do, all on its own.
The question
How does a furnace know when to turn on, and why doesn’t it click on and off every few seconds?
What you’ll discover: how automatic control works, what a threshold is, why hysteresis stops flickering, and how to design control systems that fail safely.
In simple terms
BeginnerSense, decide, act
Every automatic system follows the same three steps, over and over:
- Sense: read a sensor (temperature, light, moisture).
- Decide: compare the reading with a rule, such as “colder than 0 °C (32°F)?”
- Act: switch something: a light, a buzzer, a fan.
You’ve already built one: the Automatic Night Light senses darkness and switches on an LED, using a transistor instead of a program. A thermostat, a car’s automatic headlights and a garage-door safety sensor all work this way.
Hysteresis: why a furnace doesn’t flicker
Suppose a heater turns on below 20 °C (68°F) and off above 20 °C (68°F). When the room is right at 20 °C (68°F), tiny wobbles in the reading would switch it on and off every few seconds, which is noisy and wears it out.
The fix is hysteresis: use two different thresholds. Turn the heater on at 19.5 °C (67°F), but off only when it reaches 20.5 °C (69°F). In between, it keeps doing whatever it was already doing. The gap between thresholds is called the dead band.
Go deeper
IntermediateProgram it: a frost alarm
This program uses the DS18B20 sensor wiring from the Digital Thermometer build, plus an LED and 220 Ω resistor on GP15. Wire GP15 (physical pin 20, the last pin on the left side) through a 220 Ω resistor to the LED’s long leg, and the LED’s short leg to the − rail. The LED comes on when the temperature falls to 0 °C (32°F), and stays on until it’s back up to 1 °C (34°F). If the sensor goes missing or a reading fails, the LED also comes on, so a fault is never silent. To test it, change the numbers to ALARM_ON = 2.0 and ALARM_OFF = 3.0, put the probe in your ice bath (Lesson 29), then warm it in your hand. Change them back afterwards.
from machine import Pin
import onewire, ds18x20, time
sensor = ds18x20.DS18X20(onewire.OneWire(Pin(22)))
roms = sensor.scan()
led = Pin(15, Pin.OUT) # alarm LED with a 220 ohm resistor
ALARM_ON = 0.0 # turn the alarm on at or below 0 C
ALARM_OFF = 1.0 # turn it off again only at 1 C or warmer
alarm = False
while True:
if not roms: # no sensor: fail safe
led.on()
print("FAULT: no sensor found")
time.sleep(5)
roms = sensor.scan() # keep looking
continue
try:
sensor.convert_temp()
time.sleep_ms(750) # the sensor needs up to 750 ms
t = sensor.read_temp(roms[0])
except Exception:
led.on() # failed reading: fail safe
print("FAULT: reading failed")
time.sleep(5)
continue
if not alarm and t <= ALARM_ON:
alarm = True
elif alarm and t >= ALARM_OFF:
alarm = False
led.value(1 if alarm else 0)
print(round(t, 1), "ALARM" if alarm else "")
time.sleep(5)
To sound a buzzer as well, drive it through a transistor (Lesson 10), not straight from a pin.
AdvancedDesigning for failure
Good control systems plan for things going wrong. Ask: what happens if the sensor wire breaks, the reading is garbage, or the program crashes? That’s why this frost alarm lights its LED, rather than going quiet, if no sensor is found or a reading fails. This idea is called fail-safe design.
Real automation also has limits and overrides: a maximum on-time for a pump, a manual off switch, and a safe state when power returns. And whatever your program controls, it must stay low-voltage. Microcontroller projects in Electronics School never switch household power: relays and outlets on the mains need certified equipment and qualified people.
Quick check
What are the three steps of an automatic control system?
Sense, decide, act.
What problem does hysteresis solve?
It stops a system from switching on and off rapidly when a reading sits right at the threshold.
What does fail-safe design mean?
Planning so that if something goes wrong, the system ends up in a safe state.
Words to know
- Threshold
- The value at which a system switches.
- Hysteresis
- Using separate on and off thresholds to prevent rapid switching.
- Fail-safe
- Designed to end up safe when something fails.
- Thermostat
- A device that controls heating or cooling from a temperature reading.
Electronics School is part of the Discovery Zone. Every activity here runs on small batteries or a USB cable: never wall outlets. The one plug-in tool is the optional soldering iron, set up by an adult.