LED PROJECTSULTRASONIC SENSOR

Ultrasonic Water Level Detector with ESP32 & LED Indicators

Running out of water in your tank without knowing? Or worried about overflow? In this project, we build a simple yet effective Water Level Detector using an ESP32 and an HC-SR04 ultrasonic sensor. The system measures the distance to the water surface and lights up different coloured LEDs to indicate the water level — Green for low, Yellow for mid-low, Orange for mid-high, and Red for high!

No complex coding, no expensive parts — just a fun and practical project you can build in under an hour!

What You Will Learn :

  • How to use the HC-SR04 ultrasonic sensor with ESP32
  • How to calculate distance from ultrasonic pulse duration
  • How to indicate water levels using 4 coloured LEDs

Components Required :

ComponentQuantity
ESP32 Development Board1
HC-SR04 Ultrasonic Sensor1
Red LED1
Orange LED1
Yellow LED1
Green LED1
Resistors (220Ω)4
Jumper WiresSeveral
USB Cable1
Computer with Arduino IDE1

Circuit Diagram :

Wiring Table :

HC-SR04 Ultrasonic Sensor → ESP32

HC-SR04 PinESP32 PinWire Color
VCCVCC (5V)Red
GNDGNDBlack
TRIGPin 26Yellow
ECHOPin 25Blue

LEDs → ESP32

LED ColorESP32 PinResistor
RedPin 21220Ω
OrangePin 19220Ω
YellowPin 18220Ω
GreenPin 17220Ω

Important Note: All LED resistors are connected to VCC, making them Active LOW — they turn ON when the pin is LOW and turn OFF when the pin is HIGH. All LED resistors are connected to VCC, making them Active LOW — they turn ON when the pin is LOW and turn OFF when the pin is HIGH. All LED resistors are connected to VCC, making them Active LOW — they turn ON when the pin is LOW and turn OFF when the pin is HIGH.

How It Works :

The HC-SR04 ultrasonic sensor sends out a sound pulse and measures how long it takes to bounce back from the water surface. This time is converted into distance in centimeters. Based on the distance, the ESP32 lights up the corresponding LED:

Distance (cm)Water LevelLED
0 – 100 cmLowest🟢 Green ON
101 – 200 cmLow-Mid🟡 Yellow ON
201 – 300 cmHigh-Mid🟠 Orange ON
301 cm and aboveHighest🔴 Red ON

Simulation Video :

Watch the water level detector in action — see how each LED lights up as the distance changes!

Arduino Code :

#define PIN_TRIG 26
#define PIN_ECHO 25
#define GREENLED 17
#define YELLOWLED 18
#define ORANGELED 19
#define REDLED 21
unsigned int level = 0;
void setup() {
pinMode(GREENLED, OUTPUT);
pinMode(YELLOWLED, OUTPUT);
pinMode(ORANGELED, OUTPUT);
pinMode(REDLED, OUTPUT);
digitalWrite(GREENLED, HIGH);
digitalWrite(YELLOWLED, HIGH);
digitalWrite(ORANGELED, HIGH);
digitalWrite(REDLED, HIGH);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration = pulseIn(PIN_ECHO, HIGH);
level = duration / 58;
digitalWrite(GREENLED, HIGH);
digitalWrite(YELLOWLED, HIGH);
digitalWrite(ORANGELED, HIGH);
digitalWrite(REDLED, HIGH);
if (level <= 100) { digitalWrite(GREENLED, LOW); } else if (level > 100 && level <= 200) { digitalWrite(YELLOWLED, LOW); } else if (level > 200 && level <= 300) { digitalWrite(ORANGELED, LOW); } else if (level > 300)
{
digitalWrite(REDLED, LOW);
}
delay(1000);
}

Customisation Tips :

  • Adjust distance ranges: Change the values 100, 200, 300 to match your actual tank size
  • Add a buzzer: Connect a buzzer to a free pin and trigger it when Red LED is ON
  • Add a Relay: Connect a relay to automatically turn ON a water pump when needed
  • Add WiFi alerts: Use ESP32’s built-in WiFi to send notifications to your phone
  • Add an OLED display: Show the exact distance reading on a small screen

This is a very practical project that can be used in real life for home water tanks, aquariums, or any container monitoring.

The best part about using the ESP32 is that you can easily expand this project by adding WiFi alerts, an OLED display, or even a relay to automate a water pump. The possibilities are endless!

Try experimenting with the distance thresholds to match your specific tank size, and share your build with us at Tinkercircuits.com!

Happy Tinkering! — The Tinker Circuits Team

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button