Blog

HC-SR04 Ultrasonic Distance Sensor Module – Complete Guide

Ultrasonic Distance Sensor

HC-SR04 Ultrasonic Distance Sensor Module

Accurate Non-Contact Ranging from 2cm to 400cm

The HC-SR04 is the most widely used ultrasonic ranging module in hobby electronics. It works on the same time-of-flight principle bats and sonar use: a 10-microsecond pulse on the Trigger pin fires a burst of eight 40kHz ultrasonic waves from the transmitter; these travel outward, bounce off whatever object they hit, and return to the receiver. The module then holds its Echo pin HIGH for exactly as long as that round trip took. Since sound travels through air at a known, fairly constant speed (~343 m/s at room temperature), your microcontroller only needs to time that Echo pulse and multiply by the speed of sound - divided by two, since the pulse covers the distance twice - to get an accurate reading in centimeters. Because the measurement is purely acoustic, it works reliably regardless of the target's color, transparency, or lighting conditions, which is exactly where infrared distance sensors tend to struggle. With just two digital pins and no I2C or SPI setup required, it remains one of the fastest sensors to get working on any microcontroller.

2cm – 400cm
Range
± 3mm
Accuracy
Digital Trigger / Echo
Interface

Key features

Simple 2-Pin Interface

No I2C or SPI needed - a single Trigger pulse in and an Echo pulse out is all it takes to get a distance reading.

40kHz Ultrasonic Transducers

Dedicated transmit and receive transducers give a clean, reliable echo signal independent of ambient light.

Wide Measuring Angle

A roughly 15° detection cone makes it forgiving for robot-mounted obstacle sensing.

Millimeter-Level Resolution

Timing-based measurement gives sub-centimeter resolution, far finer than most IR distance sensors.

Low Cost, Beginner Friendly

One of the cheapest reliable distance sensors available, with huge community support and example code.

Works Well Indoors

Unaffected by target color or transparency, unlike IR sensors which struggle with dark or glass surfaces.

Technical specifications

Operating Voltage 5V DC
Operating Current ~15 mA
Operating Frequency 40 kHz
Maximum Range 400 cm (4 m)
Minimum Range 2 cm
Measuring Angle ~15°
Trigger Input 10µs TTL pulse
Echo Output TTL pulse, width proportional to distance
Dimensions 45 × 20 × 15 mm

Pinout

PinFunction
VCC 5V power supply
Trig Trigger input - send a 10µs HIGH pulse to start a measurement
Echo Echo output - stays HIGH for a duration proportional to distance
GND Ground

Applications

Robot obstacle avoidance and navigation Automated parking distance sensors Liquid level sensing in open tanks Motion-triggered security projects Interactive distance-based art installations Smart bin fill-level detection

What's in the box

1x HC-SR04 ultrasonic sensor module
4-pin header pre-soldered

Downloads

Datasheet (PDF) Arduino Library (NewPing)
🔌
Pin Connections
1 Table
Pin Connections Table 1
BoardSensor PinConnection
HC-SR04VCC/GND/TRIG/ECHO5V / GND / board TRIG pin / board ECHO pin
📟
HC-SR04 Ultrasonic Distance Sensor Module
7 Examples • C/C++ | Boards: Arduino Uno, Arduino Nano, Arduino Mega, ESP32, ESP8266, STM32, Raspberry Pi Pico
HC-SR04 Ultrasonic Distance Sensor Module - Arduino Uno
HC-SR04 Ultrasonic Distance Sensor Module - Arduino Nano
HC-SR04 Ultrasonic Distance Sensor Module - Arduino Mega
HC-SR04 Ultrasonic Distance Sensor Module - ESP32
HC-SR04 Ultrasonic Distance Sensor Module - ESP8266
HC-SR04 Ultrasonic Distance Sensor Module - STM32
HC-SR04 Ultrasonic Distance Sensor Module - Raspberry Pi Pico
📄 File: hc-sr04_ultrasonic_distance_sensor_module_-_arduino_uno.inoArduino Uno
713 characters
1//ekostra.com
2//electronics store in egypt
3//+201008896258
4
5// TRIG=9, ECHO=10 on Arduino Uno
6#define TRIG_PIN 9
7#define ECHO_PIN 10
8
9void setup() {
10 Serial.begin(115200);
11 pinMode(TRIG_PIN, OUTPUT);
12 pinMode(ECHO_PIN, INPUT);
13}
14
15void loop() {
16 digitalWrite(TRIG_PIN, LOW);
17 delayMicroseconds(2);
18 digitalWrite(TRIG_PIN, HIGH);
19 delayMicroseconds(10);
20 digitalWrite(TRIG_PIN, LOW);
21
22 long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout (~5m max range)
23 float distanceCm = duration * 0.0343 / 2.0;
24
25 if (duration == 0) {
26 Serial.println("No echo received (out of range).");
27 } else {
28 Serial.print("Distance: "); Serial.print(distanceCm); Serial.println(" cm");
29 }
30
31 delay(200);
32}
📄 File: hc-sr04_ultrasonic_distance_sensor_module_-_arduino_nano.inoArduino Nano
714 characters
1//ekostra.com
2//electronics store in egypt
3//+201008896258
4
5// TRIG=9, ECHO=10 on Arduino Nano
6#define TRIG_PIN 9
7#define ECHO_PIN 10
8
9void setup() {
10 Serial.begin(115200);
11 pinMode(TRIG_PIN, OUTPUT);
12 pinMode(ECHO_PIN, INPUT);
13}
14
15void loop() {
16 digitalWrite(TRIG_PIN, LOW);
17 delayMicroseconds(2);
18 digitalWrite(TRIG_PIN, HIGH);
19 delayMicroseconds(10);
20 digitalWrite(TRIG_PIN, LOW);
21
22 long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout (~5m max range)
23 float distanceCm = duration * 0.0343 / 2.0;
24
25 if (duration == 0) {
26 Serial.println("No echo received (out of range).");
27 } else {
28 Serial.print("Distance: "); Serial.print(distanceCm); Serial.println(" cm");
29 }
30
31 delay(200);
32}
📄 File: hc-sr04_ultrasonic_distance_sensor_module_-_arduino_mega.inoArduino Mega
714 characters
1//ekostra.com
2//electronics store in egypt
3//+201008896258
4
5// TRIG=9, ECHO=10 on Arduino Mega
6#define TRIG_PIN 9
7#define ECHO_PIN 10
8
9void setup() {
10 Serial.begin(115200);
11 pinMode(TRIG_PIN, OUTPUT);
12 pinMode(ECHO_PIN, INPUT);
13}
14
15void loop() {
16 digitalWrite(TRIG_PIN, LOW);
17 delayMicroseconds(2);
18 digitalWrite(TRIG_PIN, HIGH);
19 delayMicroseconds(10);
20 digitalWrite(TRIG_PIN, LOW);
21
22 long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout (~5m max range)
23 float distanceCm = duration * 0.0343 / 2.0;
24
25 if (duration == 0) {
26 Serial.println("No echo received (out of range).");
27 } else {
28 Serial.print("Distance: "); Serial.print(distanceCm); Serial.println(" cm");
29 }
30
31 delay(200);
32}
📄 File: hc-sr04_ultrasonic_distance_sensor_module_-_esp32.inoESP32
860 characters
1//ekostra.com
2//electronics store in egypt
3//+201008896258
4
5// TRIG=5, ECHO=18 on ESP32
6// NOTE: HC-SR04's ECHO pin idles at 5V logic. On this 3.3V board, add a simple
7// voltage divider (e.g. 1k + 2k resistors) on ECHO before wiring it in.
8#define TRIG_PIN 5
9#define ECHO_PIN 18
10
11void setup() {
12 Serial.begin(115200);
13 pinMode(TRIG_PIN, OUTPUT);
14 pinMode(ECHO_PIN, INPUT);
15}
16
17void loop() {
18 digitalWrite(TRIG_PIN, LOW);
19 delayMicroseconds(2);
20 digitalWrite(TRIG_PIN, HIGH);
21 delayMicroseconds(10);
22 digitalWrite(TRIG_PIN, LOW);
23
24 long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout (~5m max range)
25 float distanceCm = duration * 0.0343 / 2.0;
26
27 if (duration == 0) {
28 Serial.println("No echo received (out of range).");
29 } else {
30 Serial.print("Distance: "); Serial.print(distanceCm); Serial.println(" cm");
31 }
32
33 delay(200);
34}
📄 File: hc-sr04_ultrasonic_distance_sensor_module_-_esp8266.inoESP8266
864 characters
1//ekostra.com
2//electronics store in egypt
3//+201008896258
4
5// TRIG=D5, ECHO=D6 on ESP8266
6// NOTE: HC-SR04's ECHO pin idles at 5V logic. On this 3.3V board, add a simple
7// voltage divider (e.g. 1k + 2k resistors) on ECHO before wiring it in.
8#define TRIG_PIN D5
9#define ECHO_PIN D6
10
11void setup() {
12 Serial.begin(115200);
13 pinMode(TRIG_PIN, OUTPUT);
14 pinMode(ECHO_PIN, INPUT);
15}
16
17void loop() {
18 digitalWrite(TRIG_PIN, LOW);
19 delayMicroseconds(2);
20 digitalWrite(TRIG_PIN, HIGH);
21 delayMicroseconds(10);
22 digitalWrite(TRIG_PIN, LOW);
23
24 long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout (~5m max range)
25 float distanceCm = duration * 0.0343 / 2.0;
26
27 if (duration == 0) {
28 Serial.println("No echo received (out of range).");
29 } else {
30 Serial.print("Distance: "); Serial.print(distanceCm); Serial.println(" cm");
31 }
32
33 delay(200);
34}
📄 File: hc-sr04_ultrasonic_distance_sensor_module_-_stm32.inoSTM32
866 characters
1//ekostra.com
2//electronics store in egypt
3//+201008896258
4
5// TRIG=PB0, ECHO=PB1 on STM32
6// NOTE: HC-SR04's ECHO pin idles at 5V logic. On this 3.3V board, add a simple
7// voltage divider (e.g. 1k + 2k resistors) on ECHO before wiring it in.
8#define TRIG_PIN PB0
9#define ECHO_PIN PB1
10
11void setup() {
12 Serial.begin(115200);
13 pinMode(TRIG_PIN, OUTPUT);
14 pinMode(ECHO_PIN, INPUT);
15}
16
17void loop() {
18 digitalWrite(TRIG_PIN, LOW);
19 delayMicroseconds(2);
20 digitalWrite(TRIG_PIN, HIGH);
21 delayMicroseconds(10);
22 digitalWrite(TRIG_PIN, LOW);
23
24 long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout (~5m max range)
25 float distanceCm = duration * 0.0343 / 2.0;
26
27 if (duration == 0) {
28 Serial.println("No echo received (out of range).");
29 } else {
30 Serial.print("Distance: "); Serial.print(distanceCm); Serial.println(" cm");
31 }
32
33 delay(200);
34}
📄 File: hc-sr04_ultrasonic_distance_sensor_module_-_raspberry_pi_pico.inoRaspberry Pi Pico
874 characters
1//ekostra.com
2//electronics store in egypt
3//+201008896258
4
5// TRIG=14, ECHO=15 on Raspberry Pi Pico
6// NOTE: HC-SR04's ECHO pin idles at 5V logic. On this 3.3V board, add a simple
7// voltage divider (e.g. 1k + 2k resistors) on ECHO before wiring it in.
8#define TRIG_PIN 14
9#define ECHO_PIN 15
10
11void setup() {
12 Serial.begin(115200);
13 pinMode(TRIG_PIN, OUTPUT);
14 pinMode(ECHO_PIN, INPUT);
15}
16
17void loop() {
18 digitalWrite(TRIG_PIN, LOW);
19 delayMicroseconds(2);
20 digitalWrite(TRIG_PIN, HIGH);
21 delayMicroseconds(10);
22 digitalWrite(TRIG_PIN, LOW);
23
24 long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout (~5m max range)
25 float distanceCm = duration * 0.0343 / 2.0;
26
27 if (duration == 0) {
28 Serial.println("No echo received (out of range).");
29 } else {
30 Serial.print("Distance: "); Serial.print(distanceCm); Serial.println(" cm");
31 }
32
33 delay(200);
34}
Classic ultrasonic distance sensor - trigger a pulse, measure the echo return time.
The Echo pin outputs a full 5V signal - if you are using a 3.3V-only board like the ESP32 or ESP8266, add a simple voltage divider (e.g. 1k + 2k resistors) on the Echo line before connecting it to your microcontroller to avoid damaging the GPIO pin. For steadier readings, average 3-5 consecutive measurements rather than trusting a single reading, and leave at least 60ms between measurements so a stray echo from the previous pulse cannot be mistaken for the next one. Soft or angled surfaces (foam, cloth, sharply angled walls) can scatter the pulse away from the receiver and cause missed readings - this is a sensor limitation, not a wiring fault, so always add a timeout in code for when Echo never goes HIGH.