This circuit is designed to monitor the water level in a tank using an HC-SR04 Ultrasonic Sensor and display the water level on an LCD Display. It also controls a Mini Diaphragm Water Pump via a KY-019 Relay module. The entire system is managed by an Arduino UNO microcontroller.
HC-SR04 Ultrasonic Sensor
Arduino UNO
LCD Display 20x4 I2C
Mini Diaphragm Water Pump
KY-019 Relay module 1 channel
18650 Li-Ion
#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 initializes the LCD display, sets up the pins for the ultrasonic sensor and relay, and continuously measures the water level in the tank. If the water level drops below a certain percentage, the relay is activated to turn on the water pump. The water level and pump status are displayed on the LCD and printed to the serial monitor.