Skip to content

Project 3.9.63: Smart Water Distribution Monitor

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

Overview

The Pico measures flow and line pressure, then shows whether a small water-distribution branch looks normal, restricted, or leak-prone.

Water systems are harder to troubleshoot when flow and pressure are checked separately instead of being interpreted together.

A Pico 2 W prototype with a pulse flow sensor, analog pressure sensor, and OLED dashboard for water-distribution status.

Multi-sensor interpretation, flow calibration, pressure mapping, and local infrastructure diagnostics.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Water level sensor
Water level sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
Water flow sensor
Water flow sensor
Push button
Push button
OLED
OLED
---

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Flow sensor pulse output GPIO 14 GPIO 14 / physical pin 19 Use a 3.3V-safe pulse line
Pressure sensor analog output GPIO 27 ADC1 GPIO 27 / physical pin 32 Relative pressure input
OLED SDA GPIO 20 GPIO 20 / physical pin 26 I2C0 SDA
OLED SCL GPIO 21 GPIO 21 / physical pin 27 I2C0 SCL
---

Wiring Diagram

Smart Water Distribution Monitor

  Flow pulse output -> GPIO 14
  Pressure sensor analog output -> GPIO 27
  GPIO 20 -> OLED SDA
  GPIO 21 -> OLED SCL
  Common GND -> all modules and OLED

Step-by-Step Assembly

  1. Connect the flow sensor pulse output to GPIO 14 and confirm the signal is 3.3V-safe.
  2. Connect the pressure sensor analog output to GPIO 27 through a safe analog path.
  3. Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
  4. Upload ssd1306.py and confirm it appears in os.listdir() before running the dashboard.
  5. Prepare a low-risk test setup so you can vary flow and pressure separately during validation.

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: Test the flow pulses and the pressure ADC separately so you know each subsystem responds to physical changes.
  3. Test the output device: Run an OLED hello-world test before adding the hydraulic logic.
  4. Test the decision logic: Change the flow and pressure conditions and confirm the diagnostic label changes for the right reason.
  5. Run the full system: Run the full dashboard and compare the live values with the diagnosed state.
  6. Validate the prototype: Repeat the test with different calibration points and note how sensitive the diagnosis becomes.
  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, I2C, Pin
import time

try:
    from ssd1306 import SSD1306_I2C
    HAS_OLED = True
except ImportError:
    HAS_OLED = False

FLOW_PIN = 14
PRESSURE_PIN = 27
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
PRESSURE_EMPTY = 5000
PRESSURE_FULL = 54000
SAMPLE_WINDOW_S = 5

pulse_count = 0
flow = Pin(FLOW_PIN, Pin.IN, Pin.PULL_UP)
pressure = ADC(PRESSURE_PIN)
i2c = I2C(0, sda=Pin(I2C_SDA_PIN), scl=Pin(I2C_SCL_PIN), freq=400000)
if HAS_OLED:
    oled = SSD1306_I2C(128, 64, i2c)


def count_pulse(pin):
    global pulse_count
    pulse_count += 1


def clamp(value, low, high):
    return max(low, min(high, value))


def pressure_percent(raw):
    span = PRESSURE_FULL - PRESSURE_EMPTY
    if span <= 0:
        return 0
    pct = int((raw - PRESSURE_EMPTY) * 100 / span)
    return clamp(pct, 0, 100)


def flow_lpm_from_pulses(pulses, seconds):
    if seconds <= 0:
        return 0.0
    return (pulses / seconds) / 7.5


flow.irq(trigger=Pin.IRQ_FALLING, handler=count_pulse)
print('Water distribution monitor ready')

while True:
    pulse_count = 0
    start = time.ticks_ms()
    while time.ticks_diff(time.ticks_ms(), start) < SAMPLE_WINDOW_S * 1000:
        time.sleep_ms(100)

    pulses = pulse_count
    flow_lpm = flow_lpm_from_pulses(pulses, SAMPLE_WINDOW_S)
    pressure_raw = pressure.read_u16()
    pressure_pct = pressure_percent(pressure_raw)

    if pressure_pct >= 65 and flow_lpm < 0.8:
        status = 'BLOCKAGE'
    elif pressure_pct <= 20 and flow_lpm < 0.5:
        status = 'LOW SUPPLY'
    elif pressure_pct <= 25 and flow_lpm > 3.0:
        status = 'LEAK RISK'
    else:
        status = 'NORMAL'

    print('Flow:{:.2f}L/min Pressure:{}% Status:{}'.format(
        flow_lpm, pressure_pct, status))

    if HAS_OLED:
        oled.fill(0)
        oled.text('Water Branch', 0, 0)
        oled.text('Flow:{:.1f}L'.format(flow_lpm), 0, 16)
        oled.text('P:{}%'.format(pressure_pct), 0, 30)
        oled.text(status[:16], 0, 46)
        oled.show()

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Pulse counting Counts flow-sensor pulses during a fixed sample window The flow calculation depends on stable counting Verify the pulse line before changing any diagnostic thresholds
pressure_percent Maps the raw pressure ADC value into a relative percentage A relative pressure value is easier to interpret than raw ADC counts Adjust the empty and full references after measuring your real sensor
Diagnostic rules Compares flow and pressure together to identify branch-health states This is what makes the project more advanced than two isolated readings Tune the thresholds only after you observe real test conditions
OLED dashboard Displays the current hydraulic state locally A local dashboard helps students validate the diagnosis in real time If the display is blank, verify ssd1306.py and the I2C wiring first

Expected Result

The OLED and serial monitor show live flow and pressure values together with a branch-health label.

High pressure with weak flow produces a blockage-style warning.

Low pressure with very low flow produces a low-supply warning, while low pressure with strong flow suggests leak risk.

Validation Checks

  • Normal condition test: run a moderate stable flow and confirm the monitor reports NORMAL
  • Warning condition test: reduce outlet flow while pressure remains high and confirm the BLOCKAGE label appears
  • Critical interpretation test: create a low-pressure condition and confirm the monitor distinguishes LOW SUPPLY from LEAK RISK
  • Sensor reading validation: compare the flow estimate with a measured container fill and check that pressure changes move the relative percentage
  • Calibration test: record empty and high-pressure analog values before finalizing the pressure mapping
  • Limitation test: explain why this prototype cannot replace a certified plumbing or utility instrument

Deployment and Limitations

  • This prototype is useful for classroom water-network demonstrations and maintenance training
  • Before deployment, the pressure-sensor scaling, enclosure, and hydraulic safety setup need improvement
  • Field use would also need better plumbing integration, more calibration, and stronger water protection for the electronics

Troubleshooting

Problem Possible Cause Solution
Flow stays at zero The pulse output is not reaching GPIO 14 Print the pulse count during a known flow event and verify the flow-sensor signal path
Pressure percentage never changes The analog pressure path is wrong or the calibration constants are unrealistic Print the raw ADC value and re-measure the reference points
Status is always blockage The flow threshold is too strict for your test hardware Collect real baseline values and adjust the blockage threshold
OLED is blank The display library or I2C wiring is wrong Use GPIO 20 and GPIO 21 and confirm ssd1306.py is on the Pico