Skip to content

Project 3.3.2: Drainage Flow Monitor

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


Overview

The Pico counts flow-sensor pulses, calculates flow rate and total volume, and displays the result locally.

Water-use discussions are less meaningful when learners cannot see real-time flow and accumulated volume.

A Pico 2 W prototype with a water flow sensor, OLED display, and optional alert output.

Pulse-based flow measurement, calibration, and dashboard interpretation.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Water level sensor
Water level sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
Buzzer
Buzzer
OLED
OLED

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Flow sensor signal GPIO 14 GPIO 14 / physical pin 19 Pulse input with pull-up
OLED SDA GPIO 20 GPIO 20 / physical pin 26 I2C0 SDA
OLED SCL GPIO 21 GPIO 21 / physical pin 27 I2C0 SCL

Wiring Diagram

Circuit Diagram

  Flow sensor signal    -> GPIO 14
  GPIO 20               -> OLED SDA
  GPIO 21               -> OLED SCL
  Optional buzzer       -> GPIO 16
  Common GND            -> flow sensor, OLED, and optional buzzer

Step-by-Step Assembly

  1. Connect the flow sensor signal wire to GPIO 14 and power the sensor according to its rating.
  2. Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
  3. Upload ssd1306.py to the Pico and confirm it appears in os.listdir().
  4. If using the optional buzzer, connect it to GPIO 16 and ground.

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 pulses first with the serial monitor before trusting the volume calculation.
  3. Test the output device: Run an OLED hello-world script before integrating the flow dashboard.
  4. Test the decision logic: Pass a measured amount of water through the sensor and compare real volume with calculated volume.
  5. Run the full system: Run the full dashboard and check that the OLED matches the serial output.
  6. Validate the prototype: Adjust the calibration factor if the displayed total differs significantly from the measured volume.
  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 I2C, Pin
import time

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

FLOW_PIN = 14
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
PULSES_PER_LITER = 450
WINDOW_SECONDS = 5

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

pulse_count = 0
total_liters = 0.0

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

flow_sensor.irq(trigger=Pin.IRQ_FALLING, handler=count_pulse)

print('Flow dashboard ready')

while True:
    pulse_count = 0
    time.sleep(WINDOW_SECONDS)
    pulses = pulse_count
    liters = pulses / PULSES_PER_LITER
    total_liters += liters
    flow_lpm = liters * (60 / WINDOW_SECONDS)
    print('Pulses:{} Flow:{:.2f}L/min Total:{:.2f}L'.format(pulses, flow_lpm, total_liters))

    if HAS_OLED:
        oled.fill(0)
        oled.text('Flow Dash', 0, 0)
        oled.text('Rate:{:.1f}L/m'.format(flow_lpm), 0, 20)
        oled.text('Tot:{:.2f}L'.format(total_liters), 0, 40)
        oled.show()

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Pulse interrupt Counts each flow pulse over a measurement window Accurate counting is essential before any dashboard claim is trusted Compare the count against a known flow test
Volume calculation Converts pulses into liters and liters per minute This turns raw pulses into useful engineering data Adjust the calibration factor if your sensor model differs
Local dashboard Shows flow rate and total liters on the OLED The display makes the measurement easier to interpret live Verify the OLED library and wiring if the screen is blank

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 works well for classroom demonstrations and local monitoring pilots
  • Before deployment, it needs calibration records, protective mounting, and regular validation checks
  • Display-only systems still depend on correct sensor placement and trained users

Troubleshooting

Problem Possible Cause Solution
The flow rate stays at zero No pulses are being received Check the signal wire, pull-up input, and sensor output voltage level
The total volume drifts from reality The calibration factor is wrong Measure a known volume and adjust the pulses-per-liter constant
The OLED is blank The library is missing or the I2C pins are wrong Confirm ssd1306.py is present and use GPIO 20 and GPIO 21
Values jump around Flow is unstable or the sensor wiring is loose Secure the wiring and test with a steadier flow source