#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Initialize the LCD using the address 0x27 and size of 120x4
#define trigPin 9 // Arduino digital pin connected to the trigger pin of the ultrasonic sensor
#define echoPin 10 // Arduino digital pin connected to the echo pin of the ultrasonic sensor
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setBacklight(HIGH); // Turn on the backlight
lcd.begin(20,4); // Initialize the LCD with 16 columns and 2 rows
lcd.setCursor(0, 0); // Set the cursor to the first column and the first row
lcd.print("God Is My Light"); // Print the heading text
pinMode(trigPin, OUTPUT); // Set the trigger pin as an output
pinMode(echoPin, INPUT); // Set the echo pin as an input
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
long duration, distance;
float waterLevel;
String waterStatus;
// Clear the trigger pin and wait for a short delay
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigger pin to high for 10 microseconds to transmit the ultrasonic pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pin to measure the duration of the pulse and calculate the distance
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.0343) / 2; // Calculate the distance in cm (speed of sound is 0.0343 cm/microsecond)
// Assuming tank height is 20 cm, calculate the water level in percentage
waterLevel = 100 - (distance * 100 / 200);
lcd.setCursor(0, 1); // Set the cursor to the first column and the second row
lcd.print("Level: "); // Print the label
if (waterLevel < 10) {
waterStatus = "Empty Tank";
} else if (waterLevel < 25) {
waterStatus = "Low Water ";
} else {
waterStatus = String(waterLevel) + "% ";
}
lcd.print(waterStatus); // Print the water level status
delay(1000); // Delay for 1 second before the next reading
}