Maker Pro
Arduino

How to Make a Water-Level Indicator With Arduino

August 14, 2016 by Aravind Jayan
Share
banner

Use an Arduino and an ultrasonic module to determine the water level in a tank.

Hardware

Tools

1 10K resistor
1 16Mhz crystal
2 22pf disc capacitors
0 Connecting wires
1 LM7805 5V regulator
1 9V battery and connector
1 10uF electrolytic capacitor

Software

0 Arduino IDE

Knowing the amount of water in an overhead tank can be one tedious task. Usually, you’ll end up climbing up the stairs to the tank and checking the level manually or you'll hear the water overflowing from the top. But these days electronic water level indicators are available to fix this problem, but they often come with a hefty price tag and are usually difficult to install. Most of the available systems use dipped electrodes or float switches, which can be a headache in the long run. We present a different approach to knowing the water level using an Ultrasonic module with Arduino. The advantage of this method is that it is contactless, so issues like corrosion of the electrodes won’t affect this system. Furthermore, this Arduino water level indicator it is much easier to install than regular systems.

How Does the Arduino Water Level Indicator Work?

This Arduino water level indicator uses an ultrasonic sensor or Ping sensor to determine the level of water in the tank. The Ping sensor measures distance using sonar. An ultrasonic (well above human hearing) pulse is transmitted from the unit and distance-to-target is determined by measuring the time required for the echo return. Output from the Ping sensor is a variable-width pulse that corresponds to the distance to the target. This is then fed to the microcontroller that determines the water level and displays it through a series of LEDs.

The following project can be interfaced to an Arduino board if you have one or directly to an ATmega 328 microcontroller on a breadboard. You can also check out the Maker Pro tutorial from Jeff to learn to interface an ultrasonic sensor to Arduino.

Code for the Arduino Water Level Indicator

Copy-paste the provided sketch in the Arduino IDE and find the line “ int d=18;” and change ‘18’ to the depth of your tank in centimeters.
//Coded by MATHEW VARGHESE
//Note that the numbering of arduino pins are different from microcontroller pinout

int d = 18; //Enter depth of your tank here in centimeters

int trig = 11; // Attach Trig of ultrasonic sensor to pin 11
int echo = 10; // Attach Echo of ultrasonic sensor to pin 10
int pin1 = 2;//Highest level
int pin2 = 3;
int pin3 = 4;
int pin4 = 5;
int pin5 = 6;
int pin6 = 7;//Lowest evel

void setup() {
pinMode (pin1, OUTPUT);// Set pins to output for controlling I/O
pinMode (pin2, OUTPUT);
pinMode (pin3, OUTPUT);
pinMode (pin4, OUTPUT);
pinMode (pin5, OUTPUT);
pinMode (pin6, OUTPUT);
}

void loop()
{ digitalWrite(pin1, LOW);//Resetting the LEDs to off state
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin5, LOW);

// Establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, in, cm;       //'in' is inches and 'cm' is centimeter

// The PING is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);

// The same pin is used to read the signal from the PING: a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echo, INPUT);
duration = pulseIn(echo, HIGH);

// Convert the time into a distance
in = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

delay(100);// To save battery,remove if felt inconvenient
if (in < 6 * d / 7)//   Else is included to light only one led at a level and thus save battery charge
digitalWrite(pin1, HIGH);
else if (in < 5 * d / 6)
digitalWrite(pin2, HIGH);
else if (in < 4 * d / 6)
digitalWrite(pin3, HIGH);
else if (in < 3 * d / 6)
digitalWrite(pin4, HIGH);
else if (in < 2 * d / 6)
digitalWrite(pin5, HIGH);
else if (in < 1 * d / 6)
digitalWrite(pin5, HIGH);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING, there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PI...
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Making the Connections

Populate the circuit on a PCB or breadboard following the attached Fritzing diagram. This is for an ATMEga328 on a breadboard running Arduino.You can follow Mayoogh Girish's tutorial to make your own Arduino board using ATMega328 on a breadboard. If you are using an Arduino board, you can just make the connections for the LEDs and the ultrasonic sensor as below.

Uploading the Code

Burn the code for the Arduino water level indicator directly onto the Arduino board or into an ATMega328P microcontroller. 

Interfacing the Ultrasonic Sensor on the Water Tank

Fix the Ping sensor so it directly faces the water in the tank. The main control board with indication LEDs can be fixed inside the home at any comfortable position. Any multi-cored cables (ethernet cable) can be used to connect the Ping sensor and the rest of the circuitry. Keep in mind not to increase the length between the components more than 20mts.

Now just hook up a battery and your Contactless Arduino water level indicator is ready for use.

Related Content

Comments


You May Also Like