This circuit is designed to monitor water levels using an HC-SR04 Ultrasonic Sensor and provide visual and auditory alerts based on predefined distance thresholds. It utilizes an Arduino Nano as the central microcontroller to process sensor data and control the outputs, which include three LEDs (green, yellow, and red) and a buzzer. The circuit also features a SIM800L GSM Module for sending SMS alerts when certain conditions are met. A 5V battery powers the GSM module, while the Arduino Nano and other components are powered through the Arduino's voltage regulator.
#include <SoftwareSerial.h>
const int trigPin = 9; // Trig pin for ultrasonic sensor
const int echoPin = 10; // Echo pin for ultrasonic sensor
const int greenLED = 3; // Green LED pin
const int yellowLED = 4; // Yellow LED pin
const int redLED = 5; // Red LED pin
const int buzzer = 6; // Buzzer pin
// Define distance thresholds (in cm)
const int safeDistance = 100; // Safe distance
const int warningDistance = 50; // Warning distance
const int dangerDistance = 20; // Danger distance
// GSM module connections
SoftwareSerial gsm(7, 8); // GSM TX to pin 7, RX to pin 8
void setup() {
// Pin Modes
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(buzzer, OUTPUT);
// Initialize GSM module and Serial for debugging
gsm.begin(9600);
Serial.begin(9600);
delay(1000); // Allow time for setup
}
void loop() {
// Variables to hold duration of echo and calculated distance
long duration, distance;
// Trigger the ultrasonic sensor to send a pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the echo pulse
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = (duration * 0.034) / 2;
// Print the distance for debugging
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Flood warning logic based on distance
if (distance > safeDistance) {
// Safe condition
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(buzzer, LOW);
}
else if (distance <= safeDistance && distance > warningDistance) {
// Warning condition
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
digitalWrite(buzzer, LOW);
sendSMS("Warning! Water level is rising.");
}
else if (distance <= warningDistance && distance > dangerDistance) {
// Danger condition
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
digitalWrite(buzzer, HIGH);
sendSMS("Danger! Water level has reached a critical point.");
}
// Delay for 2 seconds before the next reading
delay(2000);
}
// Function to send SMS alerts
void sendSMS(String message) {
gsm.println("AT+CMGF=1"); // Set GSM to text mode
delay(1000);
gsm.println("AT+CMGS=\"+91xxxxxxxxxx\""); // Replace with recipient's phone number
delay(1000);
gsm.println(message); // Send the message
delay(100);
gsm.println((char)26); // ASCII code of CTRL+Z to send SMS
delay(1000);
}
This code is responsible for reading the distance from the ultrasonic sensor and activating the appropriate LED and buzzer based on the distance thresholds. It also includes functionality to send SMS alerts via the GSM module when the water level reaches warning or danger conditions.