Project 3.9.9: Smart Agriculture Iot Os
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico acts as a local farm orchestrator that controls irrigation and indicates when supplemental lighting is needed.
Farm automation becomes hard to explain when sensors are collected without a clear local control layer that decides which action should happen next.
A Pico 2 W prototype with soil, light, and BME280 climate sensing, one relay-controlled irrigation output, and a supplemental-light indicator.
Multi-sensor orchestration, single-relay control, and honest wording for an OS-style supervisor that is still a prototype.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Soil sensor |
![]() Breadboard |
![]() Jumper wires |
![]() LDR |
![]() BME280 |
![]() Single-channel relay |
![]() Water pump |
![]() Supplemental-light LED |
![]() 220 ohm resistor |
||
| --- |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor AO | GPIO 26 ADC0 | GPIO 26 / physical pin 31 | Soil input |
| Light sensor output | GPIO 27 ADC1 | GPIO 27 / physical pin 32 | Light input |
| BME280 VCC | 3.3V output | Physical pin 36 | Sensor power |
| BME280 GND | GND | Physical pin 38 | Common ground |
| BME280 SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA |
| BME280 SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
| Relay IN | GPIO 16 | GPIO 16 / physical pin 21 | Irrigation output |
| Supplemental-light LED anode | 220 ohm resistor to GPIO 17 | GPIO 17 / physical pin 22 | Lighting-needed indicator |
| --- |
Wiring Diagram

Soil sensor AO -> GPIO 26
Light sensor output -> GPIO 27
BME280 SDA -> GPIO 20
BME280 SCL -> GPIO 21
BME280 VCC -> 3.3V
BME280 GND -> GND
GPIO 16 -> relay IN (pump)
GPIO 17 -> 220 ohm resistor -> supplemental-light LED anode
External supply -> relay-switched pump
Step-by-Step Assembly
- Connect the soil sensor analog output to GPIO 26 and plan to calibrate dry and wet references.
- Build the light sensor analog path and connect it to GPIO 27.
- Connect the BME280 to Pico 3.3V, GND, GPIO 20 (SDA), and GPIO 21 (SCL).
- Connect the relay input to GPIO 16 for the pump and wire the supplemental-light LED to GPIO 17 through a 220 ohm resistor.
- Test the relay without the real pump attached and test the LED before connecting the final load.
Testing Individual Components
Before running the full project, test each subsystem separately. This makes it easier to find wiring, library, or logic problems before full integration.
- Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
- Test the input sensor: Test each sensor separately so you trust the dryness, light, and climate values before control begins.
- Test the output device: Toggle the relay and supplemental-light LED from a short test script before running the supervisor logic.
- Test the decision logic: Create dry and dark conditions separately so you can verify irrigation control and the lighting-needed indicator.
- Run the full system: Run the full orchestrator and compare the measured values with the relay and LED states.
- Validate the prototype: Review whether the rules are enough for real farming or whether timing and scheduling would still be required.
- Save the project: Save the validated program on the Pico as main.py and keep a copy on the computer for future edits.
Full Project Code
After completing and checking the circuit connections, open Thonny IDE, copy and paste this code into a new file or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.
Upload and run this code after the individual tests work correctly.
from machine import ADC, I2C, Pin
import bme280
import time
SOIL_PIN = 26
LIGHT_PIN = 27
BME_SDA_PIN = 20
BME_SCL_PIN = 21
IRRIGATION_RELAY_PIN = 16
LIGHT_INDICATOR_PIN = 17
SOIL_DRY_RAW = 48000
SOIL_WET_RAW = 18000
LIGHT_THRESHOLD = 22000
HOT_TEMP = 34
soil = ADC(SOIL_PIN)
light = ADC(LIGHT_PIN)
i2c = I2C(0, sda=Pin(BME_SDA_PIN), scl=Pin(BME_SCL_PIN), freq=400000)
climate = bme280.BME280(i2c=i2c)
irrigation_relay = Pin(IRRIGATION_RELAY_PIN, Pin.OUT)
light_indicator = Pin(LIGHT_INDICATOR_PIN, Pin.OUT)
def clamp(value, low, high):
return max(low, min(high, value))
def dryness_percent(raw):
span = SOIL_DRY_RAW - SOIL_WET_RAW
if span <= 0:
return 0
pct = int((raw - SOIL_WET_RAW) * 100 / span)
return clamp(pct, 0, 100)
print('Smart agriculture IoT OS ready')
while True:
dry_pct = dryness_percent(soil.read_u16())
light_raw = light.read_u16()
try:
values = climate.values
temperature = float(values[0].replace('C', ''))
humidity = float(values[2].replace('%', ''))
except Exception:
temperature = 0
humidity = 0
irrigation_on = dry_pct >= 65 or (dry_pct >= 55 and temperature >= HOT_TEMP)
lighting_on = light_raw < LIGHT_THRESHOLD
irrigation_relay.value(1 if irrigation_on else 0)
light_indicator.value(1 if lighting_on else 0)
print('Dry:{}% Light:{} Temp:{}C Hum:{}% Irrigation:{} LightAssist:{}'.format(
dry_pct,
light_raw,
temperature,
humidity,
irrigation_on,
lighting_on,
))
time.sleep(3)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Sensor normalization | Converts raw soil and light values into more useful decision inputs. | The supervisor should act on interpretable signals rather than raw counts only. | Calibrate the soil and light thresholds with your own hardware before field-style tests. |
| Control orchestration | Decides irrigation and lighting-needed states in the same control loop. | This combines several sensor inputs while keeping the hardware simple. | Test the relay and LED one at a time before trusting both together. |
| Output update | Applies irrigation to the relay and lighting-needed state to the LED. | Hardware outputs must match the documented logic for the project to be trustworthy. | Invert the relay output here if your relay module is active low. |
Expected Result
The serial monitor shows soil, light, temperature, humidity, and the state of both outputs.
Dry conditions turn on irrigation while dark conditions turn on the supplemental-light indicator.
The pump relay and supplemental-light indicator can be active at once when the farm conditions require both actions.
Validation Checks
- Normal condition test: use moist soil and bright light and confirm the relay and LED stay off.
- Irrigation test: dry the soil and confirm the irrigation relay turns on when the threshold is crossed.
- Lighting test: reduce the light level and confirm the supplemental-light LED turns on.
- Combined condition test: use dry and dark conditions together and confirm the relay and LED activate.
- Output response validation: test the relay module without the real pump first, then attach the pump after safe switching is confirmed.
Deployment and Limitations
- This prototype could be used in school, farm, or sanitation demonstrations with safe low-voltage loads.
- Before deployment, the load wiring, enclosure, and power design need careful review.
- Actuator systems need maintenance planning because relay wear and sensor drift affect reliability.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Irrigation turns on too often | The dryness threshold is too low | Collect soil baseline data and raise the irrigation threshold carefully. |
| Light assist never turns on | The light threshold is too low or the analog divider is wrong | Print the raw light value and recalibrate the threshold in bright and dark conditions. |
| Relay or light indicator does not respond | The output wiring or pin mapping is incorrect | Test the relay and LED separately from a short script and verify the control pins. |
| Climate values stay at zero | The BME280 read failed | Test the BME280 separately and check the I2C wiring. |









