Project 2.6.12: Smart Greenhouse Safety Alarm
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project builds a greenhouse alarm that reacts quickly when conditions suggest heat stress, severe dryness, or another urgent plant-safety problem.
Students will combine a BME280 sensor, a soil sensor, an acknowledge button, LEDs, and a buzzer into a multi-level safety system.
The final system should classify conditions as SAFE, CAUTION, or ALARM, use different buzzer behaviour for each level, and allow the buzzer to be silenced while the fault is still visible.
Required Components
![]() BME280 sensor module |
![]() Red LED and 220 Ω resistor |
![]() Raspberry Pi Pico 2 W |
![]() Soil moisture sensor (analog output) |
![]() Push button |
![]() Yellow LED and 220 Ω resistor |
![]() Active buzzer |
![]() Breadboard and jumper wires |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor VCC | Physical pin 36 | Physical pin 36 | Sensor power |
| Soil sensor GND | Any GND pin | Any GND pin | Common ground |
| Soil sensor AOUT | GPIO 26 / physical pin 31 | GPIO 26 / physical pin 31 | Analog soil input |
| Acknowledge button one side | GPIO 5 | GPIO 5 / physical pin 7 | Uses internal pull-up |
| Acknowledge button other side | GND | Any GND pin | Pressing pulls the input low |
| Yellow LED anode | GPIO 16 through 220 Ω resistor | GPIO 16 / physical pin 21 | Caution indicator |
| Red LED anode | GPIO 17 through 220 Ω resistor | GPIO 17 / physical pin 22 | Alarm indicator |
| Buzzer positive | GPIO 18 / physical pin 24 | Alarm output | |
| LED cathodes and buzzer negative | GND | Any GND pin | Return path |
| 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

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 2 ───────────┤──── BME280 SDA
│ GPIO 3 ───────────┤──── BME280 SCL
│ │
│ GPIO 26 ───────────┤──── Soil Sensor AOUT
│ │
│ GPIO 5 ───────────┤──── Acknowledge Button ──── GND
│ │
│ GPIO 16 ────220Ω───┤──── Yellow LED (+)
│ GPIO 17 ────220Ω───┤──── Red LED (+)
│ GPIO 18 ───────────┤──── Buzzer (+)
│ │
│ 3.3V ───────────┤──── BME280 VCC
│ 3.3V ───────────┤──── Soil Sensor VCC
│ │
│ GND ───────────┤──── BME280 GND
│ GND ───────────┤──── Soil Sensor GND
│ GND ───────────┤──── Yellow LED (-)
│ GND ───────────┤──── Red LED (-)
│ GND ───────────┤──── Buzzer (-)
│ GND ───────────┤──── Acknowledge Button GND
│ │
└─────────────────────┘
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 your BME280 is bare, use the module pins labeled VCC, GND, SDA, and SCL.
Step 3: 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 26.
Step 4: Place the Acknowledge Button
Place the acknowledge push button across the breadboard center gap. Connect one side of the acknowledge button to GPIO 5. Connect the opposite side of the button to GND.
Step 5: Place and Connect the Warning LEDs
Place the yellow caution LED and red alarm LED on the breadboard. Connect the yellow LED long leg through a 220Ω resistor to GPIO 16. Connect the red LED long leg through a 220Ω resistor to GPIO 17. Connect both LED short legs to GND.
Step 6: Place and Connect the Buzzer
Place the buzzer on the breadboard and identify its positive (+) and negative (-) pins. Connect the buzzer positive pin to GPIO 18. Connect the buzzer negative pin to GND.
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] Soil sensor AOUT / AO / Signal connects to GPIO 26
- [x] Acknowledge button connects between GPIO 5 and GND
- [x] Yellow LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Red LED long leg connects through a 220Ω resistor to GPIO 17
- [x] Both LED short legs connect to GND
- [x] Buzzer positive pin connects to GPIO 18
- [x] Buzzer negative pin connects to GND
- [x] No loose jumper wires
Intermediate Note The acknowledge button silences the buzzer while you investigate. The BME280 needs a short delay between readings, and soil thresholds should be calibrated before final testing.
Safety Note This is a learning prototype, not a certified safety alarm. Keep wet soil and water away from the Pico, breadboard, USB cable, and jumper wires.
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 add the LEDs, buzzer, and acknowledge button
- Keep the sensors steady so sudden changes in readings are caused by real conditions, not loose wires
Test the Input Sensor
- Read the BME280 and soil sensor separately before turning on the alarm logic
- Confirm the acknowledge button changes state correctly in the serial output
Test the Output Device
- Test the yellow LED, red LED, and buzzer with short manual checks before relying on automatic alarms
- Make sure the buzzer can be silenced while the LEDs still show the problem
Test Communication
- Watch the serial monitor and confirm it prints the current safety level and the rule that caused it
- Use the printed reason to explain the difference between CAUTION and ALARM
Run the Full System
- Simulate a mild issue and confirm the yellow LED and caution beep pattern appear first
- Then create a more severe condition and confirm the red LED and stronger alarm pattern are used
Save the Project
- Save the final code and record which rules created CAUTION and which created ALARM in your test
- Write down how the system should be improved before any real deployment
Additional Testing and Calibration Checks
- Dry/wet reading test: calibrate the soil sensor so the drought thresholds are meaningful
- Normal condition test: confirm the system reports SAFE with normal room readings
- Threshold condition test: create a caution-level problem and then a more severe alarm-level problem
- Output response test: confirm the LEDs and buzzer pattern match the current state
- Button test: confirm the acknowledge button silences the buzzer but does not turn off the warning LEDs
- Calibration note: the exact heat and dryness thresholds depend on the crop and greenhouse design
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
soil_sensor = ADC(26)
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)
ack_button = Pin(5, Pin.IN, Pin.PULL_UP)
caution_led = Pin(16, Pin.OUT)
alarm_led = Pin(17, Pin.OUT)
buzzer = Pin(18, Pin.OUT)
SOIL_DRY = 52000
SOIL_WET = 22000
CAUTION_TEMP = 32
ALARM_TEMP = 38
LOW_HUMIDITY = 35
CAUTION_SOIL = 25
ALARM_SOIL = 15
DEBOUNCE_MS = 250
silenced = False
last_button_ms = 0
last_state = 'SAFE'
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
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 read_environment():
try:
temp_c, humidity, pressure_hpa = read_bme280()
return temp_c, humidity
except Exception as error:
print('BME28022 read error:', error)
return None, None
def button_pressed():
global last_button_ms
now_ms = time.ticks_ms()
if ack_button.value() == 0 and time.ticks_diff(now_ms, last_button_ms) > DEBOUNCE_MS:
while ack_button.value() == 0:
time.sleep(0.02)
last_button_ms = now_ms
return True
return False
def safety_state(temp_c, humidity, soil):
if temp_c is None or humidity is None:
return 'ALARM', 'Sensor read failed'
if temp_c >= ALARM_TEMP or soil <= ALARM_SOIL:
return 'ALARM', 'Extreme heat or severe drought'
if temp_c >= CAUTION_TEMP and humidity <= LOW_HUMIDITY:
return 'ALARM', 'Hot and dry greenhouse condition'
if temp_c >= CAUTION_TEMP or soil <= CAUTION_SOIL:
return 'CAUTION', 'Heat or drought stress building'
return 'SAFE', 'Conditions are within the working range'
def beep_pattern(level):
if level == 'CAUTION':
buzzer.value(1)
time.sleep(0.08)
buzzer.value(0)
elif level == 'ALARM':
for _ in range(3):
buzzer.value(1)
time.sleep(0.12)
buzzer.value(0)
time.sleep(0.08)
print('=== Smart Greenhouse Safety Alarm ===')
print('Press the button to silence the buzzer while investigating.\n')
while True:
soil = soil_percent()
temp_c, humidity = read_environment()
state, reason = safety_state(temp_c, humidity, soil)
if state != last_state:
silenced = False
last_state = state
if button_pressed() and state != 'SAFE':
silenced = True
print('Alarm output silenced by the user.')
caution_led.value(1 if state == 'CAUTION' else 0)
alarm_led.value(1 if state == 'ALARM' else 0)
if state == 'SAFE':
buzzer.value(0)
elif not silenced:
beep_pattern(state)
print('Temp: {} | Humidity: {} | Soil: {}% | State: {} | Reason: {}'.format(
temp_c,
humidity,
soil,
state,
reason
))
time.sleep(4)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| safety_state() | Classifies the greenhouse into SAFE, CAUTION, or ALARM | This is the main rule-based decision logic in the project |
| Different thresholds | Uses separate values for caution and alarm levels | Students can see how alarm systems escalate instead of treating every issue equally |
| beep_pattern() | Produces different buzzer patterns for each risk level | Different outputs make the risk level easier to understand |
| Silence logic | Lets the user mute the buzzer while the condition is still active | This matches real alarm workflows |
Expected Result
Under normal conditions, both LEDs should stay off and the buzzer should remain silent.
During a moderate problem, the yellow LED should turn on and the caution beep pattern should sound.
During a serious problem, the red LED should turn on and the stronger alarm pattern should sound until the user silences it or the condition recovers.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The system always stays in ALARM | Thresholds are too sensitive or a sensor reading is out of range | Check the BME280 and soil readings individually and review the chosen thresholds |
| The buzzer never sounds | GPIO 18 wiring is wrong or the button is stuck low | Check GPIO 18 wiring and confirm the acknowledge button returns to HIGH when released |
| CAUTION never appears | The CAUTION and ALARM thresholds are too close together | Separate the CAUTION and ALARM values more clearly so both states can be tested |
| The LEDs do not match the printed state | The LED wiring or code pin assignments are wrong | Recheck GPIO 16 and GPIO 17 against the circuit table and the code |






