

This circuit is designed to monitor the water level in a tank and control a water pump accordingly. It uses an HC-SR04 Ultrasonic Sensor to measure the distance to the water surface, an Arduino UNO microcontroller to process the distance measurements and control a relay module, which in turn controls a Mini Diaphragm Water Pump. An LCD Display is used to provide a user interface, displaying the current water level and the status of the pump. The system is powered by a 5V supply from the Arduino UNO and a separate 18650 Li-Ion battery for the pump.
#include <LiquidCrystal_I2C.h>
// Initialize the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define connections to sensor
#define TRIGPIN 7  // Trigger pin
#define ECHOPIN 8  // Echo pin
#define RelayPin 6  // Relay pin
float duration;
float distance;
int waterLevelPer;
// Set Water Level Distance in CM
int emptyTankDistance = 20;  // Distance when tank is empty
int fullTankDistance = 10;    // Distance when tank is full
// Set trigger value in percentage
int triggerPer = 10;  // Alarm/pump will start when water level drops below this percentage
void setup() {
  // Set up serial monitor
  Serial.begin(9600);
  // Set pin modes for sensor connections
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, HIGH); // Ensure relay is off initially
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Water Level");
  lcd.setCursor(0, 1);
  lcd.print("Monitoring...");
  delay(2000);
  lcd.clear();
}
void measureDistance() {
  // Set the trigger pin LOW for 2us
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  // Set the trigger pin HIGH for 20us to send pulse
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(20);
  digitalWrite(TRIGPIN, LOW);
  // Measure the width of the incoming pulse
  duration = pulseIn(ECHOPIN, HIGH);
  
  // Determine distance from duration
  distance = (duration * 0.017); // Convert to cm
  // Clamp distance to ensure it doesn't exceed boundaries
  distance = constrain(distance, fullTankDistance, emptyTankDistance);
  
  // Calculate water level percentage
  waterLevelPer = map((int)distance, emptyTankDistance, fullTankDistance, 0, 100);
  waterLevelPer = constrain(waterLevelPer, 0, 100); // Clamp to 0-100%
  // Print result to serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  Serial.print("Water Level: ");
  Serial.print(waterLevelPer);
  Serial.println("%");
  // Control the relay based on water level
  if (waterLevelPer <= triggerPer) {
    digitalWrite(RelayPin, LOW); // Turn on relay (pump)
  } else if (waterLevelPer >= 100) {
    digitalWrite(RelayPin, HIGH); // Turn off relay
  }
  // Update LCD
  updateLCD();
}
void updateLCD() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("WLevel:");
  lcd.setCursor(7, 0);
  lcd.print(waterLevelPer);
  lcd.setCursor(10, 0);
  lcd.print("%");
  lcd.setCursor(0, 1);
  lcd.print("Motor:");
  lcd.setCursor(6, 1);
  lcd.print(digitalRead(RelayPin) == LOW ? "ON " : "OFF");
}
void loop() {
  measureDistance();
  delay(1000); // Measure every second
}
This code is responsible for initializing the LCD display and the HC-SR04 Ultrasonic Sensor, measuring the distance to the water surface, calculating the water level percentage, and controlling the relay module based on the water level. The LCD display is updated with the current water level and the status of the pump. The relay module activates the pump when the water level is below the set threshold and turns it off when the tank is full.