Project 3.5.3: Flood Prone Area Alert
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico measures the distance from an ultrasonic sensor to the water surface and warns when the water level rises into flood-danger ranges.
Flood-prone drains and channels need early warning before the water reaches an unsafe height.
A Pico 2 W prototype with an ultrasonic sensor, LED, and buzzer for local flood-level monitoring.
Distance-based water-level interpretation, threshold design, and ultrasonic sensor safety on the Pico.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Ultrasonic sensor |
![]() Breadboard |
![]() Jumper wires |
![]() LED |
![]() Resistor |
![]() Buzzer |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| HC-SR04 VCC | VBUS 5V | Physical pin 40 | Power the module from USB 5V |
| HC-SR04 GND | GND | Physical pin 38 | Common ground |
| HC-SR04 Trig | GPIO 14 | GPIO 14 / physical pin 19 | 3.3V trigger output |
| HC-SR04 Echo | Voltage-divider midpoint to GPIO 13 | GPIO 13 / physical pin 17 | Do not connect Echo directly |
| LED anode | 220 ohm resistor to GPIO 15 | GPIO 15 / physical pin 20 | Warning LED |
| Buzzer positive | GPIO 16 | GPIO 16 / physical pin 21 | Critical buzzer |
Wiring Diagram

VBUS -> HC-SR04 VCC
GND -> HC-SR04 GND, LED cathode, buzzer negative
GPIO 14 -> HC-SR04 Trig
HC-SR04 Echo -> resistor divider -> GPIO 13
GPIO 15 -> 220 ohm resistor -> LED anode
GPIO 16 -> buzzer positive
Step-by-Step Assembly
- Connect HC-SR04 VCC to VBUS and GND to Pico ground.
- Connect Trig to GPIO 14 and use a voltage divider before connecting Echo to GPIO 13.
- Wire the LED to GPIO 15 through a 220 ohm resistor and connect the LED cathode to GND.
- Connect the buzzer positive pin to GPIO 16 and its negative pin to GND.
- Mount the ultrasonic sensor above the water path so it can see the surface without being splashed.
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: Measure known distances of 5 cm, 10 cm, 20 cm, and 30 cm before pointing the sensor at real water.
- Test the output device: Test the LED and buzzer outputs separately before linking them to the flood thresholds.
- Test the decision logic: Confirm that SAFE, WARNING, and CRITICAL states change at the intended water-level distances.
- Run the full system: Run the full system over a test container as the water rises and compare the alert state with the actual distance.
- Validate the prototype: Check how ripples, foam, or angled sensor mounting affect the stability of the reading.
- 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 Pin
import time
TRIG_PIN = 14
ECHO_PIN = 13
LED_PIN = 15
BUZZER_PIN = 16
WARNING_DISTANCE_CM = 30.0
DANGER_DISTANCE_CM = 15.0
TIMEOUT_US = 30000
trig = Pin(TRIG_PIN, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
led = Pin(LED_PIN, Pin.OUT)
buzzer = Pin(BUZZER_PIN, Pin.OUT)
def measure_distance_cm():
trig.low()
time.sleep_us(2)
trig.high()
time.sleep_us(10)
trig.low()
start = time.ticks_us()
while echo.value() == 0:
if time.ticks_diff(time.ticks_us(), start) > TIMEOUT_US:
return None
pulse_start = time.ticks_us()
while echo.value() == 1:
if time.ticks_diff(time.ticks_us(), pulse_start) > TIMEOUT_US:
return None
pulse_end = time.ticks_us()
duration = time.ticks_diff(pulse_end, pulse_start)
return (duration * 0.0343) / 2
print("Flood alert ready")
while True:
distance = measure_distance_cm()
if distance is None:
status = "SENSOR ERROR"
led.off()
buzzer.off()
elif distance <= DANGER_DISTANCE_CM:
status = "CRITICAL"
led.on()
buzzer.on()
elif distance <= WARNING_DISTANCE_CM:
status = "WARNING"
led.on()
buzzer.off()
else:
status = "SAFE"
led.off()
buzzer.off()
if distance is None:
print("Distance: timeout | Status: SENSOR ERROR")
else:
print("Distance: {:.1f} cm | Status: {}".format(distance, status))
time.sleep(1)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Pin and threshold setup | Defines the ultrasonic pins and the warning and danger distances | Flood projects invert the usual distance meaning because smaller distance means higher water | Measure your own channel or container before changing the threshold constants |
| measure_distance_cm | Triggers the ultrasonic module and converts pulse time into centimeters | The water-level decision is only as good as the distance measurement | If the sensor sits far above the water, adjust TIMEOUT_US and revalidate |
| State logic | Maps the distance into SAFE, WARNING, CRITICAL, or SENSOR ERROR | Clear states help users react before the flood condition becomes severe | Adjust WARNING_DISTANCE_CM and DANGER_DISTANCE_CM after container tests |
| Outputs and reporting | Applies the LED and buzzer behavior and prints the current status | Students can compare the live water level with the chosen alert outputs | Keep the buzzer reserved for CRITICAL so the warning stage is distinct |
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 SAFE when the measured distance is comfortably above the warning level
- Warning condition test: use a target at about 20 cm to 30 cm and confirm the LED turns on without the buzzer
- Critical condition test: use a target at 5 cm to 15 cm and confirm both LED and buzzer activate
- Sensor reading validation: test at 5 cm, 10 cm, 20 cm, and 30 cm and confirm the measured value is reasonably close
- Limitation test: compare calm water, rippled water, and reflective surfaces to see how the reading changes
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 |
|---|---|---|
| Distance always times out | Echo divider or wiring is wrong | Rebuild the Echo voltage divider and confirm the ground is shared with the Pico |
| System goes to CRITICAL too early | Thresholds do not match the mounting height | Measure the normal water distance and recalibrate the warning and danger distances |
| Readings jump on moving water | Ripples or angled reflections are confusing the sensor | Mount the sensor straighter and average multiple readings if needed |
| Buzzer never sounds | Danger threshold is too small or the buzzer is miswired | Temporarily increase DANGER_DISTANCE_CM and verify the buzzer wiring with a direct test |






