- All products
- Sensors & Modules
- Ultrasonic HC-SR04 Module Distance Sensor
- Sensors & Modules
Features:
- Supply voltage: 5V (DC).
- Supply current: 15mA.
- Output: 0 – 5V (Output high when obstacle detected in range).
- Beam Angle: Max 15 degrees.
- Distance: 2cm – 400cm.
- Accuracy: 0.3cm.
- Communication: Positive TTL pulse.
Principle of Work:
The ultrasonic sensor operates on the same principles as the SONAR and RADAR systems, which are used to calculate the distance to an object. The high-frequency sound (ultrasound) waves are produced by an ultrasonic sensor. When this ultrasound strikes an object, it reflects as an echo, which the receiver detects. We can calculate the distance by measuring the time it takes for the echo to reach the receiver. This is the fundamental operation of the Ultrasonic module for measuring distance.

The HC-SR-04 has an ultrasonic transmitter, receiver, and control circuit. We must issue a trigger pulse to the ultrasonic module HCSR04 in order for it to generate ultrasound at a frequency of 40 kHz. It raises the echo pin after producing ultrasound, i.e. 8 pulses at 40 kHz. The echo pin remains high until the echo sound is not received. As a result, the width of the echo pin will be the time it takes for sound to travel to the object and return. We can calculate distance once we have the time and the speed of sound. The HC-SR04 has a measurement range of 2 cm to 400 cm.
At 20°C, the speed of sound is approximately 343 m/s (0.034 cm/s). Assume that the time interval between sending and receiving sound waves is 2000 microseconds. The distance traveled by sound waves is calculated by multiplying the speed of sound by the time the waves traveled.
Distance = Time x Speed
However, this is not the desired outcome. Because sound waves travel from the sensor to the object and back, the distance between the sensor and the object is actually only half this distance. As a result, divide the result by two.
Distance (cm) = Sound speed (cm/s) / Time (s)
Pinout of the Board:

| VCC | Powers the sensor (5V) |
| Trig | Trigger Input Pin |
| Echo | Echo Output Pin |
| GND | Common GND |
Applications:
- Anti-Collision Detection.
- People Detection.
- Presence Detection.
- Box Sorting using a Multi-Transducer System.
- Easy Control of Trash Collection Vehicles.
- Pallet Detection with Forklifts.
- Bottle Counting on Drink Filling Machine
- Brushless DC electric motors detect the position of the permanent magnet.
- Computer printers to detect missing paper and open covers.
Circuit:
The module's Ground and VCC pins must be connected to the Arduino Board's Ground and 5 volts pins, respectively, and the trig and echo pins to 11 -12 Digital I/O pin on the Arduino Board.

| Ultrasonic Sensor HC-SR04 | Arduino |
| VCC | 5V |
| Trig | Pin 11 |
| Echo | Pin 12 |
| GND | GND |
Library:
No Need for Library for this module
Code:
we'll read values from both the digital and analog interfaces. The Arduino's LED will turn on when a magnetic field is detected by the digital interface.
The potentiometer and input voltage set the initial value for the analog interface, which will change depending on the strength of the magnetic field.
int trigPin = 11; // Trigger
int echoPin = 12; // Echo
long duration, cm, inches;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(250);
}
Technical Details:
| Operating Voltage | 5V DC |
| Operating Current | 15mA |
| Operating Frequency | 40KHz |
| Min Range | 2cm / 1 inch |
| Max Range | 400cm / 13 feet |
| Accuracy | 3mm |
| Measuring Angle | <15° |
| Dimension | 45 x 20 x 15mm |
Resources:
- Tutorial
- Datasheet
- HC-SR04 Raspberry Pi Python Script Here
- HC-SR04 Dimensions
- HC-SR04 Timing Diagram
Comparisons:
The primary distinction between a PIR sensor and an ultrasonic sensor is that a PIR sensor detects the presence of an object by sensing the difference between heat emitted by the movement of the object and heat emitted by the background, whereas an ultrasonic sensor detects the presence of an object by emitting ultrasonic sound waves and measuring the speed at which the waves return. A PIR sensor is an electronic sensor that can measure the IR light emitted by objects in its field of view, whereas an ultrasonic sensor can measure the distance to an object using ultrasonic sound waves. ultrasonic Tends to consume lower current/power than PIR, but still ultrasonic has a limited detection range in comparison to PIR
Features:
- Supply voltage: 5V (DC).
- Supply current: 15mA.
- Output: 0 – 5V (Output high when obstacle detected in range).
- Beam Angle: Max 15 degrees.
- Distance: 2cm – 400cm.
- Accuracy: 0.3cm.
- Communication: Positive TTL pulse.
Principle of Work:
The ultrasonic sensor operates on the same principles as the SONAR and RADAR systems, which are used to calculate the distance to an object. The high-frequency sound (ultrasound) waves are produced by an ultrasonic sensor. When this ultrasound strikes an object, it reflects as an echo, which the receiver detects. We can calculate the distance by measuring the time it takes for the echo to reach the receiver. This is the fundamental operation of the Ultrasonic module for measuring distance.

The HC-SR-04 has an ultrasonic transmitter, receiver, and control circuit. We must issue a trigger pulse to the ultrasonic module HCSR04 in order for it to generate ultrasound at a frequency of 40 kHz. It raises the echo pin after producing ultrasound, i.e. 8 pulses at 40 kHz. The echo pin remains high until the echo sound is not received. As a result, the width of the echo pin will be the time it takes for sound to travel to the object and return. We can calculate distance once we have the time and the speed of sound. The HC-SR04 has a measurement range of 2 cm to 400 cm.
At 20°C, the speed of sound is approximately 343 m/s (0.034 cm/s). Assume that the time interval between sending and receiving sound waves is 2000 microseconds. The distance traveled by sound waves is calculated by multiplying the speed of sound by the time the waves traveled.
Distance = Time x Speed
However, this is not the desired outcome. Because sound waves travel from the sensor to the object and back, the distance between the sensor and the object is actually only half this distance. As a result, divide the result by two.
Distance (cm) = Sound speed (cm/s) / Time (s)
Pinout of the Board:

| VCC | Powers the sensor (5V) |
| Trig | Trigger Input Pin |
| Echo | Echo Output Pin |
| GND | Common GND |
Applications:
- Anti-Collision Detection.
- People Detection.
- Presence Detection.
- Box Sorting using a Multi-Transducer System.
- Easy Control of Trash Collection Vehicles.
- Pallet Detection with Forklifts.
- Bottle Counting on Drink Filling Machine
- Brushless DC electric motors detect the position of the permanent magnet.
- Computer printers to detect missing paper and open covers.
Circuit:
The module's Ground and VCC pins must be connected to the Arduino Board's Ground and 5 volts pins, respectively, and the trig and echo pins to 11 -12 Digital I/O pin on the Arduino Board.

| Ultrasonic Sensor HC-SR04 | Arduino |
| VCC | 5V |
| Trig | Pin 11 |
| Echo | Pin 12 |
| GND | GND |
Library:
No Need for Library for this module
Code:
we'll read values from both the digital and analog interfaces. The Arduino's LED will turn on when a magnetic field is detected by the digital interface.
The potentiometer and input voltage set the initial value for the analog interface, which will change depending on the strength of the magnetic field.
int trigPin = 11; // Trigger
int echoPin = 12; // Echo
long duration, cm, inches;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(250);
}
Technical Details:
| Operating Voltage | 5V DC |
| Operating Current | 15mA |
| Operating Frequency | 40KHz |
| Min Range | 2cm / 1 inch |
| Max Range | 400cm / 13 feet |
| Accuracy | 3mm |
| Measuring Angle | <15° |
| Dimension | 45 x 20 x 15mm |
Resources:
- Tutorial
- Datasheet
- HC-SR04 Raspberry Pi Python Script Here
- HC-SR04 Dimensions
- HC-SR04 Timing Diagram
Comparisons:
The primary distinction between a PIR sensor and an ultrasonic sensor is that a PIR sensor detects the presence of an object by sensing the difference between heat emitted by the movement of the object and heat emitted by the background, whereas an ultrasonic sensor detects the presence of an object by emitting ultrasonic sound waves and measuring the speed at which the waves return. A PIR sensor is an electronic sensor that can measure the IR light emitted by objects in its field of view, whereas an ultrasonic sensor can measure the distance to an object using ultrasonic sound waves. ultrasonic Tends to consume lower current/power than PIR, but still ultrasonic has a limited detection range in comparison to PIR
"

