Skip to content

Project 3.8.3: Water Safety Alarm

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


Overview

The Pico reads a turbidity or TDS-style analog input and warns when water quality moves beyond the chosen prototype threshold.

Water quality problems are difficult to notice without a visible or audible alert.

A Pico 2 W prototype with a water-quality proxy sensor, two LEDs, and a buzzer.

Analog thresholding, calibration, and honest interpretation of water-quality proxy data.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Turbidity sensor
Turbidity sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
Buzzer
Buzzer

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Water-quality sensor AO GPIO 26 ADC0 GPIO 26 / physical pin 31 Analog water-quality proxy input
Green LED anode 220 ohm resistor to GPIO 15 GPIO 15 / physical pin 20 Normal-state indicator
Red LED anode 220 ohm resistor to GPIO 16 GPIO 16 / physical pin 21 Warning or critical indicator
Buzzer positive GPIO 17 GPIO 17 / physical pin 22 Critical alarm

Wiring Diagram

Circuit Diagram

  Sensor AO             -> GPIO 26
  GPIO 15               -> 220 ohm resistor -> green LED anode
  GPIO 16               -> 220 ohm resistor -> red LED anode
  GPIO 17               -> buzzer positive
  Common GND            -> sensor, LEDs, and buzzer

Step-by-Step Assembly

  1. Connect the analog sensor output to GPIO 26 through a safe ADC path.
  2. Wire the green LED to GPIO 15 and the red LED to GPIO 16 through 220 ohm resistors.
  3. Connect the buzzer to GPIO 17.
  4. Place only the probe or sample interface in the water sample area and keep the Pico dry.

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: Measure the raw analog value for at least two known sample conditions before setting the thresholds.
  3. Test the output device: Test the LEDs and buzzer outputs separately.
  4. Test the decision logic: Switch between clearer and dirtier sample conditions and confirm the system changes state logically.
  5. Run the full system: Run the full system and compare the printed reading with the output behavior.
  6. Validate the prototype: Discuss why this proxy measurement is not enough for certified water safety decisions.
  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 ADC, Pin
import time

SENSOR_PIN = 26
GREEN_LED_PIN = 15
RED_LED_PIN = 16
BUZZER_PIN = 17
WARNING_LEVEL = 26000
CRITICAL_LEVEL = 42000

sensor = ADC(SENSOR_PIN)
green_led = Pin(GREEN_LED_PIN, Pin.OUT)
red_led = Pin(RED_LED_PIN, Pin.OUT)
buzzer = Pin(BUZZER_PIN, Pin.OUT)

print('Water safety alarm ready')

while True:
    reading = sensor.read_u16()
    if reading >= CRITICAL_LEVEL:
        state = 'CRITICAL'
    elif reading >= WARNING_LEVEL:
        state = 'WARNING'
    else:
        state = 'NORMAL'

    green_led.value(1 if state == 'NORMAL' else 0)
    red_led.value(1 if state in ('WARNING', 'CRITICAL') else 0)
    buzzer.value(1 if state == 'CRITICAL' else 0)
    print('Reading:{} State:{}'.format(reading, state))
    time.sleep(1)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Analog thresholding Maps the proxy reading into NORMAL, WARNING, or CRITICAL Thresholds are the basis of the interpretation in this prototype Use measured sample values instead of guessing thresholds
Staged indicators Uses LEDs and the buzzer to reflect the chosen state Layered outputs help distinguish mild and strong warning conditions Confirm all outputs match the printed state during testing
Prototype framing Keeps the decision local and simple Honest framing prevents overclaiming sensor capability Explain clearly that this is not a certified water test

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 can be used for local warning demonstrations and early field trials
  • Before deployment, it needs enclosure protection, calibration records, and a clear response procedure
  • Alert systems should always be treated as decision-support tools unless they are certified for the application

Troubleshooting

Problem Possible Cause Solution
The red LED is always on The thresholds are too low or the sensor output is out of range Measure the clean-sample baseline first and adjust the thresholds
The reading never changes The sensor is not powered or the sample conditions are too similar Confirm the analog output and compare more distinct sample conditions
The buzzer never sounds The critical threshold is too high or the buzzer wiring is wrong Force a stronger test condition and test the buzzer pin separately
Values seem noisy The analog wiring is loose or the sample is unstable Shorten the analog wire and keep the sample conditions steady during testing