Skip to content

Project 2.6.26: Smart Plant Watering Assistant

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


Overview

This project builds a plant watering assistant that reminds the user when soil has stayed dry for several readings in a row.

Students will calibrate a soil moisture sensor, add reminder logic with confirmation counts, and use an acknowledge button so the alert does not beep constantly.

The final system should classify the soil as OK, WATCH, or WATER NEEDED, light the correct LED, and sound a reminder only when the dry condition is confirmed.

Required Components

Soil moisture sensor (analog output)
Soil moisture sensor (analog output)
Active buzzer
Active buzzer
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Push button
Push button
Green LED and 220 Ω resistor
Green LED and 220 Ω resistor
Red LED and 220 Ω resistor
Red LED and 220 Ω resistor
Breadboard and jumper wires
Breadboard and jumper wires

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Soil sensor VCC 3.3V Physical pin 36 Sensor power
Soil sensor GND GND Any GND pin Common ground
Soil sensor AOUT GPIO 26 GPIO 26 / physical pin 31 Analog moisture input
Push button one side GPIO 5 GPIO 5 / physical pin 7 Uses internal pull-up
Push button other side GND Any GND pin Pressing pulls the input low
Green LED anode GPIO 16 through 220 Ω resistor GPIO 16 / physical pin 21 Healthy soil indicator
Red LED anode GPIO 17 through 220 Ω resistor GPIO 17 / physical pin 22 Dry-soil reminder indicator
Buzzer positive GPIO 18 GPIO 18 / physical pin 24 Reminder output
LED cathodes and buzzer negative GND Any GND pin Return path for outputs

Wiring Diagram

Circuit Diagram

  Raspberry Pi Pico 2 W
  ┌─────────────────────┐
  │                     │
  │  GPIO 26 ───────────┤──── Soil Sensor AOUT
  │  GPIO 5  ───────────┤──── Push Button ──── GND
  │                     │
  │  GPIO 16 ────220Ω───┤──── Green LED (+)
  │  GPIO 17 ────220Ω───┤──── Red LED (+)
  │  GPIO 18 ───────────┤──── Buzzer (+)
  │                     │
  │  3.3V    ───────────┤──── Soil Sensor VCC
  │                     │
  │  GND     ───────────┤──── Soil Sensor GND
  │  GND     ───────────┤──── Green LED (-)
  │  GND     ───────────┤──── Red LED (-)
  │  GND     ───────────┤──── Buzzer (-)
  │  GND     ───────────┤──── Push Button GND
  │                     │
  └─────────────────────┘

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 plant 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: Place the Acknowledge Button

Place the push button across the breadboard center gap. Connect one side of the button to GPIO 5. Connect the opposite side of the button to GND. The code uses the Pico internal pull-up, so the button reads LOW when pressed.

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.

Step 7: Place and Connect the Buzzer

Place the buzzer on the breadboard and identify its positive (+) and negative (-) pins. Connect the buzzer positive pin to GPIO 18. Connect the buzzer negative pin 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] Acknowledge button connects between GPIO 5 and GND
  • [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] Buzzer positive pin connects to GPIO 18
  • [x] Buzzer negative pin connects to GND
  • [x] No loose jumper wires

Intermediate Note Calibrate SOIL_DRY and SOIL_WET before final use. The reminder is confirmed over several readings, so one noisy reading should not immediately trigger the buzzer.

Safety Note Do not pour water onto the Pico, breadboard, USB cable, or jumper wires. Keep the electronics away from the plant pot while testing.


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 soil sensor and LED wiring first, then add the button and buzzer
  • Keep the Pico on a dry surface while the soil probe is placed in the pot

Test the Input Sensor

  • Read raw soil values in dry soil and wet soil before finalising SOIL_DRY and SOIL_WET
  • Press the acknowledge button and confirm that the input changes cleanly in the Thonny Shell

Test the Output Device

  • Force the LEDs and buzzer on briefly to confirm the output wiring
  • Check that the buzzer stops after the reminder pulse instead of staying on continuously

Test Communication

  • Watch the Thonny Shell and confirm that the program prints moisture percentage, dry hit count, and reminder state
  • Verify that acknowledging the reminder is reflected in the printed status

Run the Full System

  • Place the sensor in plant soil and let the system sample several times so the confirmation counter can work correctly
  • Dry the soil gradually or test with a dry sample to confirm that the reminder only appears after repeated dry readings

Save the Project

  • Save the final program and record the dry threshold that matched your plant and soil type
  • Write down whether the reminder cooldown and acknowledge button reduced alert fatigue

Additional Testing and Calibration Checks

  • Dry/wet reading test: record raw values in dry soil and wet soil before finalising the calibration points
  • Normal condition test: leave the probe in moist soil and confirm the system stays in OK or WATCH
  • Threshold condition test: move the probe into a drier sample and confirm the reminder appears only after CONFIRM_READS is reached
  • Output response test: confirm the green LED, red LED, and buzzer match the printed status
  • Calibration note: different soil types behave differently, so the dry threshold may need retuning for each plant

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_sensor = ADC(26)
ack_button = Pin(5, Pin.IN, Pin.PULL_UP)
green_led = Pin(16, Pin.OUT)
red_led = Pin(17, Pin.OUT)
buzzer = Pin(18, Pin.OUT)

SOIL_DRY = 52000
SOIL_WET = 22000
DRY_THRESHOLD = 35
RECOVERY_THRESHOLD = 45
CONFIRM_READS = 3
REMINDER_COOLDOWN = 120
SAMPLE_DELAY = 5

dry_hits = 0
acknowledged = False
last_alert = time.time() - REMINDER_COOLDOWN


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 button_pressed():
    return ack_button.value() == 0


def beep(times=2, on_time=0.15, off_time=0.1):
    for _ in range(times):
        buzzer.value(1)
        time.sleep(on_time)
        buzzer.value(0)
        time.sleep(off_time)


print('=== Smart Plant Watering Assistant ===')
print('Calibrate SOIL_DRY and SOIL_WET before final use.\n')

while True:
    moisture = soil_percent()

    if moisture <= DRY_THRESHOLD:
        dry_hits += 1
    else:
        dry_hits = 0

    if moisture >= RECOVERY_THRESHOLD:
        acknowledged = False
        green_led.value(1)
        red_led.value(0)
        status = 'OK'
    elif moisture > DRY_THRESHOLD:
        green_led.value(1)
        red_led.value(1)
        status = 'WATCH'
    else:
        green_led.value(0)
        red_led.value(1)
        status = 'WATER NEEDED'

    if dry_hits >= CONFIRM_READS and not acknowledged:
        if time.time() - last_alert >= REMINDER_COOLDOWN:
            beep()
            last_alert = time.time()

    if button_pressed() and dry_hits >= CONFIRM_READS:
        time.sleep_ms(50)
        if button_pressed():
            acknowledged = True
            print('Reminder acknowledged. Soil is still dry until moisture recovers.')
            while button_pressed():
                time.sleep(0.05)

    print('Soil: {}% | Dry hits: {} | Acknowledged: {} | Status: {}'.format(
        moisture, dry_hits, acknowledged, status
    ))
    time.sleep(SAMPLE_DELAY)

How the Code Works

Code Section What It Does Why It Matters
SOIL_DRY and SOIL_WET Convert the raw ADC reading into a soil-moisture percentage Calibration makes the reminder match real soil conditions instead of arbitrary numbers
dry_hits counter Requires several dry readings before a true reminder is created This reduces false alerts caused by noisy or unstable sensor readings
Acknowledge button logic Lets the user silence repeated reminders after checking the plant This matches the human-in-the-loop purpose of the assistant
Recovery threshold Clears the reminder only after the moisture has improved enough Prevents the system from resetting too early on a barely changed reading

Expected Result

When the soil is healthy, the green LED should stay on and the Shell should report OK.

When the soil begins to dry, the system should move to WATCH before declaring a full watering reminder.

When the soil stays below the dry threshold for repeated readings, the red LED should turn on, the buzzer should pulse, and the Shell should report WATER NEEDED.


Troubleshooting

Problem Possible cause Solution
The reminder triggers too early The dry threshold is too high for your soil type Lower DRY_THRESHOLD after testing several real readings from your plant pot
The buzzer keeps repeating too often The reminder cooldown is too short or the button was not acknowledged Increase REMINDER_COOLDOWN or test the acknowledge button wiring
The button does nothing The button is not connected to GND correctly or the input is not using pull-up logic Recheck the GPIO 5 wiring and confirm the button pulls the input low when pressed
The soil reading never changes much The probe is not inserted well or the calibration values do not match the sensor Reinsert the probe and record new dry and wet readings for your actual soil sample