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

Arduino-Controlled Ultrasonic Water Level Indicator with LCD Display and Relay Switching

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Ultrasonic Water Level Indicator with LCD Display and Relay Switching

Circuit Documentation

Summary

This circuit is designed to monitor water levels using an HC-SR04 Ultrasonic Sensor and control a pump via a 1-Channel Relay based on the detected water level. The system's status and water level percentage are displayed on an LCD I2C Display. User interaction is facilitated through a Pushbutton and a Rocker Switch, allowing manual control over the pump and setting the water level threshold. The Arduino UNO serves as the central processing unit, running the embedded code to manage sensor readings, display updates, and relay control.

Component List

HC-SR04 Ultrasonic Sensor

1-Channel Relay (5V 10A)

LCD I2C Display

Arduino UNO

Pushbutton

Rocker Switch

Wiring Details

HC-SR04 Ultrasonic Sensor

1-Channel Relay (5V 10A)

LCD I2C Display

Arduino UNO

Pushbutton

Rocker Switch

Documented Code

#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

long duration, inches;
int set_val, percentage;
bool state, pump;

void setup() {
  
  lcd.begin();
  lcd.print("WATER LEVEL:");
  lcd.setCursor(0, 1); 
  lcd.print("PUMP:OFF MANUAL");
  
  pinMode(2, OUTPUT);
  pinMode(3, INPUT);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  
  set_val = EEPROM.read(0);
  if (set_val > 20) set_val = 20;
}

void loop() {
  digitalWrite(2, HIGH);
  delayMicroseconds(10);
  digitalWrite(2, LOW);
  duration = pulseIn(3, HIGH);
  inches = microsecondsToInches(duration);
  
  percentage = (set_val - inches) * 110 / set_val;
  
  lcd.setCursor(12, 0); 
  if (percentage < 0) percentage = 0;
  lcd.print(percentage);
  lcd.print("%   ");
  
  if (percentage < 30 & digitalRead(11)) pump = 1;
  if (percentage > 85) pump = 0;
  digitalWrite(13, !pump);
    
  lcd.setCursor(5, 1);
  if (pump == 1) lcd.print("ON ");
  else if (pump == 0) lcd.print("OFF");

  lcd.setCursor(9, 1);
  if (!digitalRead(11)) lcd.print("MANUAL ");
  lcd.print("AUTO   ");
  
  if (!digitalRead(10) & !state & digitalRead(11)) {
    state = 1;
    set_val = inches;
    EEPROM.write(0, set_val);
  }
  
  if (!digitalRead(10) & !state & !digitalRead(11)) {
    state = 1;
    pump = !pump;
  }
  
  if (digitalRead(10)) state = 0;
  
  delay(500);
}

long microsecondsToInches(long microseconds) {
  return microseconds / 29 / 2;
}

This code is responsible for initializing the LCD display and setting up the pins for the ultrasonic sensor, pushbutton, rocker switch, and relay. It reads the water level using the ultrasonic sensor, calculates the percentage of water level, and displays it on the LCD. The relay is controlled based on the water level and the state of the rocker switch, allowing for manual or automatic pump control. The pushbutton is used to set the water level threshold and toggle the pump manually. The EEPROM is used to store the threshold value even after the Arduino is powered off.