Fareed Mohmed← Project library

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.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Greenhouse climate control: 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
Vent fanRelay module
Heater lampRelay 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)
7ColouredVent fan - INPin 11 (GPIO17)
8BlackVent fan - GNDPin 9 (GND)
9RedVent fan - VCCPin 2 (5V)
10ColouredHeater lamp - INPin 13 (GPIO27)
11BlackHeater lamp - GNDPin 14 (GND)
12RedHeater lamp - VCCPin 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

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.

#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
2Robu.in Amazon.in ThinkRobotics
5230 V LED bulb (9 W) with holder and wire1Robu.in Amazon.in ThinkRobotics
6230 V AC small fan1Robu.in Amazon.in ThinkRobotics
7Closed ABS enclosure (IP54) for the mains side1Robu.in Amazon.in ThinkRobotics
83-way screw terminal block (rated 10 A)2Robu.in Amazon.in ThinkRobotics
9MCB 6 A single pole and RCCB 30 mA (by an electrician)
Mains protection
1Robu.in Amazon.in ThinkRobotics
10MCP3008 8-channel 10-bit ADC (DIP-16)
One chip can read 8 sensors
1Robu.in Amazon.in ThinkRobotics
11TMP36 temperature sensor1Robu.in Amazon.in ThinkRobotics
12Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
13Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 40. Button code lockAll projects42. Solar panel tracker with two LDRs →

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.