Liquid Overflow Detection With Ultrasonic Sensor

 

Water wastage due to tank overflow is a common issue in many households, especially in developing countries. This problem not only leads to significant water loss but also results in unnecessary expenditure on water bills. To address this issue, we can use an ultrasonic sensor to detect and prevent the overflow of liquid in storage tanks. This blog post discusses a practical project that leverages the HC-SR04 ultrasonic sensor to monitor and control the water level in a storage tank, ensuring efficient water usage.

Project Motivation

The primary goal of this project is to use an ultrasonic sensor to detect the overflow of liquid in a storage tank and alert the user before an overflow occurs. Additionally, it allows for remote monitoring of the liquid level in the tank using the Adafruit feed and Adafruit Dashboard. This project is particularly useful for tanks where the liquid level fluctuates and is not constant. The same concept can be applied to monitor the level of various substances in different applications. 

liquid tank

Understanding Ultrasonic Sound Waves

Sound waves are vibrations that travel through a medium (solid, liquid, or gas). Based on their frequency, sound waves are categorized into three types:

- Infrasonic Sound: Frequencies less than 20 Hz.
- Audible Sound: Frequencies between 20 Hz and 20,000 Hz, which are audible to humans.
- Ultrasonic Sound: Frequencies greater than 20,000 Hz, which are inaudible to humans but can be detected by some animals and used in various applications.

Ultrasonic sound waves share physical properties with audible sound waves but are used in specialized fields like non-destructive testing, medical imaging, and electronic sensors. In this project, we use ultrasonic waves for liquid level detection, similar to sonar technology used in underwater range finding.

Components and Specifications

For this project, we use the HC-SR04 ultrasonic sensor, a cost-effective and low-power device commonly used in IoT projects.

Ultrasonic sensor

The sensor's specifications are as follows:

- Working Voltage:5V DC
- Working Current:15 mA
- Frequency:40 Hz
- Range:2 cm to 400 cm
- Measuring Angle:15 degrees
- Trigger Input Signal:10 µs TTL pulse
- Echo Output Signal:TTL level signal

The HC-SR04 consists of two ultrasonic transducers: one acts as a transmitter, sending out ultrasonic pulses, and the other acts as a receiver, detecting the reflected pulses. The sensor also has VCC and GND pins for power, and TRIG and ECHO pins for triggering the pulses and receiving the echo signal.
 

How It Works

When the NodeMCU sends a 10 µs HIGH pulse to the TRIG pin of the HC-SR04, the sensor emits an 8-cycle ultrasonic burst at 40 kHz. This burst travels through the air and reflects off any obstacles. If the pulse is reflected back, the ECHO pin goes HIGH, starting the echo-back signal. The duration of this pulse is proportional to the distance between the sensor and the obstacle. If no obstacle is detected within 38 ms, the ECHO pin goes LOW, indicating no obstacle within range.

Building the System

Hardware Required:
- NodeMCU: 1
- HC-SR04 Ultrasonic Sensor: 1
- Buzzer: 1
- Jumper Wires: As required

Connection Details:
- NodeMCU VU or Vin: HC-SR04 VCC
- NodeMCU D5: HC-SR04 TRIG
- NodeMCU D6: HC-SR04 ECHO
- NodeMCU D7: Buzzer +ve pin
- NodeMCU GND: HC-SR04 GND, Buzzer -ve pin

See the connection diagram below.

hardware connection for water level detection

Software Setup

 Adafruit Feed and Dashboard:

1. Create a New Feed:Sign in to your Adafruit IO account and create a new feed named "distance."

2. Create a New Dashboard:Go to the Dashboard section and create a new dashboard named "Storage Tank Dashboard."

 


3. Add GUI Blocks:Add a Gauge GUI block to visualize the distance feed data, setting the appropriate min and max values, and optionally configure warning levels.

Arduino IDE Code:

#include <ESP8266WiFi.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>

// WiFi and MQTT setup
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* mqtt_server = "io.adafruit.com";
const char* mqtt_user = "your_AIO_username";
const char* mqtt_key = "your_AIO_key";

// Pins
#define TRIG D5
#define ECHO D6
#define BUZZER D7

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, mqtt_server, 1883, mqtt_user, mqtt_key);
Adafruit_MQTT_Publish distanceFeed = Adafruit_MQTT_Publish(&mqtt, "your_AIO_username/feeds/distance");

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUZZER, OUTPUT);
  Serial.begin(115200);
 
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
 
  // Connect to Adafruit IO
  connectToMQTT();
}

void loop() {
  long duration, distance;
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
 
  duration = pulseIn(ECHO, HIGH);
  distance = (duration / 2) / 29.1; // Convert to cm
 
  Serial.print("Distance: ");
  Serial.println(distance);
 
  if (distance < 15) { // Threshold for overflow warning
    digitalWrite(BUZZER, HIGH);
  } else {
    digitalWrite(BUZZER, LOW);
  }
 
  // Publish to Adafruit IO
  if (!mqtt.ping()) {
    mqtt.disconnect();
    connectToMQTT();
  }
  distanceFeed.publish(distance);
  delay(2000); // Delay for 2 seconds
}

void connectToMQTT() {
  while (mqtt.connect() != 0) {
    delay(1000);
    Serial.println("Connecting to MQTT...");
  }
  Serial.println("Connected to MQTT");
}

Conclusion

By using the HC-SR04 ultrasonic sensor and integrating it with the Adafruit IO platform, you can efficiently monitor and control the water level in a storage tank. This project not only helps in preventing water wastage but also provides a scalable solution for various applications where level monitoring is crucial. Start building this project today and contribute to water conservation efforts!

The HC-SR04 ultrasonic sensor can also be used as obstacle detector for Arduino based car and as intruder detector in security system.

Post a Comment

Previous Post Next Post