Skip to content

Project 3.1.23: Predictive Water Usage System

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


Overview

The Pico counts flow-sensor pulses, estimates short-term usage, and projects expected daily water use from recent history.

Water systems are easier to manage when people can see usage trends before tanks empty or waste becomes severe.

A Pico 2 W prototype with a pulse flow sensor and warning LED used for trend-based water-use projection.

Pulse counting, rolling averages, prediction limits, and honest data interpretation without overclaiming forecasting accuracy.

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

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 digital signal
LED anode 220 ohm resistor to GPIO 15 GPIO 15 / physical pin 20 High-use warning LED

Wiring Diagram

Predictive Water Usage System wiring diagram

  Flow sensor pulse output -> GPIO 14
  GPIO 15 -> 220 ohm resistor -> LED anode
  Sensor power -> module-rated supply with common ground

Step-by-Step Assembly

  1. Power the flow sensor from its rated supply and confirm the pulse line is 3.3V-safe before connecting it to GPIO 14.
  2. Connect the flow pulse output to GPIO 14 and the sensor ground to Pico GND.
  3. Connect the LED to GPIO 15 through a 220 ohm resistor.
  4. Prepare a measured container so you can calibrate pulses per liter 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: Count raw pulses during a short timed flow test and compare them with a measured container volume.
  3. Test the output device: Blink the LED from a short script so the high-usage indicator path is verified.
  4. Test the decision logic: Confirm that the projected daily usage increases only when the rolling history window increases.
  5. Run the full system: Run repeated flow intervals and compare the printed usage numbers with your manual measurement.
  6. Validate the prototype: Change the sample window and observe how a shorter or longer window affects prediction 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 Pin
import time

FLOW_PIN = 14
LED_PIN = 15
PULSES_PER_LITER = 450.0
SAMPLE_WINDOW_S = 10
HISTORY_LENGTH = 12
DAILY_LIMIT_L = 120.0

pulse_count = 0
history_lph = []
flow = Pin(FLOW_PIN, Pin.IN, Pin.PULL_UP)
led = Pin(LED_PIN, Pin.OUT)


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


flow.irq(trigger=Pin.IRQ_FALLING, handler=count_pulse)
print('Predictive water usage system 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
    liters = pulses / PULSES_PER_LITER
    liters_per_hour = liters * (3600 / SAMPLE_WINDOW_S)
    history_lph.append(liters_per_hour)
    history_lph = history_lph[-HISTORY_LENGTH:]
    average_lph = sum(history_lph) / len(history_lph)
    predicted_daily = average_lph * 24

    if predicted_daily > DAILY_LIMIT_L:
        status = 'HIGH_USE'
        led.on()
    elif predicted_daily > DAILY_LIMIT_L * 0.7:
        status = 'WATCH'
        led.on()
    else:
        status = 'NORMAL'
        led.off()

    print('Pulses:{} WindowL:{:.2f} AvgLph:{:.2f} PredDay:{:.1f} Status:{}'.format(
        pulses,
        liters,
        average_lph,
        predicted_daily,
        status,
    ))

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Interrupt counting Counts each falling pulse from the flow sensor Pulse sensors must be counted accurately or every later calculation is wrong Verify the signal level and pulse integrity before changing the math
Volume conversion Converts pulse totals into estimated liters for one sample window The whole prediction depends on this calibration factor Measure your own sensor with a known volume before changing PULSES_PER_LITER
Rolling average Keeps a short history of hourly-equivalent usage rates A prediction should reflect recent behavior instead of one noisy moment Increase HISTORY_LENGTH for smoother output or decrease it for faster response
Usage status Compares the projected daily use to a chosen limit and drives the LED A simple alert makes the projection actionable during testing Choose a DAILY_LIMIT_L that matches your test scenario

Expected Result

The serial monitor prints pulses, estimated water volume, average usage rate, and projected daily usage.

The LED lights when the projected daily usage rises above the selected warning level.

The projection changes more smoothly after several windows than it does from one single measurement.

Validation Checks

  • Normal condition test: run a low, steady flow and confirm the projected daily use stays in NORMAL
  • Warning condition test: increase the flow rate and confirm the system enters WATCH before HIGH_USE
  • Critical condition test: run a strong flow and confirm the projection rises above the daily limit
  • Sensor reading validation: compare the measured container volume with the code estimate for the same timed interval
  • Calibration test: repeat the same fill test several times and refine PULSES_PER_LITER until the estimate is reasonably close
  • Limitation test: explain why a short rolling average can overreact to temporary spikes or underrepresent long-term demand

Deployment and Limitations

  • This prototype is useful for classroom water studies and local demand demonstrations
  • Before deployment, the flow calibration, enclosure, and long-term logging strategy need improvement
  • Real usage forecasting would also need more historical data, more stable timing, and maintenance checks for sensor wear

Troubleshooting

Problem Possible Cause Solution
No pulses are counted The flow signal is not reaching GPIO 14 or the sensor is not flowing Print the pulse count during a known flow event and verify the wiring and supply
Estimated liters are very wrong The pulses-per-liter constant does not match your sensor Calibrate with a known container and update PULSES_PER_LITER
LED is always on The daily limit is too low for the test setup Raise DAILY_LIMIT_L or reduce the test flow rate
Prediction jumps too much The sample window or history length is too short Increase SAMPLE_WINDOW_S or HISTORY_LENGTH to smooth the trend