Project 3.9.70: Smart Water Policy Simulation System
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico lets the user cycle through water-policy modes and compares how each mode interprets the same flow and storage conditions.
Water policy trade-offs stay abstract when learners never test how different rules would change the same physical situation.
A Pico 2 W prototype with a flow sensor, water-level sensor, mode button, and OLED policy-simulation dashboard.
Policy-mode comparison, human-in-the-loop testing, and clear documentation of decision trade-offs.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Water level sensor |
![]() Breadboard |
![]() Jumper wires |
![]() Water flow sensor |
![]() OLED |
![]() Push button |
|
| --- |
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 | Mode 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 the signal is 3.3V-safe.
- Connect the analog level sensor output to GPIO 27 through a safe ADC path.
- Wire the mode 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 simulator.
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 button inputs separately so the policy recommendation is driven by trusted values.
- Test the output device: Run an OLED hello-world test before adding policy logic.
- Test the decision logic: Cycle through policies and confirm one button press changes one mode only.
- Run the full system: Run the full simulator and compare how each policy interprets the same flow and storage condition.
- Validate the prototype: Discuss what additional variables a real water-policy model would still need.
- 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
POLICIES = ['CONSERVE', 'BALANCED', 'PRIORITY']
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)
policy_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)
def recommend(policy, flow_lpm, level_pct):
if policy == 'CONSERVE':
if flow_lpm > 2.0 or level_pct < 50:
return 'RESTRICT'
elif policy == 'BALANCED':
if flow_lpm > 3.5 or level_pct < 30:
return 'MONITOR'
else:
if level_pct < 15:
return 'EMERGENCY'
return 'ALLOW'
flow.irq(trigger=Pin.IRQ_FALLING, handler=count_pulse)
print('Smart water policy simulation system ready')
while True:
button_now = button.value()
if button_now == 1 and last_button == 0:
policy_index = (policy_index + 1) % len(POLICIES)
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())
policy = POLICIES[policy_index]
action = recommend(policy, flow_lpm, level_pct)
print('Policy:{} Flow:{:.2f} Level:{} Action:{}'.format(policy, flow_lpm, level_pct, action))
if HAS_OLED:
oled.fill(0)
oled.text('Water Policy', 0, 0)
oled.text(policy[:16], 0, 16)
oled.text('F:{:.1f} L:{}%'.format(flow_lpm, level_pct), 0, 32)
oled.text(action[:16], 0, 50)
oled.show()
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Policy cycling | Uses one debounced button press to move through the named policy modes. | Simulation is only useful if the user can reliably compare different rules. | Print the button state if one press skips multiple modes. |
| Measurement conversion | Turns flow pulses and level ADC values into interpretable values. | Policy rules need meaningful inputs rather than raw counts. | Update the level calibration and flow factor after measuring your hardware. |
| recommend | Applies a different rule set for each policy mode. | This is the core simulation layer promised by the project title. | Adjust one policy at a time so the trade-offs remain easy to explain. |
Expected Result
The OLED and serial monitor show the active policy, measured flow, storage level, and resulting action.
Pressing the button cycles between CONSERVE, BALANCED, and PRIORITY SERVICE style behavior.
The same physical condition can produce a different recommendation when the policy mode changes.
Validation Checks
- Normal condition test: use moderate flow and healthy storage, then compare how each policy responds.
- Button validation test: confirm one press changes one mode only.
- Policy comparison test: keep the same physical condition while cycling policies and note the different outputs.
- Calibration test: measure low and high level references and verify the displayed percentage is believable.
- Limitation test: list the important real-world variables this local simulation still ignores.
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 mode changes too quickly | The button is bouncing or being held too long | Use edge detection as shown and test with single deliberate presses. |
| Flow stays at zero | The pulse line is not reaching GPIO 14 | Print the pulse count during a known flow event and verify the sensor wiring. |
| Level never changes | The analog level path or calibration is wrong | Print the raw ADC reading and recheck the level sensor wiring and reference values. |
| OLED is blank | The display library or I2C wiring is wrong | Confirm ssd1306.py is on the Pico and verify GPIO 20 and GPIO 21. |






