In this tutorial, you’ll get to know about liquids level detection with Arduino. First, you’ll see some information about the water level sensor, and then you’ll learn how to use this module with some practical examples.
What You Will Learn- What water level detecting is
- How water level sensor works
- How to use a water level sensor with Arduino
Liquid level detection or in other words, measuring the level of liquid in deep tanks or water in reservoirs is done in order to control the depth of water and prevent it from the overflow in industry. In household applications also it can be used for example to detect the water level inside aquariums. The purpose of the level measurement is to determine the level of liquid at any moment and do the required operation accordingly.
There are different ways to measure the liquid level. Some sensors calculate the depth of liquid according to the pressure caused by the liquid’s specific gravity and the vertical distance to the surface. Some others emit ultrasonic waves from a transducer which also detects and measures the reflected waves to calculate the liquid depth.
The sensor that we use in this tutorial is Arduino compatible and works on the basis of resistance measurements, you’ll soon find out how it works.
This module, which is one of the most widely used modules for detecting the liquid level, works on the basis of resistance change. On this module, there are parallel lines of conductivity that are connected to the Ground and are in fact the path of electric current. Water is a good conductor so when these lines are in the water, they will be short circuit, and the resistance of the module decreases.
By fixing the module on the liquid container, the variable resistance sets on a specific value based on the water level. The module measures this analog resistor and sends it to Arduino. Arduino uses this value directly or by converting this value to a digital amount.
This water level sensor has 3 Pins. 2 of them are for power (+), connecting to the +5V, and ground (-), connecting to the ground terminal of the Arduino. The other pin (S), is the analog output pin.
You don’t need any specific library to use this sensor. Just read the analog value of the output pin and calculate the liquid level accordingly.
CircuitUpload the following code on your Arduino board and open the serial monitor window. Place the sensor in water and you can see the results on your serial monitor window.
/* Water level sensor
* by Hanie Kiani
* https://electropeak.com/learn/
*/
const int analogInPin = A0;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
Serial.print("Sensor = " );
Serial.print(sensorValue*100/1024);
Serial.println("%");
delay(1000);
}
You can also use the water level sensor to detect rain with a buzzer. To detect whether it is raining, position the sensor horizontally so that raindrops can fall on the sensor and increase the value of the pin S.
When the sensor starts getting wet, the buzzer will start beeping every few seconds. And when the module becomes completely wet, the buzzer warns with louder sound and will continue beeping nonstop.
Circuit/*
* Rain Detector with Water level sensor
* by Hanie kiani
* https://electropeak.com/learn/
*/
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1024; // sensor maximum
const int buzzer = 9;
void setup() {
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
}
void loop() {
int sensorReading = analogRead(A0);
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// range value:
switch (range) {
case 0: // Sensor is wet
Serial.println("ٌWet!");
tone(buzzer, 5000);
break;
case 1: // Sensor getting wet
Serial.println(" Warning");
tone(buzzer, 1000 , 5);
break;
case 2: // Sensor dry
Serial.println("Dry");
noTone(buzzer);
break;
}
delay(10); // delay between reads
}
The map()function divides the sensor range 0 to 1024 into 3 sections.
tone(buzzer, 5000);
The tone() function sends a PWM signal on the buzzer pin so the buzzer will make a sound.
The first argument specifies the output pin, and the second one determines the PWM frequency. It can also have a third argument standing for the signal duration.
What’s Next?- Try to use the SMS module (like the Sim800 module) with your rain detector, so that it can inform you the raining by sending a message on your phone.
If you find this tutorial helpful and interesting please like us on facebook.
Comments