Yarana Iot Guru
Published © MIT

IoT Based Smart Water Level Monitoring System using ESP32

Smart IoT Water Level System using ESP32, Ultrasonic Sensor & Blynk. Real-time tank monitoring, alerts & motor control by Yarana IoT Guru.

BeginnerFull instructions provided8 hours185
IoT Based Smart Water Level Monitoring System using ESP32

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

✅ 📌 Complete Code (with Comments) for ESP32 + Ultrasonic Sensor + Blynk

C/C++
/************* Smart Water Level Monitoring System *************
   Hardware: ESP32, HC-SR04 Ultrasonic Sensor, Blynk App
   Features:
   ✔ Measure water level in tank
   ✔ Send percentage to Blynk App
   ✔ Show water level on Serial Monitor
   ✔ Automatically detect tank full or empty
   ✔ Motor control can be added easily
****************************************************************/

#define BLYNK_TEMPLATE_ID "Your_Template_ID"
#define BLYNK_TEMPLATE_NAME "Water Level Monitor"
#define BLYNK_AUTH_TOKEN "Your_Blynk_Auth_Token"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Wi-Fi Credentials
char ssid[] = "Your_WiFi_Name";
char pass[] = "Your_WiFi_Password";

// Ultrasonic Sensor Pins
#define TRIG_PIN  5
#define ECHO_PIN  18

// Tank Dimensions (in cm)
const int TANK_DEPTH = 100; // Tank height in centimeters

long duration;
float distance, waterLevel, waterPercent;

BlynkTimer timer;

/***************** Function to Measure Water Level *****************/
void measureWaterLevel() {
  // Send ultrasonic pulse
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Read echo time
  duration = pulseIn(ECHO_PIN, HIGH);

  // Calculate Distance (cm)
  distance = duration * 0.034 / 2;

  // Calculate water level
  waterLevel = TANK_DEPTH - distance;

  // Calculate percentage
  waterPercent = (waterLevel / TANK_DEPTH) * 100.0;

  if (waterPercent < 0) waterPercent = 0;
  if (waterPercent > 100) waterPercent = 100;

  // Send data to Blynk
  Blynk.virtualWrite(V0, waterPercent);  // Water Level in %
  Blynk.virtualWrite(V1, waterLevel);    // Water Level in cm

  // Print on Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print(" cm | Water Level: ");
  Serial.print(waterLevel);
  Serial.print(" cm | Tank Filled: ");
  Serial.print(waterPercent);
  Serial.println(" %");
}

/***************** Setup Function *****************/
void setup() {
  Serial.begin(115200);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  // Connect to Blynk
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  // Timer runs every 1 second
  timer.setInterval(1000L, measureWaterLevel);
}

/***************** Main Loop *****************/
void loop() {
  Blynk.run();
  timer.run();
}

Credits

Yarana Iot Guru
43 projects • 12 followers
Yarana Iot Guru Yarana IoT Guru: Arduino, ESP32, GSM, NodeMCU & more. Projects, Tutorials & App Development. Innovate with us!
Thanks to Yarana IoT Guru.

Comments