Project 2.1.11: Flood Alert System
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project builds a flood alert system that responds differently to warning-level and critical-level water rise.
Students will connect a water-level sensor, add warning and critical thresholds, use an acknowledge button, and drive both a buzzer and a relay output for higher-level alerts.
The final system should report NORMAL, WARNING, or CRITICAL, sound the buzzer for critical conditions unless acknowledged, and switch an external alarm relay when the water level is dangerously high.
Required Components
![]() Red LED and 220 Ω resistor |
![]() Raspberry Pi Pico 2 W |
![]() Relay module or transistor relay driver |
![]() Push button |
![]() Yellow LED and 220 Ω resistor |
![]() Active buzzer |
![]() Breadboard and jumper wires |
![]() Water-level sensor |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Water-level sensor VCC | 3.3V | Physical pin 36 | Use a 3.3V-safe sensor output |
| Water-level sensor GND | GND | Any GND pin | Common ground |
| Water-level sensor AOUT | GPIO 26 | GPIO 26 / physical pin 31 | Analog water-level input |
| Relay IN | GPIO 15 | GPIO 15 / physical pin 20 | Critical alarm relay control |
| Acknowledge button one side | GPIO 5 | GPIO 5 / physical pin 7 | Uses internal pull-up |
| Acknowledge button other side | GND | Any GND pin | Pressing pulls the input low |
| Yellow LED anode | GPIO 16 through 220 Ω resistor | GPIO 16 / physical pin 21 | Warning indicator |
| Red LED anode | GPIO 17 through 220 Ω resistor | GPIO 17 / physical pin 22 | Critical indicator |
| Buzzer positive | GPIO 18 | GPIO 18 / physical pin 24 | Critical alarm buzzer |
| LED cathodes and buzzer negative | GND | Any GND pin | Return path |
Wiring Diagram

Water-level sensor AOUT -> GPIO 26
Relay IN -> GPIO 15
Acknowledge button -> GPIO 5 and GND
GPIO 16 -> 220Ω -> Yellow LED anode
GPIO 17 -> 220Ω -> Red LED anode
GPIO 18 -> Buzzer positive
Relay contacts switch the external alarm power, not Pico GPIO power
Step-by-Step Assembly
Step 1: Place the Raspberry Pi Pico 2W
Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.
Step 2: Position the Water-Level Sensor
Place the water-level sensor so only the sensing area can approach the test water. Identify VCC, GND, and AOUT / AO / Signal before wiring.
Step 3: Connect the Water-Level Sensor
Connect water-level sensor VCC to 3.3V. Connect water-level sensor GND to GND. Connect water-level sensor AOUT, AO, or Signal to GPIO 26.
Step 4: Place and Wire the Relay Module
Identify relay VCC, GND, IN, COM, NO, and NC before wiring. Connect relay IN to GPIO 15. Power the relay module from the supply required by its label. Connect relay GND to Pico GND and to the external alarm supply GND where shared grounding is required.
Step 5: Place the Acknowledge Button
Place the acknowledge push button across the breadboard center gap. Connect one side of the acknowledge button to GPIO 5. Connect the opposite side of the button to GND.
Step 6: Place and Connect the Warning Outputs
Place the yellow LED and red LED on the breadboard. Connect the yellow LED long leg through a 220Ω resistor to GPIO 16. Connect the red LED long leg through a 220Ω resistor to GPIO 17. Connect both LED short legs to GND. Connect the buzzer positive pin to GPIO 18. Connect the buzzer negative pin to GND.
Step 7: Keep External Alarm Loads on the Relay Side
If you connect an external alarm, light, or siren, keep it on its own external supply. Route only the external alarm supply path through relay COM and NO.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] Water-level sensor VCC connects to 3.3V
- [x] Water-level sensor GND connects to GND
- [x] Water-level sensor AOUT / AO / Signal connects to GPIO 26
- [x] Relay IN connects to GPIO 15
- [x] Acknowledge button connects between GPIO 5 and GND
- [x] Yellow LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Red LED long leg connects through a 220Ω resistor to GPIO 17
- [x] Buzzer positive pin connects to GPIO 18
- [x] External alarm load uses relay COM and NO only if connected
- [x] No loose jumper wires
Intermediate Note Test warning and critical water levels with small, controlled changes before setting final thresholds. The acknowledge button silences the buzzer but does not remove the flood condition.
Safety Note Keep the Pico, breadboard, USB cable, and jumper wires away from water. Use external power for any real alarm load and follow that device's voltage and current limits.
Testing Individual Components
Before running the full project, test each part separately. This makes it easier to find wiring, library, or code problems.
Hardware Setup
- Build the sensor, LEDs, buzzer, and button first before connecting any external alarm to the relay contacts
- Keep all electronics well above the water source during testing
Test the Input Sensor
- Record low-level, warning-level, and high-level readings from the water sensor using safe test conditions
- Press the acknowledge button and confirm the serial output detects the input correctly
Test the Output Device
- Run a relay click test with no external alarm connected and confirm the LEDs and buzzer match the forced states
- If the relay is active-low, change RELAY_ON and RELAY_OFF before connecting the real load
Test Communication
- Watch the Thonny Shell and confirm it prints the water percentage, critical-hit count, and acknowledgement status
- Use the printed values to choose better warning and critical thresholds
Run the Full System
- Raise the sensor level gradually and confirm the system moves from NORMAL to WARNING before entering CRITICAL
- Press the acknowledge button during a critical condition and confirm the buzzer silences while the red LED and relay remain active until the level drops
Save the Project
- Save the final code and record the warning and critical thresholds that matched your sensor
- Write down what extra protection would be needed before real outdoor deployment
Additional Testing and Calibration Checks
- Normal condition test: confirm the system stays in NORMAL when the water level is low
- Threshold condition test: raise the sensor level to trigger WARNING and then CRITICAL
- Output response test: confirm the LEDs, buzzer, and relay match the current state
- Relay click test: keep the external alarm disconnected during the first relay test
- Water safety test: do not let the Pico, USB cable, or breadboard get wet during the experiment
- Calibration note: if the sensor is noisy, adjust the thresholds or confirmation count after measuring several real readings
Full Project Code
After completing and checking the circuit connections, open Thonny IDE. Copy and paste the code below 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
water_sensor = ADC(26)
relay = Pin(15, Pin.OUT)
ack_button = Pin(5, Pin.IN, Pin.PULL_UP)
warning_led = Pin(16, Pin.OUT)
critical_led = Pin(17, Pin.OUT)
buzzer = Pin(18, Pin.OUT)
RELAY_ON = 1
RELAY_OFF = 0
WARNING_LEVEL = 55
CRITICAL_LEVEL = 75
CONFIRM_READS = 2
DEBOUNCE_MS = 250
critical_hits = 0
acknowledged = False
last_button_ms = 0
def button_pressed():
global last_button_ms
now_ms = time.ticks_ms()
if ack_button.value() == 0 and time.ticks_diff(now_ms, last_button_ms) > DEBOUNCE_MS:
while ack_button.value() == 0:
time.sleep(0.02)
last_button_ms = now_ms
return True
return False
def water_level_percent():
return int((water_sensor.read_u16() / 65535) * 100)
def beep():
for _ in range(2):
buzzer.value(1)
time.sleep(0.12)
buzzer.value(0)
time.sleep(0.12)
print('=== Flood Alert System ===')
print('Only the sensor should be near water. Keep the Pico dry.\n')
while True:
level = water_level_percent()
if level >= CRITICAL_LEVEL:
critical_hits += 1
else:
critical_hits = 0
if button_pressed() and level >= CRITICAL_LEVEL:
acknowledged = True
print('Critical alarm acknowledged by the user.')
if level < WARNING_LEVEL:
state = 'NORMAL'
warning_led.value(0)
critical_led.value(0)
relay.value(RELAY_OFF)
buzzer.value(0)
acknowledged = False
elif level < CRITICAL_LEVEL:
state = 'WARNING'
warning_led.value(1)
critical_led.value(0)
relay.value(RELAY_OFF)
buzzer.value(0)
acknowledged = False
else:
state = 'CRITICAL'
warning_led.value(0)
critical_led.value(1)
relay.value(RELAY_ON)
if critical_hits >= CONFIRM_READS and not acknowledged:
beep()
print('Water level: {}% | Critical hits: {} | Acknowledged: {} | State: {}'.format(
level, critical_hits, acknowledged, state
))
time.sleep(2)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| Two thresholds | Separate warning-level water rise from a critical flood state | Students can see why early warning is useful before full alarm |
| Confirmation count | Requires repeated critical readings before the buzzer response becomes trusted | This reduces false critical alarms from one noisy reading |
| Acknowledge button | Lets the user silence the buzzer during a real critical condition | This matches real alarm workflows without hiding the flood risk |
| Relay output | Activates an external alarm path only during the critical state | Students learn how the Pico can trigger a bigger warning system safely |
Expected Result
Below the warning threshold, both LEDs and the relay should stay off.
At warning level, the yellow LED should turn on but the relay and buzzer should remain off.
At confirmed critical level, the red LED and relay should turn on, and the buzzer should sound unless the alarm is acknowledged.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The alarm goes critical too early | The critical threshold is too low for your sensor range | Measure the water readings again and increase CRITICAL_LEVEL if needed |
| The relay never turns on | The relay logic is reversed or the relay module wiring is wrong | Check GPIO 15 and swap RELAY_ON / RELAY_OFF if your board is active-low |
| The buzzer never stops | The water level is still critical or the acknowledge button is not working | Check the button wiring and confirm the level really falls below the warning threshold to clear the alarm |
| The readings are unstable | The sensor is moving or getting splashed irregularly | Secure the sensor and test in calmer water conditions first |






