Project 3.9.65: Smart Water Governance Demo
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico demonstrates how the same water conditions can lead to different allocation advice for different service-priority groups.
Governance decisions remain abstract when students cannot compare how supply and demand affect different user priorities.
A Pico 2 W prototype with a flow sensor, water-level sensor, profile button, and OLED governance dashboard.
Priority-profile comparison, allocation logic, and honest local governance-demo framing.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Water level sensor |
![]() Breadboard |
![]() Jumper wires |
![]() Water flow sensor |
![]() Push button |
![]() OLED |
|
| --- |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Flow sensor pulse output | GPIO 14 | GPIO 14 / physical pin 19 | 3.3V-safe pulse input |
| Water level analog output | GPIO 27 ADC1 | GPIO 27 / physical pin 32 | Storage level input |
| Button leg 1 | GPIO 15 | GPIO 15 / physical pin 20 | Profile button input |
| Button leg 2 | 3.3V output | Physical pin 36 | High when pressed |
| OLED SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA |
| OLED SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
| --- |
Wiring Diagram

Flow pulse output -> GPIO 14
Level analog output -> GPIO 27
Button -> GPIO 15 and 3.3V
GPIO 20 -> OLED SDA
GPIO 21 -> OLED SCL
Common GND -> all sensors and OLED
Diagram Placeholder [Insert wiring diagram or breadboard image here]
Step-by-Step Assembly
- Connect the flow sensor pulse output to GPIO 14 and confirm it is 3.3V-safe.
- Connect the analog level sensor output to GPIO 27 through a safe ADC path.
- Wire the profile button between GPIO 15 and 3.3V so the input goes high when pressed.
- Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
- Upload ssd1306.py and confirm it appears on the Pico before running the governance demo.
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.
- Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
- Test the input sensor: Test the flow, level, and profile button inputs separately so the governance demo uses trusted values.
- Test the output device: Run an OLED hello-world test before adding profile logic.
- Test the decision logic: Press the button to change one profile at a time and confirm the same physical condition produces different advice.
- Run the full system: Run the full demo and compare how each profile interprets the same flow and supply state.
- Validate the prototype: Discuss which real-world governance factors are still missing from this local classroom model.
- 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.
Upload and run this code after the individual tests work correctly.
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
LEVEL_PIN = 27
BUTTON_PIN = 15
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
LEVEL_EMPTY = 12000
LEVEL_FULL = 52000
SAMPLE_WINDOW_S = 5
PROFILES = [('HOUSEHOLD', 1.0), ('SCHOOL', 1.2), ('CLINIC', 1.5)]
pulse_count = 0
flow = Pin(FLOW_PIN, Pin.IN, Pin.PULL_UP)
level = ADC(LEVEL_PIN)
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_DOWN)
i2c = I2C(0, sda=Pin(I2C_SDA_PIN), scl=Pin(I2C_SCL_PIN), freq=400000)
if HAS_OLED:
oled = SSD1306_I2C(128, 64, i2c)
profile_index = 0
last_button = 0
def count_pulse(pin):
global pulse_count
pulse_count += 1
def clamp(value, low, high):
return max(low, min(high, value))
def flow_lpm_from_pulses(pulses, seconds):
if seconds <= 0:
return 0.0
return (pulses / seconds) / 7.5
def level_percent(raw):
span = LEVEL_FULL - LEVEL_EMPTY
if span <= 0:
return 0
pct = int((raw - LEVEL_EMPTY) * 100 / span)
return clamp(pct, 0, 100)
flow.irq(trigger=Pin.IRQ_FALLING, handler=count_pulse)
print('Smart water governance demo ready')
while True:
button_now = button.value()
if button_now == 1 and last_button == 0:
profile_index = (profile_index + 1) % len(PROFILES)
last_button = button_now
pulse_count = 0
start = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), start) < SAMPLE_WINDOW_S * 1000:
time.sleep_ms(100)
flow_lpm = flow_lpm_from_pulses(pulse_count, SAMPLE_WINDOW_S)
level_pct = level_percent(level.read_u16())
profile_name, weight = PROFILES[profile_index]
pressure = flow_lpm * weight
if level_pct < 20 or pressure > 5.5:
action = 'PRIORITY'
elif level_pct < 40 or pressure > 3.0:
action = 'WATCH'
else:
action = 'NORMAL SHARE'
print('Profile:{} Flow:{:.2f} Level:{} Pressure:{:.2f} Action:{}'.format(profile_name, flow_lpm, level_pct, pressure, action))
if HAS_OLED:
oled.fill(0)
oled.text('Water Gov', 0, 0)
oled.text(profile_name[:16], 0, 16)
oled.text('F:{:.1f} L:{}%'.format(flow_lpm, level_pct), 0, 32)
oled.text(action[:16], 0, 50)
oled.show()
time.sleep(2)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Profile switching | Cycles through the service-priority profiles with one button. | Governance comparison only works when the user can reliably change profiles. | Use edge detection and deliberate presses so profile switching stays predictable. |
| Weighted pressure score | Multiplies flow demand by a profile weighting factor. | This demonstrates how governance priorities can change a decision without changing the physical measurements. | Adjust the profile weights only after you can explain why each priority group should be treated differently. |
| Allocation advice | Maps supply and weighted demand into NORMAL SHARE, WATCH, or PRIORITY. | The advice layer turns the demo into a discussion tool instead of a raw sensor exercise. | Tune the level and pressure thresholds after repeated comparison tests. |
Expected Result
The OLED and serial monitor show the active profile, current water measurements, and an allocation recommendation.
Pressing the button cycles through household, school, and clinic style priorities.
The same flow and storage condition can produce different advice depending on the active profile.
Validation Checks
- Normal condition test: use moderate flow with healthy storage and compare the result across all profiles.
- Profile validation test: confirm that one button press changes one profile only.
- Comparison test: keep the same physical condition and record how the action changes with each profile.
- Calibration test: measure empty and full level references so the displayed percentage is believable.
- Discussion test: identify which real-world governance factors this classroom model cannot represent.
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 profile changes too fast | The button is bouncing or being held too long | Press once deliberately and keep the edge-detection logic in place. |
| Flow remains at zero | The pulse line is not reaching GPIO 14 | Print the pulse count during a known flow event and verify the sensor wiring. |
| The action never changes | The thresholds or profile weights are too weak for your test conditions | Collect baseline values and adjust the weights or action thresholds carefully. |
| OLED is blank | The library or I2C wiring is wrong | Confirm ssd1306.py is present and verify GPIO 20 and GPIO 21. |






