#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define pins
const int lowerTankPin = 2; // Lower tank float sensor
const int upperTankFullPin = 3; // Upper tank full-level float sensor
const int upperTankMidPin = 4; // Upper tank middle-level float sensor
const int pumpPin = 8; // Pump relay
// Display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(lowerTankPin, INPUT_PULLUP); // Lower tank sensor as input
pinMode(upperTankFullPin, INPUT_PULLUP); // Upper tank full sensor as input
pinMode(upperTankMidPin, INPUT_PULLUP); // Upper tank middle sensor as input
pinMode(pumpPin, OUTPUT); // Pump relay as output
lcd.init();
lcd.backlight();
lcd.print("Water System");
delay(2000);
lcd.clear();
}
void loop() {
bool lowerTankEmpty = digitalRead(lowerTankPin) == LOW; // LOWER tank empty
bool upperTankFull = digitalRead(upperTankFullPin) == LOW; // UPPER tank full
bool upperTankMidLow = digitalRead(upperTankMidPin) == LOW; // UPPER tank middle-level low
// Pump control logic
if (!lowerTankEmpty && upperTankMidLow && !upperTankFull) {
// Start the pump if lower tank has water, upper tank is not full, and middle sensor is low
digitalWrite(pumpPin, HIGH);
lcd.setCursor(0, 0);
lcd.print("Pumping...");
} else if (upperTankFull || lowerTankEmpty) {
// Stop the pump if upper tank is full or lower tank is empty
digitalWrite(pumpPin, LOW);
lcd.setCursor(0, 0);
lcd.print("Pump Off ");
}
// Display status
lcd.setCursor(0, 1);
if (upperTankFull) {
lcd.print("Upper Full ");
} else if (lowerTankEmpty) {
lcd.print("Lower Empty ");
} else if (upperTankMidLow) {
lcd.print("Mid Low ");
} else {
lcd.print("Idle ");
}
delay(500); // Small delay for stability
}