How to Build an Ultrasonic Sensor Based Distance Meter with Arduino

How to Build an Ultrasonic Sensor Based Distance Meter with Arduino
0
0
4 min read

Learn how to build an Ultrasonic Distance Meter using Arduino and HC-SR04 sensor. Includes circuit diagram, source code, and practical use cases. Perfect for beginners and DIY electronics enthusiasts.


Introduction

📌 What is HC-SR04 Ultrasonic Sensor?

The HC-SR04 is a popular, low-cost ultrasonic sensor module used to measure distances with high accuracy. It works on the principle of echolocation—similar to how bats and dolphins perceive their environment. The sensor emits an ultrasonic wave at 40 kHz, which travels through the air, reflects off an object, and returns to the sensor. By calculating the time it takes for the echo to return, the sensor can determine the distance of the object.

This module is widely used in Arduino and micro controller projects for:

  • Obstacle detection
  • Distance measurement
  • Robotic navigation
  • Automatic parking systems

It features four pins:

  • VCC – Power Supply (5V)
  • Trig – Trigger Input
  • Echo – Echo Output
  • GND – Ground

Would you like a diagram showing the working principle of HC-SR04 as well?

What the Project Does

This project demonstrates how to measure distance using the HC-SR04 ultrasonic sensor and Arduino. The sensor calculates distance by sending out an ultrasonic pulse. It measures the time it takes for the echo to return.

Real-World Use Cases

  • Automatic parking assist systems
  • Object detection in robotics
  • Smart measurement tools
  • Obstacle avoidance in drones and autonomous vehicles

Skill Level

Beginner to Intermediate

Expected Outcome

By the end of this project, you will:

  • Understand how ultrasonic distance measurement works
  • Learn to interface digital sensors with Arduino
  • Develop an LCD-based display system (optional)
  • Be able to build your own distance measuring device

Components Required

QuantityPartDescriptionBuy Link
1Arduino UNOMain micro controller boardBuy on Elecsynergy
1HC-SR04 Ultrasonic SensorMeasures distance using ultrasonic wavesBuy on Elecsynergy
116×2 LCD Display (Optional)To display distance values (requires I2C adapter ideally)Buy on Elecsynergy
1BreadboardFor circuit connectionsBuy on Elecsynergy
10Jumper WiresMale-to-Male wires for connecting componentsBuy on Elecsynergy
1USB Cable for ArduinoFor uploading code and powering ArduinoBuy on Elecsynergy

Circuit Diagram + Explanation

HC-SR04 Pin Connections:

  • VCC → 5V on Arduino
  • GND → GND on Arduino
  • Trig → Digital Pin 9
  • Echo → Digital Pin 10

LCD Display (Optional):

If using a 16×2 LCD with I2C:

  • VCC → 5V
  • GND → GND
  • SDA → A4
  • SCL → A5

Visual Schematic:


Arduino Code – Commented

// Ultrasonic Distance Meter using HC-SR04 and Arduino

const int trigPin = 9;
const int echoPin = 10;
long duration;
float distanceCm;

void setup() {
  Serial.begin(9600); // Start serial monitor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Clear the trigger pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Send a 10-microsecond pulse
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure the echo time
  duration = pulseIn(echoPin, HIGH);

  // Calculate distance (cm)
  distanceCm = duration * 0.034 / 2;

  // Display on Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distanceCm);
  Serial.println(" cm");

  delay(1000); // Delay 1 second between readings
}

Working Explanation

The HC-SR04 sensor sends out an ultrasonic pulse when triggered. When the pulse hits an object, it reflects back, and the sensor detects the echo. The Arduino calculates the time taken for this round trip and uses the speed of sound (343 m/s) to calculate the distance:

Distance = (Time x Speed of Sound) / 2

The division by 2 accounts for the pulse traveling to the object and back.

Optional: With a 16×2 LCD connected, you can display the distance without needing the serial monitor.


Demo / Output Preview

You’ll see:

  • Distance printed every second on the Serial Monitor
  • (Optional) Distance displayed on an LCD

Use your hand or any object to see the values change in real-time.


Troubleshooting & Tips

  • Always 0 or very high reading?
    • Check trigger and echo pin wiring
    • Ensure object is within 2cm–400cm
  • Inconsistent results?
    • Reduce noise by shielding sensor from cross-echoes
    • Avoid reflective surfaces
  • Nothing on serial monitor?
    • Make sure baud rate is 9600
    • Check USB cable connection

Project Expansion Ideas

1. LCD Distance Display

Add an LCD (16×2 or OLED) to display the result directly on the device.

2. Buzzer Alert System

Trigger a buzzer if the distance goes below a certain limit – great for proximity alerts.

3. Servo Integration

Attach a servo motor and scan for distances across multiple angles (like radar).

4. Wireless Transmission

Use Bluetooth or Wi-Fi (ESP32/ESP8266) to send distance data to a phone app or web dashboard.

5. Vehicle Reverse Parking Aid

Use multiple sensors at different angles and place it in a car prototype.


Conclusion

You’ve successfully built an Ultrasonic Distance Meter using Arduino and HC-SR04. It’s an excellent beginner-friendly project to introduce you to the world of sensors, timing, and environmental interaction.

This foundational project opens doors to smart automation, obstacle detection in robotics, and real-time sensor integration. Don’t stop here—keep expanding and combining this with other Arduino modules to build smarter devices!

✅ Try This: Add multiple sensors and rotate them using a servo to build a full 180-degree radar system!


🛒 Buy the Complete Kit

Order from Elecsynergy


#UltrasonicDistanceSensor #HC-SR04Arduino #ArduinoDistanceMeter #DIYUltrasonicProject #ArduinoUNO #Elecsynergy #InnovateYourself #ObjectDetectionArduino #DistanceMeasuringDevice #ArduinoSensorTutorial

Leave a Reply