Engineering · Raspberry Pi · Project 41 of 47 · Smart farm and energy · Intermediate
Greenhouse climate control
A temperature sensor keeps the greenhouse between 20 and 30 degrees. Too cold: a heater lamp switches on. Too hot: a fan switches on. In between both rest.

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 |
| Vent fan | Relay module |
| Heater lamp | 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 | Vent fan - IN | Pin 11 (GPIO17) |
| 8 | Black | Vent fan - GND | Pin 9 (GND) |
| 9 | Red | Vent fan - VCC | Pin 2 (5V) |
| 10 | Coloured | Heater lamp - IN | Pin 13 (GPIO27) |
| 11 | Black | Heater lamp - GND | Pin 14 (GND) |
| 12 | Red | Heater lamp - VCC | Pin 4 (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) # relay boards switch on a LOW signal
heater = OutputDevice(27, active_high=False)
COLD, WARM = 20, 30 # the comfortable range
while True:
temp = (sensor.value * 3.3 - 0.5) * 100
if temp < COLD:
heater.on()
fan.off()
state = "COLD: heater ON"
elif temp > WARM:
fan.on()
heater.off()
state = "HOT: fan ON"
else:
fan.off()
heater.off()
state = "OK: both off"
print("%.1f C %s" % (temp, state))
sleep(1)
How the code works
- The TMP36 gives 10 mV for each degree, plus 0.5 V. The formula turns the reading into degrees.
- Two relays switch two 230 V loads: a fan and a heater lamp.
- The heater and the fan never run at the same time: each branch switches the other one off.
You will learn
- Reading a temperature with an ADC
- Controlling two loads with one sensor
- A comfort band with two limits
Try this
- Slide the temperature from 10 to 40 degrees and watch both relays.
- Change the comfort band to 18 to 26.
- Add a red LED that lights if the temperature goes above 38.
On a real Pi
- The mains side of the relays must be wired by an electrician, in a closed box with a 30 mA RCCB.
- A greenhouse is damp. Keep the Pi, the sensor board and all plugs in a sealed, dry box.
- Put the temperature sensor in the shade at plant height, not near the heater lamp.
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 | 2 | Robu.in Amazon.in ThinkRobotics |
| 5 | 230 V LED bulb (9 W) with holder and wire | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | 230 V AC small fan | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | Closed ABS enclosure (IP54) for the mains side | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | 3-way screw terminal block (rated 10 A) | 2 | Robu.in Amazon.in ThinkRobotics |
| 9 | MCB 6 A single pole and RCCB 30 mA (by an electrician) Mains protection | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | MCP3008 8-channel 10-bit ADC (DIP-16) One chip can read 8 sensors | 1 | Robu.in Amazon.in ThinkRobotics |
| 11 | TMP36 temperature sensor | 1 | Robu.in Amazon.in ThinkRobotics |
| 12 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 13 | 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.