Community project

Water-Level Flood Alert

Photo of Water-Level Flood Alert
Generated with AI

Shoro Dnlz

Published November 24, 2026

This water-level flood alert system uses an ultrasonic sensor to continuously monitor water depth in a container and trigger visual and audible warnings when levels rise to dangerous thresholds. Built around an Arduino Uno, the system displays three-stage alerts via color-coded LEDs (green for safe, yellow for caution, red for high water) and sounds a piezo buzzer when water reaches critical levels.

The guide provides a complete wiring diagram showing how to connect the HC-SR04 ultrasonic sensor, three LEDs with current-limiting resistors, and the piezo buzzer to the Arduino. Assembly steps cover sensor mounting, all electrical connections, and firmware upload. The included code implements filtered distance measurements and hysteresis logic to prevent false alarms while reliably detecting rising water levels.

Wiring diagram

Wiring diagram for Water-Level Flood Alert
Loading diagram…

Gather all the parts

QtyComponent
1

HC-SR04

HC-SR04 ultrasonic distance sensor module with separate 40 kHz transmitter and receiver transducers, measuring 2 to 400 cm. A 10 us pulse on TRIG starts a measurement and ECHO stays high for the echo's round-trip time. This HC-SR04+ board runs from 3.3 V or 5 V.

1

Piezo Buzzer

Small passive piezo buzzer (TDK PS1240) for beeps, tones and alerts. It has no built-in oscillator, so drive one lead with a 3-30 V square wave from a microcontroller pin using tone() or PWM and connect the other lead to ground. It is loudest around 4 kHz and works from about 2 to 10 kHz; driving both leads in antiphase roughly doubles the volume.

1

LED

green

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

330 Ω

Through-hole resistor (current-limiting in series with an LED)

1

LED

yellow

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

330 Ω

Through-hole resistor (current-limiting in series with an LED)

1

LED

red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

330 Ω

Through-hole resistor (current-limiting in series with an LED)

Assemble it in 3 steps

1. Mount the sensor above the tray

Rigidly mount the HC-SR04 facing straight down with its face 20 cm above the empty floor of a wide, shallow tray. Keep the Arduino and breadboard away from the tray. Use a ruler and mark water depths of 3 cm and 6 cm.

  • Teacher supervision only. Keep the sensor and all electronics dry. Never raise water closer than 4 cm to the sensor face. Disconnect USB before handling wiring or cleaning spills.

2. Connect the sensor and buzzer

With USB unplugged, connect sensor VCC to 5V, GND to ground, TRIG to D2 and ECHO to D3. Connect the passive piezo signal to D9 and its other lead to ground.

3. Wire the three warning lights

Connect D4 to the green LED's long leg through its own 330 ohm resistor. Do the same from D5 to yellow and D6 to red. Connect each short leg to ground.

Review all connections

1. Connections between "sonar" and "Arduino"

FunctionsonarArduino
powerVCC5V
groundGNDGND
digitalTRIGGPIO 2
digitalECHOGPIO 3

2. Connections between "piezo" and "Arduino"

FunctionpiezoArduino
pwmSIGGPIO 9
groundGNDGND

3. Connections between "rgreen" and "Arduino"

FunctionrgreenArduino
digitalP1GPIO 4

4. Connections between "green" and "Arduino"

FunctiongreenArduino
digitalANODE → Resistor P2EXT
groundGNDGND

5. Connections between "ryellow" and "Arduino"

FunctionryellowArduino
digitalP1GPIO 5

6. Connections between "yellow" and "Arduino"

FunctionyellowArduino
digitalANODE → Resistor P2EXT
groundGNDGND

7. Connections between "rred" and "Arduino"

FunctionrredArduino
digitalP1GPIO 6

8. Connections between "red" and "Arduino"

FunctionredArduino
digitalANODE → Resistor P2EXT
groundGNDGND

Deploy the firmware

#include <Arduino.h>
constexpr uint8_t TRIG=2,ECHO=3,GREEN=4,YELLOW=5,RED=6,PIEZO=9;
// Measure from the sensor face to the empty container floor.
constexpr float EMPTY_CM=20.0, CAUTION_CM=3.0, HIGH_CM=6.0, HYST_CM=0.7;
enum State {ERROR, SAFE, CAUTION, HIGH_WATER};
State state=ERROR;
float samples[5], height=0;
uint8_t count=0, slot=0;
unsigned long lastPing=0,lastGood=0,seq=0;
void setup(){
  pinMode(TRIG,OUTPUT); digitalWrite(TRIG,LOW); pinMode(ECHO,INPUT);
  for(uint8_t p=GREEN;p<=RED;++p) pinMode(p,OUTPUT);
  pinMode(PIEZO,OUTPUT);
  Serial.begin(115200);
  Serial.println(F("ms,sequence,clearance_cm,filtered_height_cm,state"));
}
void loop(){
  unsigned long now=millis();
  if(now-lastGood>=500){state=ERROR;count=slot=0;}
  if(now-lastPing>=100){
    lastPing=now;
    digitalWrite(TRIG,HIGH);delayMicroseconds(12);digitalWrite(TRIG,LOW);
    unsigned long us=pulseIn(ECHO,HIGH,4000);
    float cm=us/58.0;
    bool valid=cm>=4 && cm<=EMPTY_CM+1.5;
    if(valid){
      lastGood=now;
      samples[slot]=cm;slot=(slot+1)%5;if(count<5)++count;
      if(count==5){
        float sorted[5];
        for(uint8_t i=0;i<5;++i){
          int j=i;while(j>0 && sorted[j-1]>samples[i]){sorted[j]=sorted[j-1];--j;}
          sorted[j]=samples[i];
        }
        height=max(0.0f,EMPTY_CM-sorted[2]);
        if(height>=HIGH_CM || (state==HIGH_WATER && height>=HIGH_CM-HYST_CM)) state=HIGH_WATER;
        else if(height>=CAUTION_CM || ((state==CAUTION || state==HIGH_WATER) && height>=CAUTION_CM-HYST_CM)) state=CAUTION;
        else state=SAFE;
      }
    }
    Serial.print(now);Serial.print(',');Serial.print(++seq);Serial.print(',');
    if(valid)Serial.print(cm,2);Serial.print(',');
    if(state!=ERROR)Serial.print(height,2);Serial.print(',');
    Serial.println(state==SAFE?F("SAFE"):state==CAUTION?F("CAUTION"):state==HIGH_WATER?F("HIGH"):F("SENSOR ERROR"));
  }
  bool flash=now%500<250;
  digitalWrite(GREEN,state==SAFE);
  digitalWrite(YELLOW,state==CAUTION || (state==ERROR && flash));
  digitalWrite(RED,state==HIGH_WATER || (state==ERROR && flash));
  uint16_t hz=state==HIGH_WATER && now%500<220?2200:state==ERROR && now%1500<120?1000:0;
  static uint16_t previous=0;
  if(hz!=previous){if(hz)tone(PIEZO,hz);else noTone(PIEZO);previous=hz;}
}

Remix this project

Make it yours in one click

Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.

Open in Schematik