Project 2.1.8: Farm Environmental Compliance Monitor
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project builds a monitor that checks whether farm conditions remain inside defined safe operating limits.
Students will combine a BME280 sensor, a soil sensor, visual status LEDs, and an acknowledge button into one condition-monitoring system.
The final system should report COMPLIANT, WARNING, or ALERT states, light the correct indicators, and allow the buzzer to be acknowledged while the out-of-range condition is still being investigated.
Required Components
![]() BME280 sensor module |
![]() Red LED and 220 Ω resistor |
![]() Raspberry Pi Pico 2 W |
![]() Soil moisture sensor (analog output) |
![]() Push button |
![]() Green 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 |
| Green LED anode | GPIO 16 through 220 Ω resistor | GPIO 16 / physical pin 21 | Compliant indicator |
| Red LED anode | GPIO 17 through 220 Ω resistor | GPIO 17 / physical pin 22 | Warning or alert indicator |
| Buzzer positive | 1 | GPIO 18 / physical pin 24 | Alert 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Ω───┤──── Green 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 ───────────┤──── Green 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 the monitored farm environment. 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 Indicator LEDs
Place the green LED and red LED on the breadboard. Connect the green 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] Green 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 BME280 needs a short delay between readings. The acknowledge button silences a confirmed alert, but the issue still needs to be investigated.
Safety Note This is a learning prototype, not a certified compliance instrument. 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 BME280 and soil sensor wiring first, then add the LEDs, buzzer, and acknowledge button
- Keep the sensors physically stable so the compliance readings do not change because of loose wiring
Test the Input Sensor
- Read the BME280 values and confirm they are realistic for the room before trusting the compliance limits
- Record dry and wet soil values and confirm the soil percentage moves in the expected direction
Test the Output Device
- Briefly test the LEDs and buzzer so you know the outputs work before relying on the alert logic
- Confirm the buzzer can be acknowledged without pretending the sensor problem has disappeared
Test Communication
- Watch the Thonny Shell and confirm it prints the measured values, list of issues, and overall status
- Use the serial output to compare the WARNING state with the full ALERT state
Run the Full System
- Create one mild out-of-range condition and then a stronger combined condition to observe COMPLIANT, WARNING, and ALERT behaviour
- Press the acknowledge button during an active alert and confirm the buzzer silences while the red indicator remains active
Save the Project
- Save the final code and record the chosen compliance limits for temperature, humidity, and soil moisture
- Write down which alert conditions would matter most in your real farm or greenhouse context
Additional Testing and Calibration Checks
- Dry/wet reading test: calibrate the soil sensor so the minimum soil requirement is meaningful
- Normal condition test: confirm the system remains COMPLIANT under stable room conditions
- Threshold condition test: create one out-of-range value and confirm the system moves through WARNING before ALERT
- Output response test: check that the LEDs and buzzer match the current status
- Calibration note: different crops may require different humidity and soil limits
- Button test: confirm the acknowledge button silences the buzzer but does not falsely mark the system as compliant
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)
green_led = Pin(16, Pin.OUT)
red_led = Pin(17, Pin.OUT)
buzzer = Pin(18, Pin.OUT)
SOIL_DRY = 52000
SOIL_WET = 22000
TEMP_MIN = 18
TEMP_MAX = 32
HUM_MIN = 40
HUM_MAX = 80
SOIL_MIN = 30
CONFIRM_READS = 2
DEBOUNCE_MS = 250
out_of_range_hits = 0
acknowledged = False
last_button_ms = 0
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 compliance_issues(temp_c, humidity, soil):
issues = []
if temp_c is None or humidity is None:
issues.append('Sensor read failed')
return issues
if temp_c < TEMP_MIN:
issues.append('Temperature below minimum')
if temp_c > TEMP_MAX:
issues.append('Temperature above maximum')
if humidity < HUM_MIN:
issues.append('Humidity below minimum')
if humidity > HUM_MAX:
issues.append('Humidity above maximum')
if soil < SOIL_MIN:
issues.append('Soil moisture below minimum')
return issues
def beep(times=2):
for _ in range(times):
buzzer.value(1)
time.sleep(0.12)
buzzer.value(0)
time.sleep(0.12)
print('=== Farm Environmental Compliance Monitor ===')
print('Press the button to acknowledge a confirmed alert.\n')
while True:
soil = soil_percent()
temp_c, humidity = read_environment()
issues = compliance_issues(temp_c, humidity, soil)
if issues:
out_of_range_hits += 1
else:
out_of_range_hits = 0
acknowledged = False
if button_pressed() and issues:
acknowledged = True
print('Alert acknowledged by the user.')
if not issues:
status = 'COMPLIANT'
green_led.value(1)
red_led.value(0)
buzzer.value(0)
elif out_of_range_hits < CONFIRM_READS:
status = 'WARNING'
green_led.value(0)
red_led.value(1)
buzzer.value(0)
else:
status = 'ALERT'
green_led.value(0)
red_led.value(1)
if not acknowledged:
beep()
print('Temp: {} | Humidity: {} | Soil: {}% | Status: {} | Issues: {}'.format(
temp_c,
humidity,
soil,
status,
', '.join(issues) if issues else 'None'
))
time.sleep(5)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| Compliance limits | Define the acceptable range for each measurement | These limits turn raw measurements into meaningful engineering checks |
| compliance_issues() | Compares each sensor against its limits and lists the failures | Students can see exactly which condition failed instead of only seeing a red light |
| Confirmation count | Requires several consecutive bad readings before a full alert | This reduces false alarms caused by one unstable reading |
| Acknowledge button | Lets a user silence the buzzer while still keeping the warning visible | Real systems often need human acknowledgement without hiding the fault |
Expected Result
When all readings stay inside the chosen limits, the green LED should remain on and the system should report COMPLIANT.
If one or more conditions move out of range, the system should first show WARNING and then move to ALERT after repeated bad readings.
Pressing the acknowledge button during an ALERT should silence the buzzer while the red LED and printed issue list remain active until conditions recover.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The system always says sensor read failed | The BME280 wiring is wrong or the pull-up resistor is missing on a bare sensor | Check GPIO 2/3, power, ground, and add a 10 kΩ pull-up if needed |
| The buzzer never stops | The acknowledge button wiring is wrong or the condition has not recovered | Check the GPIO 5 button wiring and confirm the alert condition really changes after recovery |
| The soil condition looks unrealistic | Soil calibration values do not match the sensor | Measure new SOIL_DRY and SOIL_WET values with your own sensor and soil sample |
| The system goes straight to ALERT too often | The confirmation count is too low | Increase CONFIRM_READS to require more repeated bad readings before the alert is confirmed |






