Introduction: Measuring with Sound Waves

Distance measurement is a core requirement in robotics, automation, and object detection systems. One of the simplest and most effective methods to measure distance is through ultrasonic sensing. In this article, you’ll learn how the HC-SR04 ultrasonic sensor works and how to interface it with an Arduino Uno to measure distances in real time.

What is an Ultrasonic Sensor?

The HC-SR04 ultrasonic sensor works on the principle of echo ranging β€” the same way bats navigate. It sends out a burst of ultrasonic sound waves (typically at 40 kHz), waits for them to bounce off a nearby object, and measures the time taken for the echo to return. Using the speed of sound, the sensor calculates the distance between itself and the object.

Working Principle

  • The sensor emits an ultrasonic pulse when the TRIG pin is triggered.
  • The sound wave travels through the air, reflects off an object, and returns.
  • The sensor’s ECHO pin outputs a high pulse corresponding to the time taken for the echo to return.
  • Arduino measures this duration and calculates distance using the formula:
Distance (cm) = (Time * Speed of Sound) / 2
Speed of Sound = 343 m/s (at 25Β°C)
    

The division by 2 accounts for the round trip (from sensor to object and back).

Pin Configuration of HC-SR04

  • VCC: +5V supply
  • GND: Ground connection
  • TRIG: Trigger input pin
  • ECHO: Output pin that sends back the echo time pulse

Connecting HC-SR04 to Arduino

HC-SR04 VCC β†’ 5V  
HC-SR04 GND β†’ GND  
HC-SR04 TRIG β†’ Digital Pin 9  
HC-SR04 ECHO β†’ Digital Pin 10
    

Complete Arduino Code

// SP Tech Solutions - Ultrasonic Distance Measurement

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

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

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distanceCm = duration * 0.0343 / 2;

  Serial.print("Distance: ");
  Serial.print(distanceCm);
  Serial.println(" cm");
  delay(500);
}

Understanding the Code

  • pulseIn() function measures the duration of the echo pulse in microseconds.
  • Multiplying the duration by 0.0343 converts time into distance using the speed of sound (in cm/Β΅s).
  • Dividing by 2 adjusts for the signal’s round trip.
  • The result is printed every 500 milliseconds on the serial monitor.

Improving Accuracy

  • Ensure the sensor is stable and perpendicular to the target surface.
  • Avoid soft or absorbent materials β€” they reflect sound poorly.
  • Average multiple readings to minimize noise.
  • Keep the operating range between 2 cm and 400 cm for best results.

Common Applications

  • Obstacle detection in robots
  • Automatic door opening systems
  • Water level measurement
  • Parking assistance systems
  • Object counting or sorting

How to Expand This Project

  • Add an LCD Display: Show the distance directly without needing the serial monitor.
  • Integrate with Buzzer: Trigger alerts when an object is too close.
  • Data Logging: Record distance values over time for automation applications.
  • Wireless Communication: Send data via Bluetooth or Wi-Fi for IoT use cases.

Key Learning Points

  • Ultrasonic sensors use sound waves to measure distance precisely.
  • Timing and speed-of-sound calculations are crucial for accuracy.
  • Simple sensors like HC-SR04 can form the foundation for complex automation systems.

Conclusion

The Ultrasonic Distance Measurement project is an excellent starting point for understanding how sensors interact with microcontrollers. Once you grasp the basic timing and calculation principles, you can expand this simple setup into autonomous robots, IoT monitoring devices, or smart automation systems.

SP Tech Solutions β€” Turning Fundamentals into Future Innovation βš™οΈ