#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin definitions for Ultrasonic Sensor and other components
#define TRIG_PIN 9 // Trig pin connected to D5 (Digital Pin 5)
#define ECHO_PIN 10 // Echo pin connected to D6 (Digital Pin 6)
#define RELAY_PIN 7 // Relay pin connected to D7 (Digital Pin 7)
#define LED_PIN 8 // LED pin connected to D8 (Digital Pin 8)
#define BUTTON_PIN 13
// Constants
const int tankHeight = 400; // Height of water tank in cm
const float tankHeightP = 100.0; // Maximum tank percentage
const float lowWaterLevel = 100; // Water level threshold in cm
const float lowWaterLevelP = lowWaterLevel / tankHeight * 100.0; // Low water level as a percentage
const float maxHeightP = 95;
bool pumpOn = false; // Flag to keep track of the pump status
bool LED = false;
// Set the LCD address (replace 0x27 with your I2C address)
LiquidCrystal_I2C lcd(0x27, 20, 4); // 16x4 LCD with I2C module
// Function to measure distance from the sensor to the water surface
float measureDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 30000); // Timeout after 30ms
long distance = duration * 0.034 / 2; // Convert to cm
// Calculate percentage of distance relative to tank height
float distanceP = (float)distance / (float)tankHeight * 100.0;
return distanceP;
}
void setup() {
// Initialize serial monitor (optional for debugging)
Serial.begin(9600);
// Initialize sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Initialize relay and LED pins
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Turn off the relay initially
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Turn off the LED initially
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, LOW);
// Initialize the LCD
lcd.init();
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Water Level: ");
lcd.setCursor(0, 2);
lcd.print("Pumper's status: ");
}
void loop() {
int button=digitalRead(BUTTON_PIN);// Read the button status
// Measure the water level as a percentage
float distanceP = measureDistance();
if (distanceP < 0) { // Check for error in distance measurement
lcd.setCursor(0, 1);
lcd.print("Error in sensor ");
return; // Skip further processing
}
float waterLevel = tankHeightP - distanceP; // Calculate water level percentage
Serial.print(waterLevel);
Serial.println(" %");
// Display water level on the LCD
lcd.setCursor(0, 1); // Second row, first column
lcd.print(waterLevel);
lcd.print(" % "); // Clear extra characters
// Check if water level is below the low threshold and the pump is off
if (waterLevel < lowWaterLevelP && !pumpOn) {
pumpOn = true; // Turn on the pump
digitalWrite(RELAY_PIN, HIGH); // Activate the pump
lcd.setCursor(0, 3); // Fourth row, first column
lcd.print("Pump: Active "); // Display Pump Active on the LCD
}
// Check if water level has reached the maximum level and the pump is on
else if (waterLevel >= maxHeightP && pumpOn) {
pumpOn = false; // Turn off the pump
digitalWrite(RELAY_PIN, LOW); // Deactivate the pump
lcd.setCursor(0, 3);
lcd.print("Pump: Stopped "); // Display Pump Stopped on the LCD
digitalWrite(LED_PIN, LOW); // Ensure LED is off
}
// Display pump inactive status if neither condition is met
else if (!pumpOn) {
lcd.setCursor(0, 3);
lcd.print("Pump: Inactive "); // Display Pump Inactive on the LCD
digitalWrite(RELAY_PIN, LOW); // Ensure the pump remains off
}
// Store the initial water level when the pump is activated
float initialWaterLevel = waterLevel;
delay(3000); // Wait for 3 seconds before checking water level again
// Measure water level again after 3 seconds
distanceP = measureDistance();
waterLevel = tankHeightP - distanceP;
// Check if the water level has increased after 5 seconds
if (waterLevel <= initialWaterLevel && pumpOn) {
LED = true;
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN, HIGH); // Turn on LED to indicate issue
Serial.println("Water level not increasing after 5 seconds!");
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED if water level is increasing
}
if(button==HIGH && LED){
pumpOn = false;
digitalWrite(LED_PIN, LOW);
return;
}
delay(100); // Delay for 2 seconds before the next measurement
}