Skip to content

Project 2.6.10: Smart Greenhouse Control Panel

Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython


Overview

This project builds a small greenhouse control panel that reacts to heat, darkness, and dry soil.

Students will integrate three different inputs and three output channels, then tune thresholds and cooldown values so the system does not switch too aggressively.

The final system should switch a fan relay, a grow-light relay, and a pump relay while reporting the greenhouse state through the Thonny Shell.

Required Components

BME280 sensor module
BME280 sensor module
Breadboard and jumper wires
Breadboard and jumper wires
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
LDR light sensor module with analog output
LDR light sensor module with analog output
Soil moisture sensor (analog output)
Soil moisture sensor (analog output)
1-channel relay module
1-channel relay module

Circuit Connections

Component terminal Connect to Pico physical pin Notes
LDR module VCC Physical pin 36 Physical pin 36 Sensor power
LDR module GND Any GND pin Any GND pin Common ground
LDR module AOUT GPIO 26 / physical pin 31 GPIO 26 / physical pin 31 Analog light reading
Soil sensor AOUT GPIO 27 / physical pin 32 GPIO 27 / physical pin 32 Analog soil moisture reading
Soil sensor VCC and GND Physical pin 36 and any GND Physical pin 36 and any GND Power connection
1-channel relay module GPIO 15 / physical pin 20 GPIO 15 / physical pin 20 Verify active-low or active-high logic
1-channel relay module GPIO 16 / physical pin 21 GPIO 16 / physical pin 21 Verify active-low or active-high logic
1-channel relay module GPIO 17 / physical pin 22 GPIO 17 / physical pin 22 Verify active-low or active-high logic
BME280 VCC 3.3V Physical pin 36 Sensor power
BME280 GND GND Any GND pin Common ground
BME280 SDA GPIO 2 GPIO 2 / physical pin 4 I2C data
BME280 SCL GPIO 3 GPIO 3 / physical pin 5 I2C clock

Wiring Diagram

Circuit Diagram

  Raspberry Pi Pico 2 W
  ┌─────────────────────┐
  │                     │
  │  GPIO 2  ───────────┤──── BME280 SDA
  │  GPIO 3  ───────────┤──── BME280 SCL
  │                     │
  │  GPIO 26 ───────────┤──── LDR AOUT
  │  GPIO 27 ───────────┤──── Soil Sensor AOUT
  │                     │
  │  GPIO 15 ───────────┤──── Fan Relay IN
  │  GPIO 16 ───────────┤──── Light Relay IN
  │  GPIO 17 ───────────┤──── Pump Relay IN
  │                     │
  │  3.3V    ───────────┤──── BME280 VCC
  │  3.3V    ───────────┤──── LDR VCC
  │  3.3V    ───────────┤──── Soil Sensor VCC
  │                     │
  │  GND     ───────────┤──── BME280 GND
  │  GND     ───────────┤──── LDR GND
  │  GND     ───────────┤──── Soil Sensor GND
  │  GND     ───────────┤──── Relay GND (shared)
  │                     │
  └─────────────────────┘

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2W

Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.

Step 2: Place the BME280 Sensor Module

Place the BME280 module where it can sense greenhouse air. Connect BME280 VCC to 3.3V. Connect BME280 GND to GND. Connect BME280 SDA to GPIO 2 and BME280 SCL to GPIO 3. If you are using a bare BME280 sensor instead of a module, use the module pins labeled VCC, GND, SDA, and SCL to 3.3V.

Step 3: Place and Connect the LDR Module

Place the LDR module where it can sense the greenhouse light level. Identify VCC, GND, and AOUT before wiring. Connect LDR module VCC to 3.3V. Connect LDR module GND to GND. Connect LDR module AOUT to GPIO 26.

Step 4: Position and Connect the Soil Sensor

Place the soil moisture probe so the sensing end can enter the soil sample. Connect soil sensor VCC to 3.3V. Connect soil sensor GND to GND. Connect soil sensor AOUT, AO, or Signal to GPIO 27.

Step 5: Place the Relay Modules

Place the fan, grow-light, and pump relay modules where their IN, VCC, GND, COM, NO, and NC terminals are easy to identify. Power each relay module from the supply required by its label. Connect relay GND to Pico GND and to the matching external load supply GND where shared grounding is required.

Step 6: Connect the Relay Inputs

Connect the fan relay IN to GPIO 15. Connect the grow-light relay IN to GPIO 16. Connect the pump relay IN to GPIO 17.

Step 7: Wire the External Loads Safely

Keep fan, grow-light, and pump load current on the relay contact side, not on the Pico side. For each low-voltage load, use the relay COM and NO path to switch the external supply positive wire. Connect each load negative lead directly to its external supply negative wire.

Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] BME280 SDA connects to GPIO 2 and SCL connects to GPIO 3
  • [x] LDR module AOUT connects to GPIO 26
  • [x] Soil sensor AOUT / AO / Signal connects to GPIO 27
  • [x] Fan relay IN connects to GPIO 15
  • [x] Grow-light relay IN connects to GPIO 16
  • [x] Pump relay IN connects to GPIO 17
  • [x] Relay modules use external power and shared ground where required
  • [x] External loads are switched through relay COM and NO paths
  • [x] No load current is routed through the Pico
  • [x] No loose jumper wires

Intermediate Note Calibrate the light and soil values before final use. The BME280 needs a short delay between readings, and the pump has a cooldown so it does not cycle too often.

Safety Note Use external supplies for fan, grow-light, and pump loads. Keep electronics away from water, keep fan blades clear, and never let a pump run dry.


Testing Individual Components

Before running the full project, test each part separately. This makes it easier to find wiring, library, or code problems.

Hardware Setup

  • Build the sensor wiring first, then connect the relay inputs with the high-power loads still disconnected
  • Label the fan, light, and pump relay channels so you do not confuse them later

Test the Input Sensor

  • Print BME280 temperature and humidity values to confirm the digital sensor is stable
  • Record LDR raw values in bright and dark conditions, then record soil raw values in dry and wet conditions for calibration

Test the Output Device

  • Force each relay output on for a short moment and confirm the correct relay channel responds
  • Bench test the pump relay only with the real pump disconnected or with a safe low-power load first

Test Communication

  • Watch the Thonny Shell and confirm the program reports temperature, humidity, darkness, soil moisture, and each relay state
  • Check that the reported relay states match the physical relay click or indicator LED on the module

Run the Full System

  • Simulate heat, darkness, and dry soil one condition at a time so you can see which output changes
  • Confirm the pump cooldown prevents repeated rapid irrigation cycles

Save the Project

  • Save the final code and write down the light and soil calibration values used in your greenhouse test
  • Record the fan, light, and pump thresholds so another student can rebuild the system accurately

Additional Testing and Calibration Checks

  • Dry/wet reading test: record soil sensor values in dry and wet conditions before finalizing SOIL_DRY and SOIL_WET
  • Bright/dark test: record LDR raw readings in bright and dark conditions before finalizing LIGHT_BRIGHT_RAW and LIGHT_DARK_RAW
  • Threshold condition test: simulate heat, darkness, and dry soil separately so you can confirm each output channel responds correctly
  • Relay output test: confirm the correct relay clicks for fan, light, and pump functions
  • Calibration note: if your LDR percentage moves backward, swap LIGHT_BRIGHT_RAW and LIGHT_DARK_RAW based on your module output direction

Full Project Code

After completing and checking the circuit connections, open Thonny IDE. Copy and paste the code below into a new file, or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.

from machine import ADC, I2C, Pin
import bme280
import time

i2c_bme = I2C(1, sda=Pin(2), scl=Pin(3), freq=400000)
bme = bme280.BME280(i2c=i2c_bme)


def number_from_bme(text_value):
    cleaned = "".join(ch for ch in str(text_value) if ch.isdigit() or ch in ".-")
    return float(cleaned)


def read_bme280():
    temp_text, pressure_text, humidity_text = bme.values
    return number_from_bme(temp_text), number_from_bme(humidity_text), number_from_bme(pressure_text)


light_sensor = ADC(26)
soil_sensor = ADC(27)
fan_relay = Pin(15, Pin.OUT)
light_relay = Pin(16, Pin.OUT)
pump_relay = Pin(17, Pin.OUT)

RELAY_ON = 1
RELAY_OFF = 0
TEMP_FAN_ON = 30
TEMP_FAN_OFF = 28
LIGHT_BRIGHT_RAW = 12000
LIGHT_DARK_RAW = 52000
LIGHT_ON_DARKNESS = 60
LIGHT_OFF_DARKNESS = 45
SOIL_DRY = 52000
SOIL_WET = 22000
SOIL_DRY_THRESHOLD = 40
PUMP_RUN_SECONDS = 5
PUMP_COOLDOWN = 60

fan_relay.value(RELAY_OFF)
light_relay.value(RELAY_OFF)
pump_relay.value(RELAY_OFF)
last_pump_time = time.time() - PUMP_COOLDOWN


def clamp(value, low, high):
    if value < low:
        return low
    if value > high:
        return high
    return value


def percent_from_range(raw, high_raw, low_raw):
    span = high_raw - low_raw
    if span == 0:
        return 0
    percent = int(((raw - low_raw) * 100) / span)
    return clamp(percent, 0, 100)


def soil_percent():
    raw = soil_sensor.read_u16()
    span = SOIL_DRY - SOIL_WET
    if span <= 0:
        return 0
    percent = int(((SOIL_DRY - raw) * 100) / span)
    return clamp(percent, 0, 100)


def darkness_percent():
    raw = light_sensor.read_u16()
    return percent_from_range(raw, LIGHT_DARK_RAW, LIGHT_BRIGHT_RAW)


print('=== Smart Greenhouse Control Panel ===')
print('Calibrate light and soil values before final use.\n')

while True:
    try:
        temp_c, humidity, pressure_hpa = read_bme280()
    except Exception as error:
        print('BME280 read failed:', error)
        temp_c = None
        humidity = None

    dark_percent = darkness_percent()
    soil_moisture = soil_percent()

    if temp_c is not None:
        if temp_c >= TEMP_FAN_ON:
            fan_relay.value(RELAY_ON)
        elif temp_c <= TEMP_FAN_OFF:
            fan_relay.value(RELAY_OFF)

    if dark_percent >= LIGHT_ON_DARKNESS:
        light_relay.value(RELAY_ON)
    elif dark_percent <= LIGHT_OFF_DARKNESS:
        light_relay.value(RELAY_OFF)

    now = time.time()
    pump_state = 'OFF'
    if soil_moisture <= SOIL_DRY_THRESHOLD and (now - last_pump_time) >= PUMP_COOLDOWN:
        pump_relay.value(RELAY_ON)
        pump_state = 'RUNNING'
        print('Pump cycle started because soil moisture is {}%.'.format(soil_moisture))
        time.sleep(PUMP_RUN_SECONDS)
        pump_relay.value(RELAY_OFF)
        last_pump_time = time.time()
        pump_state = 'CYCLE COMPLETE'
    else:
        pump_relay.value(RELAY_OFF)

    print('Temp: {} C | Humidity: {} % | Darkness: {} % | Soil: {} % | Fan: {} | Light: {} | Pump: {}'.format(
        temp_c,
        humidity,
        dark_percent,
        soil_moisture,
        fan_relay.value(),
        light_relay.value(),
        pump_state
    ))

    time.sleep(10)

How the Code Works

Code Section What It Does Why It Matters
Calibration constants Sets threshold values for temperature, light, and soil Thresholds are easier to understand and tune than raw ADC values alone
Fan hysteresis Uses separate ON and OFF temperature values Prevents the fan relay from chattering near one threshold
Light control Turns grow-light relay based on darkness percentage Shows how control systems avoid wasting energy
Pump cooldown logic Prevents a new pump cycle until the cooldown period has elapsed Prevents rapid repeated watering cycles

Expected Result

When temperature rises above the fan threshold, the fan relay should turn on and remain on until the temperature falls below the lower fan-off threshold.

When the LDR reading shows the greenhouse is dark, the light relay should turn on and then switch off again when the area becomes bright enough.

When soil moisture drops below the dry threshold, the pump relay should run one short cycle and then wait for the cooldown period before another cycle.


Troubleshooting

Problem Possible cause Solution
The fan keeps switching rapidly No hysteresis between ON and OFF temperatures Keep the separate ON and OFF temperatures so the relay has hysteresis
The light turns on during bright conditions LDR bright/dark raw values are swapped Recheck the bright and dark raw values and swap them if needed
The pump runs too often Dry threshold too high or cooldown too short Lower the dry threshold or increase PUMP_COOLDOWN after testing
Relay does not respond Wiring mismatch with GPIO pins in code Compare the wiring table with GPIO 15, 16, and 17 in the code