Measuring Water Level With Ultrasonic Sensor

215,313

124

62

Introduction: Measuring Water Level With Ultrasonic Sensor

About: I am an engineer and a maker who loves technology challenges.

For my next big project (automated plant watering system) I was in need of having water level meter. I was choosing from contact and contactless methods of measuring fluid level in tank. Contact methods are resistive method, capacitive and inductive methods (magnetostriction). Most often contactless methods are optical method, radar and ultrasonic method. Because we didn’t want to affect the quality of water in tank we implement one of the contactless methods.

What method to choose? All contactless methods work on same principle: we send a signal and we measure time that send signal needs to come back. Optical method uses optical signals. Optical method can be very accurate, but sensors can get dirty over time and we are not able to make measurement at all. Radar method uses radar signals. Because of that (radar signals are high RF signals) it is not suitable for DIY. Ultrasonic method is similar to radar. Instead of radar wave we are sending ultrasonic wave. This procedure is ideal for our needs because ultrasonic sensors are accessible and low priced.

We made water level meter with Arduino platform (we used Arduino Mega2560, but any arduino will work).

For damage occurred during reproduction I am not hold responsible.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Parts and Materials

Parts:

- Arduino (Uno, Mega 2560,...)

- ultrasonic sensor HC SR04

Materials:

- wires for connecting censor to Arduino

- acrylic glass for housing (optional)

Step 2: Theory Behind Ultrasonic Level Sensor

First, let us talk about some theory behind ultrasonic method of fluid lever measuring. The idea behind all contactless methods is to measure distance between transceiver and fluid. As said before, we transmit short ultrasonic pulse and we measure travel time of that pulse from transceiver to liquid and back to transceiver. Ultrasonic pulse will bounce from liquid level since because change of density of ultrasonic pulse travel medium (ultrasonic pulse first travel through air and bounce of liquid with higher density than air). Because water has higher density, majority of pulse will bounce off.

Two disadvantages exist with ultrasonic method:

- 1st: because of pulse length there is small window that we cannot receive pulse with transceiver because transceiver is transmitting. This problem is simple to solve: we placed our sensor higher from maximum water level for few centimeters allowing receiver to start receiving.

- 2nd: because of the beam width we are limited with tank diameter. If tank diameter is too small, signal could bounce of tank’s walls and could cause false readings.

Before installing sensor in tank we tested it for those two disadvantages. We established that we could have stable measurements from minimum distance of 5 cm from sensor. That means, we must install our sensor 5 cm higher then maximum water level. We also established that we didn’t have any problems with signal bouncing from tank’s walls with 7.5 cm diameter tank (tank’s length was 0.5 m). We complied these two results at construction of water tank and at the setting up of ultrasonic sensor.

Step 3: Water Tank

Watering system will use free fall for water feeding. Because of that, water tank needs to be lifted up from floor level. We made water tank from drainpipe 1m in length and 16 cm in diameter. We divided pipe into two sections. Lover section (first 50 cm) will house valves. Upper section will serve as water tank. On the upper section we are using pipe end cap as water tank cover. On end cap we mounted ultrasonic sensor. For larger stability we added wooden base, which will also house electronics and battery pack.

Here comes the mathematical specification of the tank. This part is essential, because we want to codify fluid height measurement in %. Starting point of codifying is measurement alone. Measurement can be between 6 and 56 cm (6 cm offset). This is codified into 0 to 100 %. It comes down to simple cross calculus.

We chose homogeneous tank because of easier calculations of volume (we are using pipe – cylindrical shape). Diameter of pipe is the same through the length of the pipe. We have also made equation whit which we can measure volume of water still in the tank. We didn’t implement this because there was no need for it. For now!

Step 4: Ultrasonic Sensor, Schematics

We soldered wires to ultrasonic sensor (we used FTP or UTP cable; it can be one of them). Then we installed sensor in small custom made housing from acrylic glass. Casing with sensor in it was sealed off and mounted on tank’s cover. Housing was a bit improvised and it is not essential. Because of that, there are no picture and no plans for it. You can figure it out somehow by yourself.

We connected sensor on Arduino board following schematic in picture.

Step 5: Program

We converted program for measuring distance to program for measuring water level. Program for measuring distance is not of our making but was found on internet in this tutorial we cannot find anymore.

First we transmit signal and then we wait and measure time between transmitted signal and received signal. This time is then converted to centimeter and centimeters are then converted to % and send via serial connection to computer. We could also calculate water volume that is still in the tank.

Step 6: Testing

Because in the future we wish to implement automatic watering system with two stages regulator, we must measure tank’s flow characteristic. Question is why we must do that? You see, outgoing flow in the tank depends on hydrostatic pressure inside of the tank. With basic knowledge of physics anybody see that hydrostatic pressure id decreasing with falling water level in tank. Because we want to feed plants every time with same amount of water, we must adjust valve opening time. With tank’s flow characteristic we can calculate how much water can flow out of tank at any time and with that we can determine how long valve must stay in open position.

Also we wanted to test our level meter. We filled up water tank to maximum height. Then we opened a valve and let all the water run out. Because drain pipe is mounted to prevent sucking out sediments, tank was emptied to 2%. In picture is presented response to step function. From this response we can approximate function on which water level is changing (with Excel, Matlab, or other powerful mathematical tool).

We can conclude that sensor works in accordance with expectations.

Step 7: What's Next?

Implemented water level meter serves as a concept of principle. If we would want to use this meter in DIY project and in semi industrial or other applications we would have to make test of sensor endurance and resistance to water splattering. After that test we would be able to see if sensor is appropriate for use in DIY projects or any other environments. Right now I can only say that sensor is working fine within this short period of time.

Because sensor is measuring water level with contactless method water can stay unspoiled. Implemented meter is also cheap and accessible and because of this it is very suitable for DIY.

Please feel free to comment and let me know if I made any grammar mistakes (english is not my first language).

5 People Made This Project!

Recommendations

  • Tiny Speed Challenge

    Tiny Speed Challenge
  • Clocks Contest

    Clocks Contest
  • PCB Design Challenge

    PCB Design Challenge

62 Discussions

0
maartenweckhuysen
maartenweckhuysen

4 days ago

Hi,

Thanks for the inspiration !
I made some adjustments to send the data on mqtt. The pins you use do not work together with an Ethernet Shield W5100. I used pins 8 and 9 for this setup.

the code ;):

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Function prototypes
void subscribeReceive(char* topic, byte* payload, unsigned int length);
// Set your MAC address and IP address here
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 160);
// Make sure to leave out the http and slashes!
const char* server = "192.168.1.16";
// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
//Begin Water Measuring
// fill in you're measurements
int heightwell = 200; //inside height of the well in cm
int diameter = 235; //inside diameter of the wel in cm
int volume = 8760; //total volume of the well (measuring from the bottom to the lower edge of the overflow)(volume of cilinder = pi*r²*height)
long WaterVolume = "WaterVolume"; //mqtt topic for the volume
long WaterVolumePercent = "WaterVolumePercent"; //mqtt topic for the %
int trigPin = 9; // Trigger
int echoPin = 8; // Echo
float restvolume;
int liters;
int restdistance;
int radius;
int percent;
long duration, distance;
//End Water Measuring
void setup()
{
Serial.begin (9600);
//Begin Water Measuring
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//End Water Measuring
// Start the ethernet connection
Ethernet.begin(mac, ip);
// Ethernet takes some time to boot!
delay(3000);
// Set the MQTT server to the server stated above ^
mqttClient.setServer(server, 1883);
// Attempt to connect to the server with the ID "myClientID"
if (mqttClient.connect("myClientID"))
{
Serial.println("Connection has been established, well done");
// Establish the subscribe event
mqttClient.setCallback(subscribeReceive);
}
else
{
Serial.println("Looks like the server connection failed...");
}
}
void loop()
{
//Begin Water Measuring
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: 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(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
distance = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.0343
restdistance = heightwell - distance;
radius = diameter / 2;
restvolume = (3.14 * (radius * radius) * restdistance) / 1000;
percent = (restvolume / volume) * 100;
Serial.print("distance: ");
Serial.print(distance);
Serial.print("cm ");
Serial.print("restvolume: ");
Serial.print(restvolume);
Serial.print("L ");
Serial.print("percent: ");
Serial.print(percent);
Serial.print("%");
Serial.println();
//Message String
char distString[5];
dtostrf(restvolume, 4, 0, distString);
char percString[5];
dtostrf(percent, 4, 0, percString);
//End Water Measuring
//MQTT Begin
// Ensure that we are subscribed to the topic
mqttClient.subscribe(WaterVolume);
// Attempt to publish a value to the topic
if(mqttClient.publish(WaterVolume, distString))
{
Serial.println("Publish Volume success");
}
else
{
Serial.println("Could not send Volume :(");
}
// Ensure that we are subscribed to the topic
mqttClient.subscribe(WaterVolumePercent);
// Attempt to publish a value to the topic
if(mqttClient.publish(WaterVolumePercent, percString))
{
Serial.println("Publish VolumePercent success");
}
else
{
Serial.println("Could not send VolumePercent :(");
}
// Dont overload the server!
delay(4000);
}
void subscribeReceive(char* topic, byte* payload, unsigned int length)
{
// Print the topic
Serial.print("Topic: ");
Serial.println(topic);
// Print the message
Serial.print("Message: ");
for(int i = 0; i < length; i ++)
{
Serial.print(char(payload[i]));
}
// Print a newline
Serial.println("");
}
//MQTT End


kind regards

1
andytechdude
andytechdude

1 year ago

Great use of ultrasonics!

Refering to your comments regarding the longevity of the system. Past work experience with the cheap readily available ultrasonic transducers has taught me that they do not like water. The aluminium case and grille corrode and the internals of the piezo electric transducer eventually gives up. There are water proof versions as used on car reversing devices but their sesitivity is much lower than the cheap common open versions. The reduced sensitivity may be OK for shorter range and larger targets that produce larger echo.

A few tips for increasing the life of these open transducers.

1/ Prevent water spray onto the transducer. In the past I have used barrier materials to take the energy out of spray and prevent the majority of direct water contact. Utrasonics have strange properties and what may look opaque to you may be perfectly transparent to utrasonics. The coarse nylon material that pan scrubbers are made from produces a good barrier that drains easily and the transducer simply "sees" straight through.

2/ Humidity. The interior of a water tank gets very humid under the right conditions and that humidity will make short work of your tranducers. Try to provide ventilation arround your tranducers to keep the humidity low, vent to the outside. Your transducers can "look" in through a well placed hole while enjoying the fresh dry air outside the tank.

Ultrasonics may pick up features that you do not wish them to see. Features on a tank wall or the rim of a hole for example. The beam shape from ultrasonic transducers can be shaped to prevent this. Similarly as you can with light, reflectors and baffles may be applied to tranducers to shape / limit the beam. Trial & error is the only easy method for the home inventor for getting this right.

Minimum detection range. When attempting to detect in the coser ranges, problems can be encountered due to cross-talk directly between the tranducers. Software can be modified to include a "dead zone" an echo time interval below which the readings are ignored. Isolating the transmitter and receiver sound can also help with this problem.

Keep inventing.

0
Kaveman™️
Kaveman™️

Reply 14 days ago

Hello
You have displayed vast knowledge of the project. I am researching on building a fuel level remote sensing system. Is this sensor suitable or can you suggest a better sensor for the project? Secondly, if you have any other aid or advice you can render to my successful completion of the project, kindly share with me. WHATSAPP +2348149830651

0
andytechdude
andytechdude

Reply 12 days ago

I would advise against the use of this system in a fuel environment. I personally would look into approved technologies such as those used in vehicle fuel tanks and adapt to suit your own application. Inventing is fun but all engineering comes with a responsibility for safety. Good luck with your project.

0
ChristopherS17
ChristopherS17

6 weeks ago

HC-SR04 measures distances of 2cm to 400cm

0
minkhantzaw1210
minkhantzaw1210

9 months ago

I'm trying this for my school project, please help me whit code.I can't download the code.please can anyone help me with code for this project?

0
Jurinstar
Jurinstar

Question 11 months ago

Hello, can this same procedure be used to determine the level of gas in a gas bottle,? instead of placing the transducer above the liquid level guess it will be placed at the bottom

0
DwoodsF
DwoodsF

3 years ago

can this sensor pass sound wave through arcylic sheet ? or you need to make two holes for each sensor ?

reply at

look4ursoul@live.com

thanks

0
harshjainLDH1
harshjainLDH1

Reply 1 year ago

The acrylic will reflect part of the ultrasonic and the rest will go further and get detected when it comes back. You may need to ignore this time by including it into dead zone as @andytechdude mentioned.

0
muralivirat
muralivirat

1 year ago

does this applicable for bigger tanks like water tanks.

0
FLOWBEN
FLOWBEN

Question 1 year ago

Loved this! Thankyou.
Quick question. Can i measure the volume of an irregular shaped tank?
Thanks Ben.

2
pagalprojects
pagalprojects

Question 1 year ago

What is the maximum depth that we can measure using this sensor ?

0
fazila4
fazila4

Question 2 years ago

Using Port : COM17

Using Programmer : arduino

Overriding Baud Rate : 115200

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x68

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x68

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x68

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x68

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x68

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x68

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x68

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x68

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x68

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x68

avrdude done.

I facing this kinda error. Help me. Thanks.

0
Johnny J
Johnny J

5 years ago

Excellent idea! I suspect the sensors could be covered with food-wrap film to splash proof them without interfering too much with their operation.
I'll be trying it!

0
vonPongrac
vonPongrac

Reply 5 years ago on Introduction

Hmm... Food foil could work but I am a bit skeptical! Ultrasonic (or ultrasound) wave are same as sound just frequency is to high for you to hear it. Ultrasonic waves are mechanical waves of presure. Because of that, foil can interupt with measurment! This is why radar method is better in industry because it use high frequency electromagnetic waves which can penetrate some materials and waterproofing a transceiver is not so hard.

0
Johnny J
Johnny J

Reply 5 years ago

I was thinking of the thin plastic film ("cling film" in the UK).
It's very thin and flexible, and I think it would vibrate with the ultrasound to transmit the vibrations if it was stretched over the sensors.
The calibration may need some adjustment, but I think it might work.
I'll try it and report back. :)

0
vonPongrac
vonPongrac

Reply 5 years ago on Introduction

Let me know about the results! I am very curious if this foil you are talking about will work! =)

0
Johnny J
Johnny J

Reply 5 years ago

It does. :)
I tried it.... You need stretch the film over each sensor individually to avoid vibration transfer between the two, but otherwise it seems fine.
I just stretched film across the front of the sensors, folded it back along the sensor bodies and held it in place with rubber bands.
I couldn't see any difference in readings between the sensor with or without the film.
I suspect it would work with thin polythene as well, but I haven't tried that yet.

0
SandeepT23
SandeepT23

Reply 2 years ago

Waw thats nice.. That was something I was also looking for. Since you already tried it, I have no worries. Out of a curiosity can you find any difference in result after wrapping it in film.

I made a similar one at my home water tank. but at summer when water getting vaporized and it getting on the sensor.