Skip to content

Project 3.9.73: Smart Water Sustainability Ai

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

Overview

The Pico uses rule-based AI-style scoring to combine tank level and water flow into a local sustainability status.

Water decisions are hard to explain when people look only at current flow or only at stored water level instead of both together.

A Pico 2 W prototype with a pulse flow sensor, analog water-level sensor, and OLED sustainability dashboard.

Water-usage interpretation, storage awareness, and honest AI-style sustainability scoring.

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
OLED
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
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 sustainability ai

Flow pulse output -> GPIO 14
Level analog output -> GPIO 27
GPIO 20 -> OLED SDA
GPIO 21 -> OLED SCL
Common GND -> flow sensor, level sensor, and OLED

Diagram Placeholder [Insert wiring diagram or breadboard image here]


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 level sensor analog output to GPIO 27 through a safe ADC path.
  3. Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
  4. Upload ssd1306.py and confirm the library appears on the Pico before running the dashboard.
  5. Record low and high level reference readings before trusting the displayed percentage.

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 level sensor separately so both values have believable calibration references.
  3. Test the output device: Run an OLED hello-world test before adding the sustainability scoring logic.
  4. Test the decision logic: Change flow and level one factor at a time so you can confirm why the score moves.
  5. Run the full system: Run the full dashboard and compare the displayed label with the printed score details.
  6. Validate the prototype: Discuss whether the scoring feels fair for short spikes in flow versus long periods of low storage.
  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.

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
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
LEVEL_EMPTY = 12000
LEVEL_FULL = 52000
SAMPLE_WINDOW_S = 5

pulse_count = 0
flow = Pin(FLOW_PIN, Pin.IN, Pin.PULL_UP)
level = ADC(LEVEL_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 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 sustainability AI 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)

    flow_lpm = flow_lpm_from_pulses(pulse_count, SAMPLE_WINDOW_S)
    level_pct = level_percent(level.read_u16())

    score = 0
    if flow_lpm >= 2.0:
        score += 2
    if flow_lpm >= 4.0:
        score += 2
    if level_pct <= 40:
        score += 2
    if level_pct <= 20:
        score += 2

    if score >= 5:
        label = 'CRITICAL'
    elif score >= 2:
        label = 'MONITOR'
    else:
        label = 'SUSTAINABLE'

    print('Flow:{:.2f}L/min Level:{}% Score:{} Label:{}'.format(flow_lpm, level_pct, score, label))

    if HAS_OLED:
        oled.fill(0)
        oled.text('Water AI', 0, 0)
        oled.text('Flow:{:.1f}L'.format(flow_lpm), 0, 18)
        oled.text('Level:{}%'.format(level_pct), 0, 34)
        oled.text(label[: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
Pulse and level conversion Turns raw pulses and ADC values into liters per minute and a storage percentage. Interpretation improves when raw signals become meaningful engineering units. Adjust the level calibration constants after measuring your own hardware.
Sustainability score Adds points for high demand and low storage. This is the explainable AI-style logic that turns two measurements into one sustainability decision. Change the score thresholds only after repeated tests with known conditions.
OLED dashboard Shows flow, level, and the sustainability label on one local screen. The user can see the decision and the measurements that caused it. Verify ssd1306.py and the I2C wiring if the screen is blank.

Expected Result

The OLED and serial monitor show live flow, storage level, and a sustainability label.

Higher flow and lower storage increase the risk score and move the system from SUSTAINABLE to MONITOR or CRITICAL.

The scoring remains explainable because each threshold has a visible effect on the final label.

Validation Checks

  • Normal condition test: maintain moderate flow with a high storage level and confirm the label stays SUSTAINABLE.
  • Warning condition test: increase flow or lower the storage level and confirm the label changes to MONITOR.
  • Critical condition test: combine high flow with low storage and confirm the label becomes CRITICAL.
  • Flow validation test: compare the flow estimate with a measured container fill during a timed trial.
  • Level calibration test: record low and high tank readings and confirm the percentage behaves as expected.

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
Flow stays at zero The pulse output is not reaching GPIO 14 Print the pulse count during a known flow event and verify the sensor signal path.
Level is always 0 or 100 The level calibration values are wrong Print the raw ADC value and re-measure the empty and full references.
The label is always critical The score thresholds are too strict for your test hardware Collect baseline flow and level data, then adjust the thresholds carefully.
OLED is blank The library or I2C wiring is wrong Confirm ssd1306.py is on the Pico and recheck GPIO 20 and GPIO 21.