Free Nationwide Doorstep Delivery on orders over ₹499
← All 10 Projects•Intermediate•45 mins

Ultrasonic Distance Measurement (RADAR/Sonar Principle)

Measure physical distance by emitting 40kHz ultrasonic sound bursts and timing the returning echo reflection.

Real-World Engineering Application

Car parking reverse sensors, autonomous drone altitude holding, industrial fluid tank level gauging, and robotics collision avoidance.

Physics & Working Principle

The transmitter emits an ultrasonic wave packet. When it reflects off an obstacle, the receiver captures the echo. Using the speed of sound in air (v ≈ 343 m/s or 0.0343 cm/µs), distance = (Time × Speed) / 2.

Key Learning Outcome:Master pulseIn() timing functions, high-frequency sound physics, and converting microseconds to centimeters.

Pin Connection & Wiring Table

Component Pin / TerminalMicrocontroller Pin (ESP32 / Arduino)Wiring Note
VCC5V / VINHC-SR04 requires 5V supply
TRIGGPIO 5 / Pin 11Trigger pulse output from MCU
ECHOGPIO 18 / Pin 12Echo pulse input (via voltage divider if using 3.3V ESP32)
GNDGNDCommon ground

Step-by-Step Plug & Play Assembly

1

Mount the HC-SR04 upright on the plug-and-play base with transducers facing forward.

2

Connect VCC to 5V and GND to GND.

3

Connect TRIG to GPIO 5.

4

Connect ECHO to GPIO 18 (with a 1k/2k resistor divider for 3.3V protection).

Complete Tested Source Code

// NovaCircuit Project 4: Ultrasonic Distance Gauge
const int TRIG_PIN = 5;
const int ECHO_PIN = 18;

void setup() {
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  Serial.println("HC-SR04 Ultrasonic Ranging Ready");
}

void loop() {
  // Clear trigger pin
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  
  // Send a 10 microsecond HIGH pulse
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  // Read duration of ECHO return pulse in microseconds
  long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout
  
  // Distance calculation: Distance = (Duration * Speed of Sound 0.0343 cm/us) / 2
  float distanceCm = (duration * 0.0343) / 2;
  
  if (duration == 0 || distanceCm > 400) {
    Serial.println("Out of Range / No Obstacle Detected");
  } else {
    Serial.print("Target Distance: ");
    Serial.print(distanceCm, 1);
    Serial.println(" cm");
  }
  
  delay(300);
}
Code Logic Breakdown:

pulseIn(ECHO_PIN, HIGH) measures how long the ECHO pin stays HIGH. The echo pulse duration is directly proportional to the round-trip distance traveled by sound waves.

Expected Physical Output

Serial Monitor outputs distance in cm with 1 decimal precision, updating 3 times per second as obstacles move.
Troubleshooting Tips
  • •Zero distance reported: Check if TRIG and ECHO pins are wired backwards.
  • •Inaccurate measurements: Sound reflections bounce off soft fabrics; use a hard flat surface (like a book or wall) for testing.
Electrical Safety Directives
  • •Sensor sound is 40kHz (inaudible to human ears); safe and harmless.
Companion Resources

Related Book Chapter & Hardware Kit

Covered in depth within Chapter 6: Sensors and Their Applications. Build this easily with the Sensor Explorer Kit.