In this project, we’ll see how to display and calculate the filling percentage of your water tank. This can be very useful if you have a tank for collecting rainwater or if you have a submerged tank and cannot see the water level.

Required materials

Now, here are the materials needed for the project:

  • An Arduino Uno board

  • A 16×2 Liquid Crystal LCD screen

  • A water pump

  • A distance sensor

  • A potentiometer

  • A relay

  • A 220-ohm resistor

  • An external power supply (9V battery)

  • Jumper wires (around fifteen!)

How does the filling system work?

There are two parts to this project:

  1. Detecting the filling level of the tank

  2. Watering with water from the tank

For detecting the filling level, we use a distance sensor aimed at the bottom of the tank. The water at the bottom reflects the signal sent by the sensor back, which returns a distance measurement. We then display the filling percentage based on the tank’s original size.

To water your garden, we use a water pump. It runs on 9V, so we need an external power supply because the Arduino board can only provide up to 5V. We use a relay to control the water pump from the Arduino board.

Additionally, we added a switch to turn off the pump when you finish watering. This switch controls only the pump, allowing you to keep monitoring the filling level even when the pump is off.

#include <LiquidCrystal.h> // Include library for the LCD screen

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the LCD pins
int cm = 0; // Initialize distance in centimeters
int pump = 8; // Pump pin
bool pumpOff = HIGH; // Boolean for turning pump off
bool pumpOn = LOW;   // Boolean for turning pump on

void setup() {
  pinMode(pump, OUTPUT); // Set pump pin as output
  lcd.begin(16, 2);      // Initialize the LCD with size 16x2
  digitalWrite(pump, pumpOn); // Initially turn pump ON
}

void loop() {
  cm = 0.01723 * readUltrasonicDistance(7, 7); // Convert sensor reading to centimeters
  int level = map(cm, 400, 10, 0, 100);        // Map cm to percentage based on tank size

  lcd.setCursor(0, 0); 
  lcd.print("Filling Level:"); // Display filling level text
  lcd.setCursor(0, 1); 
  lcd.print(level);
  lcd.setCursor(3, 1);
  lcd.print("%");

  if (level >= 99) {          // If tank is almost full, turn pump OFF
    digitalWrite(pump, pumpOff);
  } 
  else if (level < 95) {      // If tank is below 95%, turn pump ON
    digitalWrite(pump, pumpOn);
  } 
  else {
    digitalWrite(pump, pumpOff); // Turn pump off if level can't be determined properly
  }
}

long readUltrasonicDistance(int triggerPin, int echoPin) {
  pinMode(triggerPin, OUTPUT);  
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  return pulseIn(echoPin, HIGH);
}