Project 3.9.10: Smart Agriculture Monitoring Grid
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico monitors two crop zones plus local climate, then shows which zone needs attention first.
Single-point crop monitoring can hide differences between farm zones and lead to poor irrigation decisions.
A Pico 2 W prototype with two soil moisture sensors, a BME280, and an OLED grid dashboard.
Multi-zone sensing, local prioritization, and dashboard-driven crop monitoring.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Soil sensor |
![]() Breadboard |
![]() Jumper wires |
![]() BME280 |
![]() OLED |
||
| --- |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Zone A soil AO | GPIO 26 ADC0 | GPIO 26 / physical pin 31 | Zone A soil input |
| Zone B soil AO | GPIO 27 ADC1 | GPIO 27 / physical pin 32 | Zone B soil 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 shared with OLED if present |
| BME280 SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL shared with OLED if present |
| OLED SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA |
| OLED SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
| --- |
Wiring Diagram

Zone A soil AO -> GPIO 26
Zone B soil AO -> GPIO 27
BME280 SDA -> GPIO 20
BME280 SCL -> GPIO 21
GPIO 20 -> OLED SDA
GPIO 21 -> OLED SCL
Common GND -> both soil sensors, BME280, and OLED
BME280 VCC -> 3.3V
BME280 GND -> GND
Diagram Placeholder [Insert wiring diagram or breadboard image here]
Step-by-Step Assembly
- Connect the Zone A soil sensor analog output to GPIO 26.
- Connect the Zone B soil sensor analog output to GPIO 27.
- Connect the BME280 to Pico 3.3V, GND, GPIO 20 (SDA), and GPIO 21 (SCL).
- Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
- Upload ssd1306.py and confirm it appears on the Pico before running the dashboard.
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 Zone A, Zone B, and the BME280 separately so the comparison starts from trusted values.
- Test the output device: Run an OLED hello-world test before adding the grid logic.
- Test the decision logic: Dry one zone more than the other and confirm the priority zone changes for the right reason.
- Run the full system: Run the full grid dashboard and compare the displayed priority with the measured dryness values.
- Validate the prototype: Discuss how sensor calibration and crop type would affect the fairness of the comparison.
- 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
try:
from ssd1306 import SSD1306_I2C
HAS_OLED = True
except ImportError:
HAS_OLED = False
ZONE_A_PIN = 26
ZONE_B_PIN = 27
BME_SDA_PIN = 20
BME_SCL_PIN = 21
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
SOIL_DRY_RAW = 48000
SOIL_WET_RAW = 18000
zone_a = ADC(ZONE_A_PIN)
zone_b = ADC(ZONE_B_PIN)
i2c = I2C(0, sda=Pin(BME_SDA_PIN), scl=Pin(BME_SCL_PIN), freq=400000)
climate = bme280.BME280(i2c=i2c)
if HAS_OLED:
oled = SSD1306_I2C(128, 64, i2c)
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 monitoring grid ready')
while True:
dry_a = dryness_percent(zone_a.read_u16())
dry_b = dryness_percent(zone_b.read_u16())
try:
values = climate.values
temperature = float(values[0].replace('C', ''))
pressure = float(values[1].replace('hPa', ''))
humidity = float(values[2].replace('%', ''))
except Exception:
temperature = 0
pressure = 0
humidity = 0
if dry_a > dry_b:
priority = 'ZONE A'
elif dry_b > dry_a:
priority = 'ZONE B'
else:
priority = 'EQUAL'
print('ZoneA:{}% ZoneB:{}% Temp:{}C Hum:{}% Priority:{}'.format(dry_a, dry_b, temperature, humidity, priority))
if HAS_OLED:
oled.fill(0)
oled.text('Agri Grid', 0, 0)
oled.text('A:{}% B:{}%'.format(dry_a, dry_b), 0, 18)
oled.text('T:{} H:{}'.format(temperature, humidity), 0, 34)
oled.text(priority[:16], 0, 50)
oled.show()
time.sleep(3)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Two-zone sensing | Reads two independent soil channels from one Pico. | This is the core change that makes the build a monitoring grid instead of a single sensor demo. | Test each zone sensor in the same conditions before comparing them. |
| Zone comparison | Chooses the zone with the higher dryness as the priority zone. | The dashboard becomes useful because it identifies where attention is needed first. | Adjust the soil calibration values after measuring your own sensors. |
| OLED dashboard | Shows both zones and the current priority on one screen. | A comparison dashboard helps users validate and explain the decision quickly. | Verify ssd1306.py and the I2C wiring if the display is blank. |
Expected Result
The serial monitor reports the current reading or state clearly.
The hardware output responds when the decision logic changes state.
Subsystem behavior matches the thresholds, timing, or rules described in the document.
Validation Checks
- Normal condition test: confirm the system stays in its safe or idle state under baseline conditions.
- Warning condition test: move the input close to the chosen threshold and confirm the transition is sensible.
- Critical condition test: trigger the strongest alert or control state and confirm the output response is correct.
- Calibration test: compare the sensor or timing against a known reference or repeated trial.
- Limitation test: deliberately create an awkward or noisy condition and note how the prototype behaves.
Deployment and Limitations
- This prototype works well for classroom demonstrations and local monitoring pilots.
- Before deployment, it needs calibration records, protective mounting, and regular validation checks.
- Display-only systems still depend on correct sensor placement and trained users.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Both zones always look the same | Both sensors are seeing similar conditions or one wiring path is wrong | Test each sensor separately in clearly different soil conditions. |
| One zone never changes | That soil sensor wiring or calibration is wrong | Print the raw ADC value for each zone and recheck the analog path. |
| Climate values stay at zero | The BME280 read failed | Test the BME280 separately and shorten the I2C wires if needed. |
| OLED is blank | The display library or I2C wiring is wrong | Confirm ssd1306.py is present and verify GPIO 20 and GPIO 21. |





