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