Engineering · Raspberry Pi · Project 15 of 47 · Home automation · Intermediate
Thermostat fan with hysteresis
A room fan that turns on above 28 degrees C and off below 26 degrees C. The gap between the two limits stops the relay from chattering.

CAUTION: 230 V single-phase mains. The relay in this project switches a mains load (Live and Neutral, 230 V AC, 50 Hz in India). Mains can kill. Practise with the simulator or a low-voltage lamp. Build the mains side only with a qualified electrician, in a closed insulated box, behind a fuse or MCB and a 30 mA RCCB. Never touch the relay screw terminals while anything is connected.
Parts in this circuit
| Part | Type |
|---|---|
| MCP3008 and TMP36 | MCP3008 + TMP36 |
| Relay and fan | Relay module |
Wiring, pin by pin
Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Orange | MCP3008 and TMP36 - 3.3 V | Pin 1 (3V3) |
| 2 | Black | MCP3008 and TMP36 - GND | Pin 6 (GND) |
| 3 | Coloured | MCP3008 and TMP36 - CLK | Pin 23 (GPIO11) |
| 4 | Coloured | MCP3008 and TMP36 - MISO | Pin 21 (GPIO9) |
| 5 | Coloured | MCP3008 and TMP36 - MOSI | Pin 19 (GPIO10) |
| 6 | Coloured | MCP3008 and TMP36 - CS | Pin 24 (GPIO8) |
| 7 | Coloured | Relay and fan - IN | Pin 11 (GPIO17) |
| 8 | Black | Relay and fan - GND | Pin 9 (GND) |
| 9 | Red | Relay and fan - VCC | Pin 2 (5V) |
Python code (gpiozero)
from gpiozero import MCP3008, OutputDevice
from time import sleep
sensor = MCP3008(channel=0) # TMP36 on channel 0
fan = OutputDevice(17, active_high=False)
ON_ABOVE = 28 # degrees C
OFF_BELOW = 26 # a gap (hysteresis) stops fast on-off switching
while True:
temp_c = (sensor.value * 3.3 - 0.5) * 100
print("Room:", round(temp_c, 1), "C Fan:", "ON" if fan.value else "OFF")
if temp_c > ON_ABOVE and not fan.value:
fan.on()
print("Too warm: fan ON")
elif temp_c < OFF_BELOW and fan.value:
fan.off()
print("Cool enough: fan OFF")
sleep(1)
How the code works
- The same TMP36 and MCP3008 as project 10 give the temperature in degrees C.
- If one limit were used, a reading that wobbles around 28 would switch the fan on and off many times. With two limits, the fan has to cool 2 degrees before it stops.
- The tests "and not fan.value" and "and fan.value" make sure on() and off() run only when the state changes.
You will learn
- Hysteresis (two limits instead of one)
- Reading the analog sensor through the ADC
- Controlling a fan with a relay
Try this
- Move the temperature slider from 24 to 30 and back down slowly. Note where the fan stops.
- Change the limits to 30 and 27.
- Add a red LED that stays on while the fan is running.
On a real Pi
- A 5 V fan can run directly from the 5 V rail through a transistor. For a mains fan use the relay and an electrician.
- Do not place the sensor next to the fan motor or the Pi itself: both heat the sensor.
Parts to buy
The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 1-channel 5 V relay module with optocoupler 10 A 250 V AC contacts | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 230 V AC small fan | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Closed ABS enclosure (IP54) for the mains side | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | 3-way screw terminal block (rated 10 A) | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | MCB 6 A single pole and RCCB 30 mA (by an electrician) Mains protection | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | MCP3008 8-channel 10-bit ADC (DIP-16) One chip can read 8 sensors | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | TMP36 temperature sensor | 1 | Robu.in Amazon.in ThinkRobotics |
| 11 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 12 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.