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

💧 Project Overview

In this lesson, we will connect an Ultrasonic Sensor with ESP32 to measure water level in a tank and control a water pump using a relay.

This system will:

  • Measure water level using ultrasonic distance

  • Detect LOW and FULL tank condition

  • Control motor automatically

  • Prevent overflow

  • Prevent dry running


📦 Components Used in This Project

🔹 1️⃣ ESP32
  • Main microcontroller

  • Reads ultrasonic data

  • Controls relay

  • Handles automation logic


🔹 2️⃣ HC-SR04 Ultrasonic Sensor

  • Measures distance to water surface

  • Calculates water level

  • Non-contact measurement


🔹 3️⃣ Single Channel Relay Module

  • Controls high voltage motor

  • Electrically isolates ESP32


🔹 4️⃣ DC Water Pump

  • Pumps water into tank

  • Controlled via relay


🔹 5️⃣ 12V Power Supply

  • Powers water pump

  • External supply required


🔌 Circuit Connections


🔹 Ultrasonic Sensor → ESP32

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

⚠ IMPORTANT:
Echo pin gives 5V output.
Use a voltage divider (10k + 20k resistor) to reduce to 3.3V for ESP32 safety.


🔹 Relay Module → ESP32

Relay Pin ESP32
VCC 5V
GND GND
IN GPIO 26

⚠ Relay Active LOW
LOW → Motor ON
HIGH → Motor OFF


🔹 Relay → DC Pump Wiring

Relay Terminal Connection
COM 12V Supply +
NO Pump +
Pump – 12V Supply –

When relay activates:
COM connects to NO → Pump turns ON


📐 How Ultrasonic Sensor Measures Water Level

1️⃣ Sensor sends ultrasonic pulse
2️⃣ Pulse hits water surface
3️⃣ Echo returns
4️⃣ ESP32 calculates distance

Formula used:

 
 
Distance = Duration × 0.034 / 2
 

If tank height = 30 cm:

Water Level = Tank Height – Measured Distance

Example:

Distance Tank Condition
25 cm Tank Low
5 cm Tank Full

⚡ Power Distribution

  • ESP32 powered by USB

  • Ultrasonic powered by 5V

  • Relay powered by 5V

  • Pump powered by 12V external supply

⚠ Always connect all GND together (Common Ground)


🚨 Important Safety Notes

✔ Use voltage divider for Echo pin
✔ Never connect pump directly to ESP32
✔ Use proper insulated wiring
✔ Keep ultrasonic sensor dry
✔ Mount sensor firmly at tank top


🎯 What Students Learn in This Lesson

  • Ultrasonic sensor interfacing

  • Distance measurement concept

  • Relay module wiring

  • Motor control circuit

  • Power isolation

  • Industrial-style wiring


📌 Lesson Summary

In this lesson, we successfully:

  • Connected HC-SR04 ultrasonic sensor with ESP32

  • Connected relay module safely

  • Designed pump control circuit

  • Built a complete hardware foundation for Smart Water Management System

This makes the system:

✔ More accurate
✔ Non-contact measurement
✔ Industry-ready
✔ Long-lasting solution

Scroll to Top