Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

Perfect Shiv 👑🔥
Now here is your final ready-to-upload lesson content for:


📘 Lesson 7.7 – Smart Water Management System Code

(Blynk IoT Version – Ultrasonic Sensor)


💧 Smart Water Management System

(ESP32 + Ultrasonic + Relay + Blynk IoT Cloud)

In this lesson, we upgrade the system to Cloud-Based IoT Control using Blynk.

This version supports:

  • 📏 Ultrasonic water level measurement

  • 📊 Tank percentage calculation

  • 🔄 AUTO Mode

  • 🎮 MANUAL Mode

  • 📱 Motor ON/OFF from anywhere

  • 🚨 Tank FULL alert indicator

  • ☁ Cloud-based monitoring

Internet required ✔
Blynk IoT Cloud ✔


📦 Components Used

  • ESP32

  • HC-SR04 Ultrasonic Sensor

  • Single Channel Relay

  • DC Water Pump

  • WiFi Connection

  • Blynk IoT Account


🔌 Pin Configuration

🔹 Ultrasonic Sensor

Ultrasonic Pin ESP32
VCC 5V
GND GND
Trig GPIO 5
Echo GPIO 18

⚠ Use voltage divider for Echo (5V → 3.3V)


🔹 Relay Module

Relay Pin ESP32
VCC 5V
GND GND
IN GPIO 26

Relay Active LOW
LOW → Motor ON
HIGH → Motor OFF


🌐 Blynk IoT Setup (Step-by-Step)

Go to:
👉 https://blynk.cloud

Login and follow these steps:


🔹 Step 1 – Create Template

  1. Click New Template

  2. Template Name:

     
     
    Ultrasonic Water System
     
  3. Hardware: ESP32

  4. Connection Type: WiFi

  5. Click Done


🔹 Step 2 – Create Datastreams

Go to Datastreams → New Datastream


1️⃣ V0 – Auto/Manual Mode Switch

  • Type: Virtual Pin

  • Pin: V0

  • Data Type: Integer

  • Min: 0

  • Max: 1

  • Default: 1

👉 1 = AUTO
👉 0 = MANUAL


2️⃣ V1 – Motor Control Button

  • Type: Virtual Pin

  • Pin: V1

  • Data Type: Integer

  • Min: 0

  • Max: 1

👉 1 = Motor ON
👉 0 = Motor OFF


3️⃣ V2 – Tank Percentage Display

  • Type: Virtual Pin

  • Pin: V2

  • Data Type: Integer

Displays water percentage.


4️⃣ V3 – Tank FULL LED Indicator

  • Type: Virtual Pin

  • Pin: V3

  • Data Type: Integer

👉 1 = Tank Full
👉 0 = Normal


🔹 Step 3 – Create Device

  1. Go to Devices

  2. Click New Device

  3. From Template

  4. Select Ultrasonic Water System

  5. Copy Auth Token

You will paste this into code.


📱 Dashboard Setup (Mobile App)

Open Blynk App and add widgets:


🔘 Switch – Auto/Manual

Datastream: V0


🔘 Button – Motor Control

Datastream: V1


📊 Value Display – Tank %

Datastream: V2


💡 LED Widget – Tank Full

Datastream: V3
ON Color: Red
OFF Color: Green


💻 COMPLETE CODE

(Ultrasonic + Auto/Manual + Blynk IoT)

Replace credentials before uploading.

#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_DEVICE_NAME "Ultrasonic Water System"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"

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

#define TRIG_PIN 5
#define ECHO_PIN 18
#define RELAY_PIN 26

char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";

float tankHeight = 30.0;
long duration;
float distance;
float waterLevel;
float percentage;

bool autoMode = true;
bool motorState = false;

BLYNK_WRITE(V0) {
autoMode = param.asInt();
}

BLYNK_WRITE(V1) {
if (!autoMode) {
int value = param.asInt();
if (value == 1) turnMotorOn();
else turnMotorOff();
}
}

void setup() {
Serial.begin(115200);

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

digitalWrite(RELAY_PIN, HIGH);

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}

void loop() {
Blynk.run();

// Ultrasonic measurement
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;

waterLevel = tankHeight - distance;

if (waterLevel < 0) waterLevel = 0;
if (waterLevel > tankHeight) waterLevel = tankHeight;

percentage = (waterLevel / tankHeight) * 100;

Blynk.virtualWrite(V2, percentage);

if (autoMode) {

if (percentage < 20 && !motorState) {
turnMotorOn();
}

if (percentage > 90 && motorState) {
turnMotorOff();
Blynk.virtualWrite(V3, 1);
} else {
Blynk.virtualWrite(V3, 0);
}
}
}

void turnMotorOn() {
digitalWrite(RELAY_PIN, LOW);
motorState = true;
Blynk.virtualWrite(V1, 1);
}

void turnMotorOff() {
digitalWrite(RELAY_PIN, HIGH);
motorState = false;
Blynk.virtualWrite(V1, 0);
}

🧠 Code Explanation


1️⃣ BLYNK_WRITE(V0)

Controls Auto/Manual mode from dashboard.

If:

  • 1 → Auto Mode

  • 0 → Manual Mode


2️⃣ BLYNK_WRITE(V1)

Controls motor manually only when Auto mode is OFF.


3️⃣ Ultrasonic Measurement

 
 
distance = duration × 0.034 / 2
 

Converts echo time into distance.


4️⃣ Tank Percentage Calculation

 
 
percentage = (waterLevel / tankHeight) × 100
 

Calculates water percentage.


5️⃣ Dashboard Updates

 
 
Blynk.virtualWrite(V2, percentage);
 

Updates tank percentage display.

 
 
Blynk.virtualWrite(V3, 1);
 

Turns ON tank FULL LED.


6️⃣ Auto Mode Logic

If tank < 20%
→ Motor ON

If tank > 90%
→ Motor OFF + LED ON


📟 System Working Flow

  1. ESP32 connects to WiFi

  2. Connects to Blynk Cloud

  3. Reads ultrasonic sensor

  4. Calculates tank percentage

  5. Checks mode

  6. Controls motor

  7. Updates dashboard


🎯 What Students Learn

  • Cloud-based IoT architecture

  • Virtual pin concept

  • Ultrasonic measurement logic

  • Remote automation system

  • Auto + Manual design

  • Real-world IoT implementation




Scroll to Top