Project 3.1.18: Multi-sensor Smart Farm Management System
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico combines soil moisture, tank level, and climate readings to show a local farm decision-support dashboard.
Farm decisions become unreliable when soil condition, water availability, and air temperature are checked separately or not checked at all.
A Pico 2 W prototype with a soil moisture sensor, water-level sensor, BME280, and OLED dashboard for farm decisions.
Multi-sensor integration, calibration-based percentage mapping, and local farm recommendation logic.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() BME280 |
![]() Breadboard |
![]() Jumper wires |
![]() OLED |
![]() Soil moisture sensor |
![]() Water level sensor |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| 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 |
| BME280 SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL shared with OLED |
| Soil sensor AO | GPIO 26 ADC0 | GPIO 26 / physical pin 31 | Analog soil reading |
| Tank sensor AO | GPIO 27 ADC1 | GPIO 27 / physical pin 32 | Analog water-level reading |
| OLED SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA |
| OLED SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
Wiring Diagram

Soil sensor AO -> GPIO 26
Tank sensor AO -> GPIO 27
BME280 SDA -> GPIO 20
BME280 SCL -> GPIO 21
GPIO 20 -> OLED SDA
GPIO 21 -> OLED SCL
Common GND -> all sensors and OLED
BME280 VCC -> 3.3V
BME280 GND -> GND
Step-by-Step Assembly
- Connect the Soil Moisture Sensor analog output to GPIO 26 and its ground to Pico GND.
- Connect the water-level sensor analog output to GPIO 27 and keep the sensor body near the tank only.
- 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 the file is visible with
os.listdir()before running the full 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: Read the soil sensor, tank sensor, and BME280 separately so each raw value is believable before combining them.
- Test the output device: Run an OLED hello-world test so the display path is confirmed before adding decision logic.
- Test the decision logic: Change soil moisture and tank level conditions and confirm the recommendation changes from MONITOR to PLAN or IRRIGATE at the expected points.
- Run the full system: Run the full dashboard and compare the OLED recommendation with the serial values in real time.
- Validate the prototype: Repeat the test after moving the sensor positions slightly to see how much calibration and placement affect the recommendation.
- 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.
from machine import ADC, I2C, Pin
import bme280
import time
try:
from ssd1306 import SSD1306_I2C
HAS_OLED = True
except ImportError:
HAS_OLED = False
SOIL_PIN = 26
TANK_PIN = 27
BME_SDA_PIN = 20
BME_SCL_PIN = 21
SOIL_DRY_RAW = 48000
SOIL_WET_RAW = 18000
TANK_EMPTY_RAW = 5000
TANK_FULL_RAW = 50000
soil = ADC(SOIL_PIN)
tank = ADC(TANK_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 percent_from_range(raw, low_raw, high_raw):
span = high_raw - low_raw
if span == 0:
return 0
pct = int((raw - low_raw) * 100 / span)
return clamp(pct, 0, 100)
print('Smart farm dashboard ready')
while True:
soil_raw = soil.read_u16()
tank_raw = tank.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
soil_dry_pct = percent_from_range(soil_raw, SOIL_WET_RAW, SOIL_DRY_RAW)
tank_pct = percent_from_range(tank_raw, TANK_EMPTY_RAW, TANK_FULL_RAW)
if soil_dry_pct >= 70 and tank_pct >= 25:
recommendation = 'IRRIGATE'
elif soil_dry_pct >= 70 and tank_pct < 25:
recommendation = 'REFILL TANK'
elif soil_dry_pct >= 45 or temperature >= 32:
recommendation = 'PLAN SOON'
else:
recommendation = 'MONITOR'
print('SoilDry:{}% Tank:{}% Temp:{}C Hum:{}% Action:{}'.format(
soil_dry_pct,
tank_pct,
temperature,
humidity,
recommendation,
))
if HAS_OLED:
oled.fill(0)
oled.text('Farm Manager', 0, 0)
oled.text('Soil:{}%'.format(soil_dry_pct), 0, 16)
oled.text('Tank:{}%'.format(tank_pct), 0, 28)
oled.text('Temp:{}C'.format(temperature), 0, 40)
oled.text(recommendation[:16], 0, 52)
oled.show()
time.sleep(2)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Sensor setup | Creates one dashboard from soil, water, and climate inputs | Farm decisions are stronger when multiple field conditions are checked together | Change pin assignments only if your physical build uses different GPIO pins |
| Calibration mapping | Converts raw soil and tank values into percentages | Percentages are easier to interpret than raw ADC numbers, but only after calibration | Measure your own dry, wet, empty, and full references before changing the constants |
| Recommendation logic | Chooses MONITOR, PLAN SOON, IRRIGATE, or REFILL TANK based on live conditions | This shows how simple farm management logic can combine resource availability with crop need | Adjust the thresholds only after collecting repeated test data |
| OLED update | Displays the current farm state locally | A local dashboard lets students validate the system without building a network stack first | If the display is blank, verify ssd1306.py and the I2C wiring before changing the logic |
Expected Result
The OLED and serial monitor show calibrated soil and tank percentages together with temperature and humidity.
Dry soil with enough stored water changes the recommendation to IRRIGATE.
Dry soil with low tank level changes the recommendation to REFILL TANK instead of pretending irrigation is available.
Validation Checks
- Normal condition test: place the soil sensor in moderately moist soil and confirm the system stays in MONITOR or PLAN SOON
- Warning condition test: dry the soil gradually and confirm the recommendation changes before the soil becomes extremely dry
- Critical decision test: simulate low tank level and confirm the system recommends REFILL TANK instead of IRRIGATE
- Sensor reading validation: compare tank and soil percentages against manually observed empty, half, and full conditions
- Calibration test: record raw values for dry soil, wet soil, empty tank, and full tank before adjusting the constants
- Limitation test: move one sensor slightly and note how placement changes the decision
Deployment and Limitations
- This prototype could support classroom farm demonstrations, greenhouse benches, or small irrigation planning pilots
- Before deployment, the enclosure, cable routing, and long-term sensor durability need improvement
- Field use would also need stable power, periodic recalibration, and user training on what each recommendation means
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Tank percentage never changes | The analog tank signal is not reaching GPIO 27 or the calibration range is wrong | Print the raw tank value and verify the sensor wiring before editing the percentage math |
| Soil percentage looks reversed | Dry and wet calibration values are swapped | Re-measure the dry and wet references and update SOIL_DRY_RAW and SOIL_WET_RAW |
| OLED is blank | The display library file or I2C wiring is wrong | Use GPIO 20 and GPIO 21 and confirm ssd1306.py is on the Pico |
| Recommendation changes too early | The thresholds do not match the real field setup | Collect baseline readings and tune the action thresholds slowly |






