Introduction: Ultrasonic-Based Distance Measurement Device With LED Indicator (Digital Circuit)

About: Hey there, I’m Mayur. I’m a CSE student, and I like DIY a lot. I also enjoy cooking a bit. You can connect with me on LinkedIn and GitHub. I also have a YouTube channel where I teach.

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:

  1. Arduino Nano (you can use any Arduino board)
  2. HC-SR04 Ultrasonic Sensor
  3. 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:

  1. Green LED → D12
  2. Blue LED → D11
  3. Yellow LED → D10
  4. 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:

  1. Echo → D2
  2. Trig → D3
  3. VCC → 5V
  4. GND → GND

Step 3: Programming the Arduino

1. Declaring pin numbers

const int GREEN = 12;
const int BLUE = 11;
const int YELLOW = 10;
const int RED = 9;

const int TRIG = 3;
const int ECHO = 2;

You assign meaningful names to the pin numbers.

This makes the code easier to read.

  1. LEDs use pins 9, 10, 11, 12
  2. Ultrasonic sensor uses Trig → 3, Echo → 2

2. Declaring variables

long duration;
float distance;
  1. duration will store how long it takes for the sound wave to return.
  2. distance will store the calculated distance in centimeters.

3. Setting up the pins

void setup()
{
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);

Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}

In setup():

  1. LEDs are set as OUTPUT.
  2. Serial communication is started at 9600 baud for printing values.
  3. Trig is set as output (Arduino sends a sound pulse).
  4. Echo is input (Arduino receives the reflection).

4. Starting the loop

void loop()
{

This code runs again and again.

5. Sending a pulse from the ultrasonic sensor

digitalWrite(TRIG, LOW);
delayMicroseconds(2);

digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);

This sends a short 10-microsecond pulse.

The HC-SR04 uses this to start measuring distance.

6. Reading the echo time

duration = pulseIn(ECHO, HIGH);
  1. pulseIn() waits for the echo signal.
  2. It measures how long the pin stays HIGH.
  3. This time represents how long the sound wave traveled to the object and back.

7. Converting time to distance

distance = (duration * 0.0343) / 2;
  1. Sound travels at 0.0343 cm per microsecond.
  2. Divide by 2 because the sound travels to the object and back.

8. Turning on LEDs based on distance

if (distance <= 99)
{
digitalWrite(GREEN, HIGH);
}
else if (distance >= 100 && distance <= 199)
{
digitalWrite(BLUE, HIGH);
}
else if (distance >= 200 && distance <= 299)
{
digitalWrite(YELLOW, HIGH);
}
else
{
digitalWrite(RED, HIGH);
}
delay(500);

LED logic:

  1. 0–99 cm → Green
  2. 100–199 cm → Blue
  3. 200–299 cm → Yellow
  4. 300+ cm → Red

9. Printing the distance to the Serial Monitor

Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

This allows you to see the distance value on your computer.

10. Turning all LEDs OFF

digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);

After showing the LED for half a second, all LEDs are turned off.

11. short delay

delay(500);

Wait 0.5 seconds before taking another distance reading.

In short:

  1. Arduino sends an ultrasonic pulse.
  2. Sensor measures time taken for sound to return.
  3. Arduino calculates distance.
  4. Based on distance range, a specific LED lights up.
  5. Distance is printed on the Serial Monitor.
  6. Repeat continuously.

Step 4: Full Code

const int GREEN = 12;
const int BLUE = 11;
const int YELLOW = 10;
const int RED = 9;

const int TRIG = 3;
const int ECHO = 2;

long duration;
float distance;

void setup()
{
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);

Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}

void loop()
{

digitalWrite(TRIG, LOW);
delayMicroseconds(2);

digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);

duration = pulseIn(ECHO, HIGH);

distance = (duration * 0.0343) / 2;

if (distance <= 99)
{
digitalWrite(GREEN, HIGH);
delay(500);
}
else if (distance >= 100 && distance <= 199)
{
digitalWrite(BLUE, HIGH);
delay(500);
}
else if (distance >= 200 && distance <= 299)
{
digitalWrite(YELLOW, HIGH);
delay(500);
}
else
{
digitalWrite(RED, HIGH);
delay(500);
}

Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);

delay(500);
}

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!