Fareed Mohmed← Project library

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.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Thermostat fan with hysteresis: the board, its power supply and the parts wired to the 40-pin header
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

PartType
MCP3008 and TMP36MCP3008 + TMP36
Relay and fanRelay 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.

#ColourFrom (part and leg)To (header pin)
1OrangeMCP3008 and TMP36 - 3.3 VPin 1 (3V3)
2BlackMCP3008 and TMP36 - GNDPin 6 (GND)
3ColouredMCP3008 and TMP36 - CLKPin 23 (GPIO11)
4ColouredMCP3008 and TMP36 - MISOPin 21 (GPIO9)
5ColouredMCP3008 and TMP36 - MOSIPin 19 (GPIO10)
6ColouredMCP3008 and TMP36 - CSPin 24 (GPIO8)
7ColouredRelay and fan - INPin 11 (GPIO17)
8BlackRelay and fan - GNDPin 9 (GND)
9RedRelay and fan - VCCPin 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

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.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
41-channel 5 V relay module with optocoupler
10 A 250 V AC contacts
1Robu.in Amazon.in ThinkRobotics
5230 V AC small fan1Robu.in Amazon.in ThinkRobotics
6Closed ABS enclosure (IP54) for the mains side1Robu.in Amazon.in ThinkRobotics
73-way screw terminal block (rated 10 A)1Robu.in Amazon.in ThinkRobotics
8MCB 6 A single pole and RCCB 30 mA (by an electrician)
Mains protection
1Robu.in Amazon.in ThinkRobotics
9MCP3008 8-channel 10-bit ADC (DIP-16)
One chip can read 8 sensors
1Robu.in Amazon.in ThinkRobotics
10TMP36 temperature sensor1Robu.in Amazon.in ThinkRobotics
11Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
12Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 14. Automatic porch lightAll projects16. Automatic plant watering →

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.