Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino-Controlled Ultrasonic Water Level Monitoring and Pump Management System

Image of Arduino-Controlled Ultrasonic Water Level Monitoring and Pump Management System

Circuit Documentation

Summary

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.

Component List

HC-SR04 Ultrasonic Sensor

  • Pins: VCC, TRIG, ECHO, GND
  • Description: This sensor measures distance by emitting ultrasonic waves and measuring the time taken for the echo to return.

Arduino UNO

  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
  • Description: A microcontroller board based on the ATmega328P, used for processing sensor data and controlling the relay and LCD display.

LCD Display 20x4 I2C

  • Pins: SCL, SDA, VCC, GND
  • Description: A 20x4 character LCD display with an I2C interface, used to display the water level and pump status.

Mini Diaphragm Water Pump

  • Pins: Positive (+), Negative (-)
  • Description: A pump used to move water when the water level in the tank is below a certain threshold.

KY-019 Relay Module 1 Channel

  • Pins: S, 5V, GND, NC, COM, NO
  • Description: A relay module used to control the high-power circuit of the water pump.

18650 Li-Ion Battery

  • Pins: Positive, Negative
  • Description: A rechargeable battery providing power to the water pump through the relay module.

Wiring Details

HC-SR04 Ultrasonic Sensor

  • VCC: Connected to 5V from Arduino UNO
  • TRIG: Connected to pin D7 on Arduino UNO
  • ECHO: Connected to pin D8 on Arduino UNO
  • GND: Connected to GND on Arduino UNO

Arduino UNO

  • 5V: Provides power to the HC-SR04, Relay Module, and LCD Display
  • GND: Common ground for all components
  • A4 (SDA): Connected to SDA on LCD Display
  • A5 (SCL): Connected to SCL on LCD Display
  • D6: Connected to S (Signal) on Relay Module
  • D7: Connected to TRIG on HC-SR04
  • D8: Connected to ECHO on HC-SR04

LCD Display 20x4 I2C

  • SCL: Connected to A5 (SCL) on Arduino UNO
  • SDA: Connected to A4 (SDA) on Arduino UNO
  • VCC: Connected to 5V from Arduino UNO
  • GND: Connected to GND on Arduino UNO

Mini Diaphragm Water Pump

  • Positive (+): Connected to NO (Normally Open) on Relay Module
  • Negative (-): Connected to Negative on 18650 Li-Ion Battery

KY-019 Relay Module 1 Channel

  • S: Connected to pin D6 on Arduino UNO
  • 5V: Connected to 5V from Arduino UNO
  • GND: Connected to GND on Arduino UNO
  • NO: Connected to Positive (+) on Mini Diaphragm Water Pump
  • COM: Connected to Positive on 18650 Li-Ion Battery

18650 Li-Ion Battery

  • Positive: Connected to COM on Relay Module
  • Negative: Connected to Negative (-) on Mini Diaphragm Water Pump

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