ESP32 ProjectsSeptember 2, 2026

How to Build an ESP32 Smart Distance Monitor Using HC-SR04 | Complete IoT Project Guide

Build a real-time ESP32 Smart Distance Monitor with the HC-SR04 ultrasonic sensor. Includes wiring, source code, web dashboard, and complete project guide.

Smart Distance Meter

Introduction

The ESP32 Smart Distance Monitor is a simple yet powerful IoT project that measures the distance of nearby objects using the HC-SR04 Ultrasonic Sensor and displays the results on a real-time web dashboard. Unlike traditional projects that require external software, this project hosts the dashboard directly on the ESP32, allowing you to monitor distance from any smartphone, tablet, or computer connected to the same Wi-Fi network.

Whether you're a beginner learning embedded systems or an IoT enthusiast exploring web-based monitoring, this project is a great way to understand sensor interfacing, Wi-Fi communication, and real-time data visualization.

What You Will Build

In this project, you'll build a wireless distance monitoring system that:

  • Measures object distance using the HC-SR04 ultrasonic sensor.
  • Displays live distance readings on a web browser.
  • Shows the object status as SAFE, NEAR, or TOO CLOSE.
  • Includes a dynamic distance gauge for better visualization.
  • Hosts the complete dashboard directly on the ESP32 without any third-party platform.

Component

Quantity

ESP32 Development Board

1

HC-SR04 Ultrasonic Sensor

1

Breadboard

1

Jumper Wires

As required



Circuit Connections

Connect the HC-SR04 ultrasonic sensor to the ESP32 as follows:

HC-SR04 Pin

ESP32 Pin

VCC

5V (VIN)

GND

GND

TRIG

GPIO (your pin)

ECHO

GPIO (your pin)

Note: Since the HC-SR04 ECHO pin outputs 5V, use a suitable voltage divider or level shifter before connecting it to the ESP32 input pin.

Working Principle

The HC-SR04 ultrasonic sensor measures distance by transmitting an ultrasonic pulse and listening for the reflected echo. The ESP32 calculates the time taken for the sound wave to return and converts it into distance in centimeters.

The measured distance is then updated instantly on a web dashboard hosted by the ESP32. Based on the measured value, the system displays one of three status levels:

  • ๐Ÿ”ด TOO CLOSE โ€“ Object is within 10 cm.
  • ๐ŸŸ  NEAR โ€“ Object is between 10 cm and 20 cm.
  • ๐ŸŸข SAFE โ€“ Object is beyond 20 cm.

The dashboard also includes a live gauge that provides a quick visual representation of the measured distance.

Source Code

Download the complete ESP32 Smart Distance Monitor source code from the link below.

https://drive.google.com/drive/folders/1DygZJ646RKROJKnIKJI8WBR_9qSI6jBp?dmr=1&ec=wgc-drive-%5Bmodule%5D-goto