Introduction: Ultrasonic-Based Distance Measurement Device With LED Indicator (Digital Circuit)
So in this project, we will be making an Ultrasonic-Based Distance Measurement Device with an LED Indicator. Since I currently don’t have the physical components, I am using a simulator. This is a very simple and fun project that works on basic Arduino functions and fundamentals.
Now you may be wondering where this device can be used. It can be used for water-level measurement, where the red LED may indicate that the water has reached above the safe level. It can also be used as a playful educational activity for kids to help them understand how sonar works, and in many other applications.
So without wasting more time, let’s make it!
Please note that this project is made in the Wokwi simulator, which can be used directly from your laptop or PC browser.
Supplies
Since this is a digital project, we don’t need any physical components. Using the Wokwi simulator and a decent PC, we can run everything directly in the browser.
To make this project, we will need the following components in Wokwi:
- Arduino Nano (you can use any Arduino board)
- HC-SR04 Ultrasonic Sensor
- LEDs (Red, Blue, Green, Yellow)
Step 1: Connecting LEDs With Arduino
For this step, we will need four LEDs: Green, Blue, Yellow, and Red. The short leg is the cathode (negative) and should be connected to GND. The long leg is the anode (positive) and will be connected to the Arduino digital pins.
Connections: Now connect the anodes (long legs) as follows:
- Green LED → D12
- Blue LED → D11
- Yellow LED → D10
- Red LED → D9
Step 2: Connecting Sensor With Arduino
Now add the HC-SR04 ultrasonic sensor to the circuit. Rotate it 180 degrees for a better wiring position.
Make the following connections:
- Echo → D2
- Trig → D3
- VCC → 5V
- GND → GND
Step 3: Programming the Arduino
1. Declaring pin numbers
You assign meaningful names to the pin numbers.
This makes the code easier to read.
- LEDs use pins 9, 10, 11, 12
- Ultrasonic sensor uses Trig → 3, Echo → 2
2. Declaring variables
- duration will store how long it takes for the sound wave to return.
- distance will store the calculated distance in centimeters.
3. Setting up the pins
In setup():
- LEDs are set as OUTPUT.
- Serial communication is started at 9600 baud for printing values.
- Trig is set as output (Arduino sends a sound pulse).
- Echo is input (Arduino receives the reflection).
4. Starting the loop
This code runs again and again.
5. Sending a pulse from the ultrasonic sensor
This sends a short 10-microsecond pulse.
The HC-SR04 uses this to start measuring distance.
6. Reading the echo time
- pulseIn() waits for the echo signal.
- It measures how long the pin stays HIGH.
- This time represents how long the sound wave traveled to the object and back.
7. Converting time to distance
- Sound travels at 0.0343 cm per microsecond.
- Divide by 2 because the sound travels to the object and back.
8. Turning on LEDs based on distance
LED logic:
- 0–99 cm → Green
- 100–199 cm → Blue
- 200–299 cm → Yellow
- 300+ cm → Red
9. Printing the distance to the Serial Monitor
This allows you to see the distance value on your computer.
10. Turning all LEDs OFF
After showing the LED for half a second, all LEDs are turned off.
11. short delay
Wait 0.5 seconds before taking another distance reading.
In short:
- Arduino sends an ultrasonic pulse.
- Sensor measures time taken for sound to return.
- Arduino calculates distance.
- Based on distance range, a specific LED lights up.
- Distance is printed on the Serial Monitor.
- Repeat continuously.
Step 4: Full Code
Step 5: Project Completed
Hurray! Our project is completed and working as expected.
Thank you for checking out this project. Let me know your thoughts in the comments down below, and feel free to check out my other projects too.
Have a nice day!




