Skip to content

Project 3.3.1: Drainage Detector

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


Overview

The Pico reads a water-level sensor and warns when drainage water rises into warning or critical ranges.

Blocked or overloaded drains need early local alerts before overflow causes flooding or sanitation failure.

A Pico 2 W prototype with a water-level sensor, LED, and buzzer for drainage warning.

Analog thresholding, local alert levels, and careful interpretation of water-sensor calibration values.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
water level sensor
water level sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
Buzzer
Buzzer

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Water sensor VCC GPIO 17 GPIO 17 / physical pin 22 Use GPIO power to reduce sensor corrosion between samples
Water sensor GND GND Physical pin 38 Common ground
Water sensor AO GPIO 26 ADC0 GPIO 26 / physical pin 31 Analog water reading
LED anode 220 ohm resistor to GPIO 15 GPIO 15 / physical pin 20 Warning LED
Buzzer positive GPIO 16 GPIO 16 / physical pin 21 Critical buzzer

Wiring Diagram

Drainage Detector wiring diagram

  GPIO 17              -> water sensor VCC
  GND                  -> water sensor GND, LED cathode, buzzer negative
  Water sensor AO      -> GPIO 26
  GPIO 15              -> 220 ohm resistor -> LED anode
  GPIO 16              -> buzzer positive

Step-by-Step Assembly

  1. Connect the water sensor ground to Pico GND and its analog output to GPIO 26.
  2. Connect the sensor VCC pin to GPIO 17 so the code can power the sensor only during readings.
  3. Connect the LED to GPIO 15 through a 220 ohm resistor and connect the LED cathode to GND.
  4. Connect the buzzer positive pin to GPIO 16 and the negative pin to GND.
  5. Keep the Pico and USB cable away from wet surfaces during drainage testing.

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.

  1. Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
  2. Test the input sensor: Read dry and wet sensor values first so the warning and critical thresholds are based on real measurements.
  3. Test the output device: Test the LED and buzzer outputs from a simple script before enabling the drainage logic.
  4. Test the decision logic: Confirm that the system enters WARNING before CRITICAL as water rises.
  5. Run the full system: Run the full system with repeated low, medium, and high water conditions and compare the raw values with the state labels.
  6. Validate the prototype: Test the effect of splashes, residue, or tilted probe placement on the reading stability.
  7. 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, Pin
import time

POWER_PIN = 17
WATER_PIN = 26
LED_PIN = 15
BUZZER_PIN = 16
WARNING_LEVEL = 22000
CRITICAL_LEVEL = 36000

power = Pin(POWER_PIN, Pin.OUT)
water = ADC(WATER_PIN)
led = Pin(LED_PIN, Pin.OUT)
buzzer = Pin(BUZZER_PIN, Pin.OUT)


def read_level(samples=6):
    power.on()
    time.sleep_ms(50)
    total = 0
    for _ in range(samples):
        total += water.read_u16()
        time.sleep_ms(10)
    power.off()
    return total // samples


print("Drainage detector ready")

while True:
    level = read_level()

    if level >= CRITICAL_LEVEL:
        status = "CRITICAL"
        led.on()
        buzzer.on()
    elif level >= WARNING_LEVEL:
        status = "WARNING"
        led.on()
        buzzer.off()
    else:
        status = "NORMAL"
        led.off()
        buzzer.off()

    print("Water level: {} | Status: {}".format(level, status))
    time.sleep(1)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Sensor power control Powers the water sensor only during sampling Shorter powered time can reduce corrosion on simple water probes If your sensor needs continuous power, change only the power strategy after testing
read_level Averages several ADC readings for a more stable water value Analog water probes are noisy and sensitive to placement Adjust the sample count if the reading still jumps too much
Threshold logic Separates NORMAL, WARNING, and CRITICAL water conditions Multiple states make it easier to respond before overflow becomes severe Tune WARNING_LEVEL and CRITICAL_LEVEL with real wet tests
Output control Turns the LED and buzzer on according to the chosen water state Different outputs provide graded response instead of a single alarm only If the buzzer is too aggressive, keep it only for the CRITICAL state

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 can be used for local warning demonstrations and early field trials
  • Before deployment, it needs enclosure protection, calibration records, and a clear response procedure
  • Alert systems should always be treated as decision-support tools unless they are certified for the application

Troubleshooting

Problem Possible Cause Solution
Readings do not change Sensor is not powered or analog output is not connected Check GPIO 17 power wiring and the AO connection to GPIO 26
System is always in CRITICAL Thresholds are too low or the sensor is contaminated Clean the probe and recalibrate WARNING_LEVEL and CRITICAL_LEVEL
Buzzer never sounds Critical level is never reached or buzzer polarity is wrong Lower CRITICAL_LEVEL temporarily and verify the buzzer with a direct Pin test
Readings drift over time Probe residue or corrosion is affecting the sensor Dry the sensor after testing and review whether a more durable probe is needed