Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Based Ultrasonic Water Level Monitoring System with LCD Display and Automatic Pump Control

View and Edit Project In Cirkit DesignerImage of Arduino-Based Ultrasonic Water Level Monitoring System with LCD Display and Automatic Pump Control

Circuit Documentation

Summary

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.

Component List

  1. HC-SR04 Ultrasonic Sensor

    • Description: Measures distance using ultrasonic waves.
    • Pins: VCC, TRIG, ECHO, GND
  2. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  3. LCD Display 20x4 I2C

    • Description: 20x4 character LCD display with I2C interface.
    • Pins: SCL, SDA, VCC, GND
  4. Mini Diaphragm Water Pump

    • Description: Small water pump for liquid transfer.
    • Pins: Positive (+), Negative (-)
  5. KY-019 Relay module 1 channel

    • Description: Relay module for switching high-power devices.
    • Pins: S, 5V, GND, NC, COM, NO
  6. 18650 Li-Ion

    • Description: Rechargeable lithium-ion battery.
    • Pins: Positive, Negative

Wiring Details

HC-SR04 Ultrasonic Sensor

Arduino UNO

LCD Display 20x4 I2C

Mini Diaphragm Water Pump

KY-019 Relay module 1 channel

18650 Li-Ion

Documented Code

#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.