Skip to content

Project 2.6.24: Smart Plant Disease Indicator

Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython


Overview

This project builds a rule-based plant disease risk indicator using soil moisture, temperature, and humidity readings.

Students will build a multi-sensor monitoring circuit, calibrate the soil sensor, and combine several readings into one disease-risk score.

The final system should classify plant conditions as LOW RISK, MODERATE RISK, or HIGH RISK and show the result with LEDs and serial output.

Required Components

Soil moisture sensor (analog output)
Soil moisture sensor (analog output)
Breadboard and jumper wires
Breadboard and jumper wires
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
BME280 sensor module
BME280 sensor module
Green LED and 220 Ω resistor
Green LED and 220 Ω resistor
Red LED and 220 Ω resistor
Red LED and 220 Ω resistor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Soil sensor VCC Physical pin 36 Sensor power
Soil sensor GND Physical pin 38 Common ground
Soil sensor AOUT GPIO 26 GPIO 26 / physical pin 31 Analog soil reading
BME280 sensor module Requires bme280.py on the Pico
BME280 sensor module Requires bme280.py on the Pico
BME280 sensor module Requires bme280.py on the Pico
Green LED anode GPIO 16 through 220 Ω resistor GPIO 16 / physical pin 21 Low-risk indicator
Red LED anode GPIO 17 through 220 Ω resistor GPIO 17 / physical pin 22 Moderate/high-risk indicator
Both LED cathodes GND Any GND pin Return path for LEDs
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

Circuit Diagram

  Raspberry Pi Pico 2 W
  ┌─────────────────────┐
  │                     │
  │  GPIO 2  ───────────┤──── BME280 SDA
  │  GPIO 3  ───────────┤──── BME280 SCL
  │                     │
  │  GPIO 26 ───────────┤──── Soil Sensor AOUT
  │                     │
  │  GPIO 16 ────220Ω───┤──── Green LED (+)
  │  GPIO 17 ────220Ω───┤──── Red LED (+)
  │                     │
  │  3.3V    ───────────┤──── Soil Sensor VCC
  │  3.3V    ───────────┤──── BME280 VCC
  │                     │
  │  GND     ───────────┤──── Soil Sensor GND
  │  GND     ───────────┤──── BME280 GND
  │  GND     ───────────┤──── Green LED (-)
  │  GND     ───────────┤──── Red LED (-)
  │                     │
  └─────────────────────┘

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: Position the Soil Moisture Sensor

Place the soil moisture probe so the sensing end can enter the soil sample. Check the module labels and identify VCC, GND, and AOUT / AO / Signal.

Step 3: Connect the Soil Sensor

Connect soil sensor VCC to 3.3V. Connect soil sensor GND to GND. Connect soil sensor AOUT, AO, or Signal to GPIO 26 (ADC0).

Step 4: Place the BME280 Sensor Module

Place the BME280 module where it can sense the plant-area air. Identify VCC, GND, and DATA before wiring. Connect BME280 VCC to 3.3V. Connect BME280 GND to GND. Connect BME280 SDA to GPIO 2 and BME280 SCL to GPIO 3. If you are using a bare BME280 sensor instead of a module, use the module pins labeled VCC, GND, SDA, and SCL to 3.3V.

Step 5: Place and Connect the Green LED

Place the green LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the green LED long leg through a 220Ω resistor to GPIO 16. Connect the green LED short leg to GND.

Step 6: Place and Connect the Red LED

Place the red LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the red LED long leg through a 220Ω resistor to GPIO 17. Connect the red LED short leg to GND.

Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] Soil sensor VCC connects to 3.3V
  • [x] Soil sensor GND connects to GND
  • [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
  • [x] BME280 SDA connects to GPIO 2 and SCL connects to GPIO 3
  • [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] No loose jumper wires

Intermediate Note Calibrate SOIL_DRY and SOIL_WET before final testing. The BME280 needs a short delay between readings, and disease-risk logic is only as good as the local thresholds.

Safety Note Do not pour water onto the Pico, breadboard, USB cable, or jumper wires. Add water to the soil sample away from the electronics.


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 full circuit on a breadboard before inserting the soil probe into a plant pot
  • Keep the Pico on a dry surface and route the soil sensor cable away from water

Test the Input Sensor

  • Print raw soil readings with the sensor in dry soil and then in wet soil to choose realistic calibration values
  • Read the BME280 temperature and humidity values several times to confirm stable sensor communication

Test the Output Device

  • Temporarily set a very low HIGH_RISK threshold to confirm the red LED turns on
  • Temporarily set a very high HIGH_RISK threshold to confirm the green LED turns on during a low-risk state

Test Communication

  • Open the Thonny Shell and verify that the program prints temperature, humidity, soil moisture, and the risk explanation text
  • Watch how the score changes when you make the soil wetter or warm the BME280 gently with your hand

Run the Full System

  • Place the sensor in the plant soil, let the system run for several measurement cycles, and observe whether the LED state matches the printed risk score
  • Compare a low-humidity room reading with a humid test condition such as covering the sensor area with a clear container for a short time

Save the Project

  • Save the final program as main.py or with a project name in Thonny
  • Write down your final dry, wet, moderate, and high-risk thresholds for future tuning

Additional Testing and Calibration Checks

  • Dry/wet reading test: record the raw soil value in dry soil and in wet soil before finalizing SOIL_DRY and SOIL_WET
  • Normal condition test: run the circuit in a normal room and confirm the score stays in the low range
  • Threshold condition test: warm the BME280 slightly or increase local humidity and watch the score move upward
  • Output response test: confirm green LED for low risk, both LEDs for moderate risk, and red LED for high risk
  • Calibration note: if the soil percentage moves backward, swap the SOIL_DRY and SOIL_WET values to match your sensor behaviour

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)


green_led = Pin(16, Pin.OUT)
red_led = Pin(17, Pin.OUT)

SOIL_DRY = 52000
SOIL_WET = 22000
TEMP_HIGH = 30
HUMIDITY_HIGH = 80
SOIL_WET_RISK = 75
MODERATE_RISK = 30
HIGH_RISK = 70
SAMPLE_DELAY = 10


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():
    temp_c, humidity, pressure_hpa = read_bme280()
    return temp_c, humidity


def disease_risk(temp_c, humidity, soil_moisture):
    score = 0
    reasons = []

    if humidity >= HUMIDITY_HIGH:
        score += 40
        reasons.append('high humidity')
    elif humidity >= 70:
        score += 20
        reasons.append('rising humidity')

    if temp_c >= TEMP_HIGH:
        score += 30
        reasons.append('high temperature')
    elif temp_c >= 26:
        score += 15
        reasons.append('warm temperature')

    if soil_moisture >= SOIL_WET_RISK:
        score += 30
        reasons.append('very wet soil')
    elif soil_moisture >= 60:
        score += 15
        reasons.append('wet soil')

    return score, reasons


print('=== Smart Plant Disease Indicator ===')
print('Calibrate SOIL_DRY and SOIL_WET before final testing.\n')

while True:
    try:
        temp_c, humidity = read_environment()
        soil_moisture = soil_percent()
        risk, reasons = disease_risk(temp_c, humidity, soil_moisture)

        if risk >= HIGH_RISK:
            status = 'HIGH RISK'
            red_led.value(1)
            green_led.value(0)
        elif risk >= MODERATE_RISK:
            status = 'MODERATE RISK'
            red_led.value(1)
            green_led.value(1)
        else:
            status = 'LOW RISK'
            red_led.value(0)
            green_led.value(1)

        reason_text = ', '.join(reasons) if reasons else 'conditions stable'
        print('Temp: {} C | Humidity: {} % | Soil: {} % | Risk: {} % | {} | {}'.format(
            temp_c, humidity, soil_moisture, risk, status, reason_text
        ))

    except Exception as error:
        red_led.value(0)
        green_led.value(0)
        print('Sensor read error:', error)

    time.sleep(SAMPLE_DELAY)

How the Code Works

Code Section 1 Why It Matters
SOIL_DRY and SOIL_WET 1 The soil percentage is only meaningful after calibration
disease_risk() 1 This is rule-based decision logic instead of full machine learning
LED status logic Maps LOW, MODERATE, and HIGH risk to visible LED states Students can understand system decisions without reading code every time
Serial print output 1 Troubleshooting is much easier when the decision process is visible

Expected Result

When the environment is cool and not too wet, the green LED should stay on and the Shell should report LOW RISK.

When humidity, temperature, or soil moisture rises, the printed risk score should increase and the system should move to MODERATE RISK or HIGH RISK.

At HIGH RISK, the red LED should turn on while the Shell explains which conditions caused the alert.


Troubleshooting

Problem 1 Solution
BME280 sensor module 1 Temperature, humidity, and pressure input
The soil percentage is always 0% or 100% 1 Measure real raw values first and update SOIL_DRY and SOIL_WET
The red LED never turns on The HIGH_RISK threshold is too high for your room conditions Temporarily lower HIGH_RISK to confirm the LED wiring, then retune the project
The risk score seems unrealistic 1 Adjust humidity, temperature, or soil thresholds after observing several readings