The HC-SR04 module is a specific ultrasonic sensor module commonly used for distance measurement. It comprises an ultrasonic transmitter-receiver pair and control circuitry, all integrated into a small board.
The HC-SR04 operates by sending out a burst of ultrasonic sound waves and then measuring the time it takes to bounce off an object and return to the sensor. Using the known speed of sound in air, the module calculates the distance to the object based on the time it takes for the sound waves to travel to the object and back.
A prominent characteristic of the HC-SR04 module is its straightforwardness and user-friendly nature. Typically demanding only minimal connections to a microcontroller or comparable electronic device for functionality, it is widely favoured among hobbyists, students, and innovators for various projects encompassing robotics, obstacle detection systems, and distance measurement tasks.

In this module, “T” represents the Transmitter, and “R” denotes the Receiver.

An Ideal Signal timing diagram is shown in Fig 2. The practical timing diagram is captured in an Oscilloscope and is demonstrated in the following sections.
Pin descriptions of HC-SR04
VCC: VCC is the module’s power pin. +5V DC is applied to this pin.
Trig Pin: This pin triggers the module. The duty cycle of the trigger signal should be at least 10 microseconds. The captured trigger pulse is shown in Fig. 3
.

When a High-Level pulse of typically 10 microseconds is applied to the trigger pin, the module’s Transmitter transmits an Ultrasonic burst signal at 40KHz, consisting of 8 pulses. See Fig. 4
.
Ultrasonic 40KHz Burst Signal

There is approximately a 2ms delay between the trigger pulse’s falling edge and the burst signal’s starting edge in Fig 5
.

Echo Pin: The output state of the Echo Pin goes high-level just after ultrasonic burst signal transmission is completed. When the reflected ultrasonic signal from any obstacle is received at the receiver, the Echo pin goes low. This way, a pulse appears on the Echo Pin which duration is equal to the total travel time of the ultrasonic burst signal Fig.6 and Fig.7.


Ground: This is the common ground of the module and is connected to the Arduino Board’s common ground.
Distance Measurement:
When a trigger pulse is sent to the module, an ultrasonic burst signal is transmitted from the transmitter, the echo pin is set to HIGH, and a timer counter starts. Then, wait for the echo signal to be received on the module’s receiver. When the echo is received, the Echo Pin sets to LOW status, and the timer stops. The time between sending the Ultrasonic burst signal and receiving the echo signal is equal to the duty cycle of the pulse received from the Echo Pin.
Calculate Distance: Use the formula below to calculate the distance to the obstacle based on the time measured:
The speed of sound in air at room temperature is approximately 346 meters per second (m/s).
Since the ultrasonic pulse travels to the object and back, we divide the total time by 2 to get the one-way distance.
Connection with Arduino


Arduino Code:
Assign the Trigger and Echo pin to the Arduino board. Also, the pins can be assigned to any Arduino IO pins. In this tutorial, A0 and A1 are used.
const char trigPin = A0; const char echoPin = A1;
Initialization of the global variable
float duration, distance; //global variables
Initial setups
void setup() {
pinMode(trigPin, OUTPUT); // configure trigger pin as OUTPUT pin
pinMode(echoPin, INPUT); // configure echo pin as INPUT pin
digitalWrite(trigPin, LOW); // initially set trigger pin LOW
Serial.begin(9600); // serial baud rate
}
This code sends a 10-microsecond trigger pulse to transmit the Ultrasonic burst signal.
digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
pulseIn(arg, arg) is the inbuilt Arduino function that counts the duration of any pulse. It can be configured on either edge of the pulse signal. In this code, when the echo pulse goes HIGH, it starts a built-in timer function millis(), and when the echo pulse goes LOW, it stops the counting and returns the pulse duration in milliseconds.
duration = pulseIn(echoPin, HIGH);
Complete Arduino Code
const int trigPin = A0;
const int echoPin = A1;
float duration, distance; //global variables
void setup() {
pinMode(trigPin, OUTPUT); // configure trigger pin as OUTPUT pin
pinMode(echoPin, INPUT); // configure echo pin as INPUT pin
digitalWrite(trigPin, LOW); // initially set trigger pin LOW
Serial.begin(9600); // serial baud rate
}
void loop() {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //capture the duration of the pulse received from echo pin
duration /=1000; // convert the duration Seconds
distance = (duration*34.6)/2; // Calculation of distance
Serial.print("Distance: ");
Serial.println(distance);
delay(100);
}