Water Level Indicator and Control using Ultrasonic Sensor and Arduino

One of the projects that have been done by so many students is the automated flow switch system that comes under various names like Water level Indicator using Arduino and Ultrasonic sensor, Water level monitor and control, automatic flow switch, water level sensor, water level controller.

you can watch the video of the design here

What the project does basically is “monitor the level of a liquid in the tank or reservoir, indicate when the liquid has reached certain thresholds and initiate an actuation mechanism, either by turning an LED ON or OFF or by switching a supply unit ON or OFF”. These devices can be built in different ways using different electronic components and sensors. The commonest sensor to use for this type of project is a metallic contact sensor. This sensor is simply 2 metallic strips separated by an insulator (air or plastic). See image below:

Metallic contact sensor
Figure 1: Metallic Contact Sensor

How the sensor works is very simple, the metallic sensor is dipped into the tank, when water touches the two metallic strips simultaneously, it bridges the separation between the strips thereby creating a poor conductive path for electric current to flow from one strip to another. Because water is a poor conductor, the amount of current that will pass through the strip is small but enough to bias a transistor which in turn can switch a system connected to it.

Typical demonstration of how metallic contact is used for water level monitoring
Figure 2: Typical demonstration of how metallic contact is used for water level monitoring.

From the figure above, we can see that current flows from the battery and enters the metallic strip on the left, if water has touched the two metallic strips, it will serve as a conductor for current to flow and enter the other strip, when that happens, current will flow through the metallic strip on the right to bias the base of the NPN transistor and switch ON the LED. This is a basic water level monitoring system. Though this particular example showcases how to monitor the level of water in the tank, it can also be used to switch the pump ON or OFF. This can be achieve with a more sophisticated but easy to assemble setup using two different metallic contact sensors, one at the top and one at the bottom, for activating switching ON and switching OFF of the pump.

Advantages Of Using Metallic Contact Sensors

The advantages of using a metallic contact sensor in designing automatic water level monitoring and control include:

  • Design materials are cheap
  • Few components are needed
  • Easy to design circuitry
  • Fast response

Disadvantages Of Using Metallic Contact Sensors

  • Contamination of water
  • Use of too many wires
  • Corrosion of metals due to repeated wetting and drying of the metallic strips
  • Can only be used for electric conductive fluids, as a result, this design only be used for water level monitoring and control.

However, with the advent of the Arduino Microcontroller and its compatible sensors like the HC-SR04 Ultrasonic Sensor, such design can be done with little stress and very efficiently. A lot of this design abounds in the internet bearing names like:

  • Arduino water level controller using ultrasonic sensor HC-SR04
  • Water level controller using arduino and ultrasonic sensor code
  • Arduino water level indicator
  • Water level controller
  • Arduino water level sensor
  • Arduino water level sensor using ultrasonic sensor, etc.

Arduino Water Level Indicator and Control using Ultrasonic Sensor HC-SR04R

The circuit will do basically what the aforementioned one will do rather efficiently.

Materials needed for this arduino water level indicator and control using Ultrasonic Sensor HC-SR04R:

  • Arduino UNO R3 (1)
  • HC-SR04 ultrasonic sensor (1)
  • Breadboard (1)
  • Bunch of Jumper wires
  • Bucket 20cm high (1)
  • 330 ohms resistor (4)
  • 5 volts power supply (1)
  • 5 volts relay (1)
  • AC bulb (1)
  • Lamp holder (1)
  • Tip122 Transistor (1)
  • IN4001 diode (1)

Before we discuss how the circuit works, let’s talk about the two key components in the circuit.

Arduino Microcontroller

I have made a comprehensive tutorial on Arduino microcontroller.

Arduino Uno R3 board
Figure 3: Arduino Uno R3 board

HC-SR04 Ultrasonic Distance Sensor

The HC-SR04 ultrasonic sensor is a distance sensing sensor; it works by sending and receiving ultrasonic signal out with its transducers.

Here is a brief specification of the HC-SR04 ultrasonic sensor:

Basic specification of an Ultrasonic distance sensor
Figure 4: Basic specification of an Ultrasonic distance sensor
Front and back views of an ultrasonic distance sensor
Figure 5: Front and back views of an ultrasonic distance sensor
Pin configuration of the Ultrasonic distance sensor
Figure 6: Pin configuration of the Ultrasonic distance sensor

Brief description of how Ultrasonic distance sensor works

I will make a complete tutorial on how the ultrasonic distance sensor works, for the sake of this very tutorial; I will give a simple and concise explanation of how the sensor works. Before that, it will be of a good assistance if I first show you a basic circuit connection of an Arduno and the HC-SR04 Ultrasonic distance sensor.

Simple arduino connection with HC-SR04 Ultrasonic distance sensor
Figure 7: Simple arduino connection with HC-SR04 Ultrasonic distance sensor

In the image above, the VCC pin of the arduino is connected to 5V pin of the Arduino MCU, the GND pin is connected to the ground pin of the arduino, the Trigger pin is connected to pin 9 of the arduino, while the Echo pin is connected to pin 10 of the arduino pin.

Tabular representation of arduino connection with ultrasonic distance sensor
Figure 8: Tabular representation of arduino connection with ultrasonic distance sensor

Ultrasonic distance sensor working description with arduino

First of all, we have to understand that the nature of the result produced with the ultrasonic distance sensor depends on the arduino code written for the design. Meanwhile, let’s discuss how the ultrasonic distance sensor works.

The sensor has two transducer bands, the trigger band and echo band, the trigger band sends out ultrasonic signals from the sensor, if this signal that was sent out bounces on any obstacle within the distances of 2 and 400 cm and reflects back to the sensor, the echo band will receive the reflected signal. The time it took the signal to travel and return back will be recorded by the sophisticated circuitry in the sensor, with this time which ranges from 150µS to 25000µS, we can compute the distance of the sensor from the obstacle using the conventional formula for calculating distance in physics multiplied by a factor of 2.

Example calculation:

Let us assume that the time it took the ultrasound signal to travel and return after bouncing on a barrier is 250µS. to calculate the distance of the barrier from the ultrasonic sensor, we utilize the formula for calculating speed, which is

Speed = Distance / Time

However, the distance covered in this journey of the ultrasound is the sum of the travel distance and the return distance, which is two times the travel distance (since the ultrasound travels and returns at the same point) hence, the formula becomes:

Speed = 2 x Distance / Time

Speed of sound= 340m/s
Time = 250µS
Distance = 2 x Distance

The unit of speed in our formula is m/s; we need to convert it to cm/µS to do so by multiplying 340 by 100 divide by 1,000,000, that is:

Speed in cm/µS = 340 x 100 / 1,000,000 = 0.034cm/µS  

Substituting the value of speed and time the formula above, we can compute the value of the distance as follows:

Distance = 250 x 0.034 / 2 = 4.5cm

However, before the trigger band sends out the ultrasonic signal, it must be created somehow using arduino code. Here is how to do it.

Consider figure 7 above, the trigger pin is connected to pin 9 of the arduino. Now, in the arduino code, a 10µS pulse is created on this pin 9 of the arduino that is connected to the trigger pin of the sensor. This 10µS pulse in conjunction with the electronic circuitry of the ultrasonic sensor creates a sonic 8 burst pulses having a frequency of 40KHz. It is this 40KHz ultrasound that is sent out by the trigger band and received by the echo band when reflected.

Implementing the circuit for water level monitoring

One good thing about ultrasound of this frequency is that it can reflect off liquids, so we can utilize this technology to measure the level of liquids in a tank. All we need to do is program the arduino in such a way that when the liquid is at certain distances from the sensor certain things happen, maybe an LED comes ON goes OFF, or a relay is energised to turn a switch ON or OFF.

Hence, in this project, I will show you how to visually monitor the level of water in a tank using an LED indicator and automatically switch a water pump ON and OFF when water in the said tank goes below and above certain thresholds in the tank respectively.

Circuit diagram of the design is shown below

Circuit diagram of Arduino Water Level Indicator and Control using Ultrasonic Sensor HC-SR04R
Figure 9: Circuit diagram of Arduino Water Level Indicator and Control using Ultrasonic Sensor HC-SR04R

Below is the code of the design with the explanation of the codes written inside the as comments.

// defines pins numbers
const int trigPin = 9; // pin on the arduinowhere the trigger pin is connected
const int echoPin = 10;// pin on the arduino where the echo pin is connected
const int led1 = 2; // water level indicator 1
const int led2 = 3; // water level indicator 2
const int led3 = 4; // water level indicator 3
const int led4 = 5; // pin connected to the base of NPN transistor through a resistor for switching the pump ON and OFF

// defines variables
long duration; // variable where the the reflection time of the ultrasound is stored
int distance; // variable where the distance of the measured object is stored


void setup()
{
  pinMode (led1, OUTPUT);// sets as output
    pinMode (led2, OUTPUT);//sets as output
      pinMode (led3, OUTPUT);//sets as aoutput
       pinMode(led4, OUTPUT);//sets as output
         pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
           pinMode(echoPin, INPUT); // Sets the echoPin as an Input
         Serial.begin(9600); // Starts the serial communication
         }
     
     void loop()
 {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
     
     // Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
     digitalWrite(trigPin, LOW);
     
// Reads the echoPin, returns the sound wave travel time in microseconds
   duration = pulseIn(echoPin, HIGH);
   
// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay (1000);

if ( distance <=15)// when water has gotten to this level or is less than this level
{ 
 digitalWrite (led1, HIGH);// led1 comes ON and remains ON
}
     else // otherwise               
       { 
    digitalWrite(led1, LOW);  //led1 goes OFF and stays OFF
       }
if (distance >=16)// when the level of water has gotten to this level             
 { 
  digitalWrite(led4, HIGH);// switch ON the Tap
 }
 
if ( distance <= 11)// when water has gotten to this level or is less than this level 
{ 
  digitalWrite (led2, HIGH);//switch ON the led2
}
     else// otherwise
       {
  digitalWrite(led2, LOW);//led2 goes OFF and stays OFF
       }
  
 if( distance <= 5 )// when water has gotten to this level or is less than this level
{
  digitalWrite (led3, HIGH);//switch ON the led3
}
      else//otherwise
       {
  digitalWrite (led3, LOW);//led3 goes OFF and stays ON
       }
    
if (distance <= 5)// when the level of water has gotten to this level    
      {
   digitalWrite(led4,LOW);//led4 goes OF and stays OF
      }
 }

Outcome of the circuit diagram of Arduino Water Level Indicator and Control

If the circuit is constructed as shown above, if initially the water in the tank is below minimum threshold, and the system is turned ON, the pump will start pumping water into the tank, and no LED will come ON, but immediately water reaches the minimum threshold, the red LED will come ON, indicating that the tank is one third full. As water keeps pumping, when the tank is two third full the blue LED will come ON, and when the tank is full, the white LED will come ON while the pump switches OFF automatically. If water starts receding from the tank, the LEDs will start going OFF gradually to indicate the level of water in the tank as it recedes. Finally when water has gotten to the minimum threshold, the pump will come ON again.

The general explanation of the circuit operation is as follows

After the sketch is sent to the arduino board and the board is powered, the system will kick into action;the ultrasonic sensor will send out ultrasound and receives it back when the signal hits the barrier whose distance from the sensor is to be measured. The time it took the signal to travel and return is computed and with the code in the arduino, the distance of the barrier will be measured. The arduino is programmed to initiate actions with various values of distances measured by the ultrasonic sensor. In the design as can be shown in the coding, let’s assume the tank is 20cm high, there is no water in the tank and the system is ON, because the distance measured by the sensor at this moment is >= 15cm, pin 5 goes high and biases a transistor which drives the relay to switch ON the pump. When water has been pumped into the tank up to the point that thesensor measures a distance of 14cm, the red LED comes ON, again as water keeps filling up the tank and gets to the point the sensor measures 8cm, the blue LED comes ON, finally when water gets close to the brim of the tank where the sensor measures a distance <=3cm, the white LED comes ON indicating that the tank is full, and at this point, pin 5 of the arduino goes LOW, thereby cutting supply to the transistor and relay, this in turn switches OFF the pump. When water starts receding in the tank, the same process is executed but this time in reverse. The white LED goes OFF followed by the blue and the red, at the point the red LED goes OFF the pump comes ON again and the circle keeps repeating. With this system water will not lack the tank as long as there is power.

Importance and application of the Automatic water level monitor and control

  1. By switching the pump on and off automatically, power is conserved
  2. By switching the tank off when the tank is full, water is conserved
  3. By having water pumped into the tank when the tank is near empty makes water readily available in the tank
  4. The system ensures that water is not littered in the environment, hence keeping the surroundings clean and dry.
  5. The system is a test of technology and scientific application
  6. The design and sales of this product brings about job and wealth creation. Etc.

2 Comments on “Water Level Indicator and Control using Ultrasonic Sensor and Arduino”

  1. i have the same project ,but me i have to use 5 leds and 5 levels that are : 10% , 25% , 50% ,75% ,100%
    i have a question how can i find the distances for every level ???
    how did you fined 15, 16, 11, 5 ??????????

    1. Thanks for reaching out. I used ultrasonic sensor to measure the distances and then used the distances measured to program the Arduino

Leave a Reply

Your email address will not be published. Required fields are marked *