Skip to content

Project 2.1.17: Plant Stress Detection

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


Overview

This project looks for early warning signs of plant stress by combining soil moisture and light conditions into one stress score.

Students will build a dual-sensor monitoring system, calculate a stress score, and display LOW, MODERATE, or HIGH stress with LEDs.

The final system should read both sensors, explain which stress factors were detected, and indicate the overall stress level clearly.

Required Components

Soil moisture sensor
Soil moisture sensor
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
LDR module with analog output
LDR module with analog output
Green, yellow, and red LEDs with 220 Ω resistors
Green, yellow, and red LEDs with 220 Ω resistors
Breadboard and jumper wires
Breadboard and jumper wires

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Soil sensor AOUT GPIO 26 GPIO 26 / physical pin 31 ADC moisture input
LDR module AOUT GPIO 27 GPIO 27 / physical pin 32 ADC light input from module
Green LED anode GPIO 16 through 220 Ω resistor GPIO 16 / physical pin 21 Low-stress indicator
Yellow LED anode GPIO 17 through 220 Ω resistor GPIO 17 / physical pin 22 Moderate-stress indicator
Red LED anode GPIO 18 through 220 Ω resistor GPIO 18 / physical pin 24 High-stress indicator

Wiring Diagram

Circuit Diagram

  Raspberry Pi Pico 2 W
  ┌─────────────────────┐
  │                     │
  │  GPIO 26 ───────────┤──── Soil Sensor AOUT
  │  GPIO 27 ───────────┤──── LDR Module AOUT
  │                     │
  │  GPIO 16 ────220Ω───┤──── Green LED (+)
  │  GPIO 17 ────220Ω───┤──── Yellow LED (+)
  │  GPIO 18 ────220Ω───┤──── Red LED (+)
  │                     │
  │  3.3V    ───────────┤──── Soil Sensor VCC
  │  3.3V    ───────────┤──── LDR Module VCC
  │                     │
  │  GND     ───────────┤──── Soil Sensor GND
  │  GND     ───────────┤──── LDR Module GND
  │  GND     ───────────┤──── Green LED (-)
  │  GND     ───────────┤──── Yellow 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. Identify VCC, GND, and AOUT / AO / Signal before wiring.

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: Connect the LDR Module

Place the LDR module where it can sense the light level. Connect LDR module VCC to 3.3V. Connect LDR module AOUT to GPIO 27. Connect LDR module GND to GND.

Step 5: Place the Three LEDs

Place the green, yellow, and red LEDs on the breadboard. For each LED, identify the long leg as the anode (+) and the short leg as the cathode (-).

Step 6: Connect the Green LED

Connect the green LED long leg through a 220Ω resistor to GPIO 16. Connect the green LED short leg to GND.

Step 7: Connect the Yellow LED

Connect the yellow LED long leg through a 220Ω resistor to GPIO 17. Connect the yellow LED short leg to GND.

Step 8: Connect the Red LED

Connect the red LED long leg through a 220Ω resistor to GPIO 18. Connect the red LED short leg to GND.

Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
  • [x] LDR divider node connects to GPIO 27
  • [x] 10kΩ resistor connects from GPIO 27 to GND
  • [x] Green LED long leg connects through a 220Ω resistor to GPIO 16
  • [x] Yellow LED long leg connects through a 220Ω resistor to GPIO 17
  • [x] Red LED long leg connects through a 220Ω resistor to GPIO 18
  • [x] All LED short legs connect to GND
  • [x] No loose jumper wires

Intermediate Note Test dry soil, wet soil, bright light, and shade before interpreting stress states. Keep the LDR module AOUT from being shorted to 3.3V or GND.

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 two analog sensor paths carefully so both percentages are stable
  • Keep the LDR module AOUT secure because loose wires create false stress readings

Test the Input Sensors

  • Calibrate the soil sensor with dry and wet references
  • Cover and uncover the LDR to confirm that the light percentage changes clearly

Test the Output Devices

  • Test the three LEDs separately before running the full stress-scoring code
  • Confirm that only one stress-state LED should be on at a time

Run the Full System

  • Create one stress factor at a time, then try combinations such as dry soil plus low light
  • Compare the reported factors and overall stress state with the actual conditions you created

Save the Project

  • Save the final code and note the moisture and light calibration values used in your test
  • Record which stress combinations produced the most severe score

Additional Testing and Calibration Checks

Calibration and tuning notes

  • Test the soil and light factors separately before combining them
  • If the light readings seem backward, check the LDR module orientation and the bright/dark calibration values
  • Adjust the stress thresholds only after the percentages are calibrated and believable
  • Use the factor list, not just the LED colour, when deciding whether the score makes sense

Quick testing checklist

  • [ ] Soil percentage changes between dry and wet conditions
  • [ ] Light percentage changes between bright and shaded conditions
  • [ ] Stress factors are listed correctly
  • [ ] Only one status LED is active at a time
  • [ ] Combined stress conditions raise the score more than single stress conditions

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, Pin
import time

soil = ADC(26)
light = ADC(27)

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

soil_dry = 52000
soil_wet = 22000
light_dark = 8000
light_bright = 60000


def clamp(value, low, high):
    if value < low:
        return low
    if value > high:
        return high
    return value


def soil_percent():
    raw = soil.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 light_percent():
    raw = light.read_u16()
    span = light_bright - light_dark
    if span == 0:
        return 0
    percent = int(((raw - light_dark) * 100) / span)
    return clamp(percent, 0, 100)


def stress_report(moisture, light_level):
    score = 0
    factors = []

    if moisture < 30:
        score += 45
        factors.append("dry soil")
    elif moisture > 85:
        score += 25
        factors.append("possible overwatering")

    if light_level < 20:
        score += 35
        factors.append("low light")
    elif light_level > 90:
        score += 20
        factors.append("light too strong")

    if score >= 60:
        state = "HIGH STRESS"
    elif score >= 30:
        state = "MODERATE STRESS"
    else:
        state = "LOW STRESS"

    return score, state, factors


def set_leds(state):
    green_led.value(1 if state == "LOW STRESS" else 0)
    yellow_led.value(1 if state == "MODERATE STRESS" else 0)
    red_led.value(1 if state == "HIGH STRESS" else 0)


print("Plant stress detection ready")

while True:
    moisture = soil_percent()
    light_level = light_percent()
    score, state, factors = stress_report(moisture, light_level)
    set_leds(state)

    print("Moisture:{}% Light:{}% Score:{} State:{}".format(moisture, light_level, score, state))
    print("Factors:", ", ".join(factors) if factors else "none")

    time.sleep(3)

How the Code Works

Code Section What It Does Why It Matters
soil_percent() and light_percent() Convert analog inputs into calibrated percentages Stress scoring is easier to reason about than raw ADC values
stress_report() Builds a stress score and factor list The student can see why the system called the plant stressed
set_leds() Maps the score state to the LEDs Visual outputs make stress levels easy to compare
Factor-specific messages Explain whether stress came from dry soil, low light, or both A score alone is not enough for troubleshooting

Expected Result

The system should show low stress with the green LED when conditions are acceptable, moderate stress with the yellow LED for one significant problem, and high stress with the red LED when strong or combined stress factors are present. The Shell should explain which factors caused the stress score.


Troubleshooting

Problem Possible cause Solution
The light stress is always high The LDR module is wired incorrectly or poorly calibrated Recheck the divider and capture fresh dark and bright reference values
The stress score never rises Thresholds are too lenient Tighten the moisture and light thresholds after verifying calibration
Multiple LEDs appear wrong LED wiring or state logic is mixed up Check the GPIO assignments and confirm only one LED is selected in set_leds()
The factors list does not match the test The scoring rules are too simple for the condition created Review the rule logic and test one factor at a time