Skip to content

Project 2.6.9: Smart Greenhouse Alarm

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


Overview

This project builds a greenhouse alarm that reacts when temperature or humidity moves outside safe limits.

Students will use a BME280 sensor, a buzzer, and status LEDs to create a clear environmental alarm system.

The final system should identify unsafe temperature or humidity conditions, show a green OK state when conditions are acceptable, and sound a repeating alarm only when unsafe conditions persist.

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
Buzzer
Buzzer
Red LED and 220 Ω resistor
Red LED and 220 Ω resistor
Green LED and 220 Ω resistor
Green LED and 220 Ω resistor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
BME280 sensor module Requires bme280.py on the Pico
Buzzer positive GPIO 15 GPIO 15 / physical pin 20 Alarm output
Red LED anode GPIO 16 through 220 Ω resistor GPIO 16 / physical pin 21 Alarm LED
Green LED anode GPIO 17 through 220 Ω resistor GPIO 17 / physical pin 22 Normal-state LED
Both LED cathodes and buzzer negative GND Any GND pin Common return
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 15 ───────────┤──── Buzzer (+)
  │                     │
  │  GPIO 16 ────220Ω───┤──── Red LED (+)
  │                     │
  │  GPIO 17 ────220Ω───┤──── Green LED (+)
  │                     │
  │  3.3V    ───────────┤──── BME280 VCC
  │                     │
  │  GND     ───────────┤──── BME280 GND
  │  GND     ───────────┤──── Buzzer (-)
  │  GND     ───────────┤──── Red LED (-)
  │  GND     ───────────┤──── Green LED (-)
  │                     │
  └─────────────────────┘

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 the greenhouse air. Identify VCC, GND, and DATA before wiring.

Step 3: Connect the BME280 Sensor Module

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.

Step 4: Place the Buzzer

Place the buzzer on the breadboard and identify its positive (+) and negative (-) pins. Connect the buzzer positive pin to GPIO 15. Connect the buzzer negative pin to GND.

Step 5: Place the Red LED

Place the red LED on the breadboard. Connect its long leg through a 220Ω resistor to GPIO 16. Connect its short leg to GND.

Step 6: Place the Green LED

Place the green LED on the breadboard. Connect its long leg through a 220Ω resistor to GPIO 17. Connect its short leg to GND.

Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] BME280 VCC connects to 3.3V
  • [x] BME280 GND connects to GND
  • [x] BME280 SDA connects to GPIO 2
  • [x] BME280 SCL connects to GPIO 3
  • [x] Buzzer positive pin connects to GPIO 15
  • [x] Buzzer negative pin connects to GND
  • [x] Red LED long leg connects through a 220Ω resistor to GPIO 16
  • [x] Green LED long leg connects through a 220Ω resistor to GPIO 17
  • [x] Both LED short legs connect to GND
  • [x] No loose jumper wires

Intermediate Note The BME280 needs a short delay between readings. Keep the BME280 away from direct heaters or mist during normal testing so the alarm is based on room conditions.


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 BME280 and LED wiring first, then add the buzzer
  • Keep the SDA and SCL wires short and stable because BME280 sensors can be sensitive to poor connections

Test the Input Sensor

  • Run a short BME280 test and confirm that temperature and humidity values appear
  • Wait at least two seconds between BME280 reads

Test the Output Device

  • Test the buzzer and both LEDs separately
  • Confirm that the green LED can indicate a safe state without the red LED being active at the same time

Run the Full System

  • Upload the final code and temporarily adjust the thresholds to trigger a safe classroom alarm
  • Confirm that the system prints the actual reason for the alarm and that the buzzer repeats only at the chosen interval

Save the Project

  • Save the final code and record the temperature and humidity thresholds used in your test
  • Write down whether the alarm repeated at a comfortable interval for the classroom environment

Additional Testing and Calibration Checks

Calibration and tuning notes

  • Use temporary thresholds for classroom testing instead of forcing the environment into extreme conditions
  • If the BME280 reads erratically, recheck the data wire and power stability before changing the code
  • If the alarm feels too frequent, increase ALARM_REPEAT_S rather than removing the warning completely
  • Test one unsafe condition at a time first, then try combined conditions

Quick testing checklist

  • [ ] BME280 readings appear correctly
  • [ ] Green LED shows the safe state
  • [ ] Unsafe thresholds trigger the alarm logic
  • [ ] The Shell lists the actual alarm reason
  • [ ] The buzzer repeats at the configured interval rather than staying on continuously

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 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)


buzzer = Pin(15, Pin.OUT)
red_led = Pin(16, Pin.OUT)
green_led = Pin(17, Pin.OUT)

TEMP_HIGH = 35
TEMP_LOW = 10
HUMIDITY_HIGH = 85
HUMIDITY_LOW = 30
ALARM_REPEAT_S = 15
last_alarm = time.time() - ALARM_REPEAT_S

buzzer.off()
red_led.off()
green_led.on()


def alarm_burst():
    for _ in range(3):
        buzzer.on()
        red_led.on()
        time.sleep(0.15)
        buzzer.off()
        red_led.off()
        time.sleep(0.15)


def read_environment():
    temp_c, humidity, pressure_hpa = read_bme280()
    return temp_c, humidity


def alert_reasons(temp_c, humidity_pct):
    reasons = []
    if temp_c > TEMP_HIGH:
        reasons.append("temperature too high")
    if temp_c < TEMP_LOW:
        reasons.append("temperature too low")
    if humidity_pct > HUMIDITY_HIGH:
        reasons.append("humidity too high")
    if humidity_pct < HUMIDITY_LOW:
        reasons.append("humidity too low")
    return reasons


print("Smart greenhouse alarm ready")
time.sleep(2)

while True:
    try:
        temp_c, humidity_pct = read_environment()
    except Exception as error:
        print("Sensor error:", error)
        time.sleep(2)
        continue

    reasons = alert_reasons(temp_c, humidity_pct)
    now = time.time()

    if reasons:
        green_led.off()
        print("ALARM:", ", ".join(reasons))
        if now - last_alarm >= ALARM_REPEAT_S:
            alarm_burst()
            last_alarm = now
    else:
        red_led.off()
        buzzer.off()
        green_led.on()
        print("Status OK | Temp:{}C Hum:{}%".format(temp_c, humidity_pct))

    time.sleep(2)

How the Code Works

Code Section 1 Why It Matters
alert_reasons() 1 A good alarm should explain what is wrong
ALARM_REPEAT_S 1 Constant alarm noise is less useful than timed repeats
alarm_burst() 1 Short bursts are safer and easier to recognise
Green versus red LED logic Shows safe state separately from alarm state Status should be obvious even if the buzzer is muted

Expected Result

When temperature and humidity are inside the safe range, the green LED should stay on and the Shell should report an OK state. When any condition becomes unsafe, the system should list the alarm reason, blink the red LED, and sound the buzzer at the configured repeat interval.


Troubleshooting

Problem 1 Solution
The alarm never triggers 1 Temporarily move the thresholds closer to the room conditions and verify the BME280 readings
The red LED stays on The system never leaves the alarm state Check the live sensor values and confirm they have returned inside the safe range
The buzzer is silent 1 Test the buzzer separately and confirm the timing logic
BME280 sensor module 1 Temperature, humidity, and pressure input