Components:
- Arduino board (e.g., Arduino Uno)
- Water level sensor
- Buzzer (Active or Passive)
- Red LED (for emergency signal)
- Resistors (for the LED)
- Breadboard and Jumper wires
- Power source (e.g., 9V battery or USB cable)
Wiring Connection:
- Water Sensor:
- VCC to 5V on Arduino.
- GND to GND on Arduino.
- Signal pin to an Analog pin on Arduino (e.g., A0).
- Buzzer:
- One pin to a digital pin on Arduino (e.g., pin 3).
- The other pin to GND.
- Red LED:
- Anode (long leg) to a digital pin on Arduino (e.g., pin 4).
- Cathode (short leg) to GND through a 220-ohm resistor.
Code:
// Pin definitions
const int waterSensorPin = A0; // Pin connected to water sensor
const int buzzerPin = 3; // Pin connected to buzzer
const int ledPin = 4; // Pin connected to red LED
// Threshold for water level (adjust based on your sensor)
const int waterThreshold = 300; // Adjust this value based on actual sensor range
// Variables
int waterLevel = 0; // Variable to store water sensor reading
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set pin modes
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Ensure everything is off at startup
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
}
void loop() {
// Read the water level from the sensor
waterLevel = analogRead(waterSensorPin);
// Debugging: Print water level to the serial monitor
Serial.print("Water Level: ");
Serial.println(waterLevel);
// Check if water level exceeds threshold (indicating flood)
if (waterLevel > waterThreshold) {
Serial.println("Flood detected! Activating alert...");
digitalWrite(buzzerPin, HIGH); // Turn on buzzer
digitalWrite(ledPin, HIGH); // Turn on red LED
} else {
Serial.println("No flood detected. Everything is normal.");
digitalWrite(buzzerPin, LOW); // Turn off buzzer
digitalWrite(ledPin, LOW); // Turn off red LED
}
// Small delay for stability
delay(500); // Delay to prevent flooding the Serial Monitor with too many prints
}
Explanation of Code:
- Pin Setup: The pins for the water sensor, buzzer, LED, and switch are defined and set in
setup()
. - Water Sensor Logic: The water level is read from the analog pin connected to the water sensor. If the water level exceeds the threshold, the buzzer and LED are activated.
- Serial Monitor: The water level is printed to the serial monitor for debugging purposes.
Threshold Adjustment:
- You can adjust the value of
waterThreshold
based on the type of water sensor you’re using. The threshold value should be set so that the system triggers only when the water level reaches a certain level, which you consider to be a flood.
Testing:
- Upload the code to your Arduino board.
- Open the Serial Monitor to view water level readings from the sensor.
- Test the system by simulating flood conditions (by adding water to the sensor) or by manually adjusting the water threshold.