HC-SR04 Ultrasonic Distance Sensor with Arduino & ESP32: Complete Beginner Guide (2026)
The HC-SR04 Ultrasonic Distance Sensor is one of the most popular modules for measuring distance in Arduino, ESP32, robotics, and IoT projects. It provides accurate, contactless distance measurement using ultrasonic sound waves, making it ideal for beginners and professionals alike.
Whether you're building an obstacle-avoiding robot, a smart parking system, a water-level monitor, or an automation project, the HC-SR04 is an affordable and reliable solution.
What is the HC-SR04?
The HC-SR04 is a 40kHz ultrasonic distance sensor capable of measuring distances from 2cm to 400cm. It works by sending ultrasonic pulses and calculating the time required for the reflected signal to return.
Key Specifications
- Operating Voltage: 5V DC
- Measuring Range: 2cm – 400cm
- Accuracy: ±3mm
- Operating Frequency: 40kHz
- Measuring Angle: 15°
- Easy interface with Arduino and ESP32
Pin Configuration
The HC-SR04 contains four pins:
- VCC – 5V Power Supply
- TRIG – Trigger Input
- ECHO – Echo Output
- GND – Ground
The TRIG pin starts the ultrasonic pulse, while the ECHO pin outputs a pulse whose duration corresponds to the measured distance.
Arduino Wiring
| HC-SR04 | Arduino Uno |
|---|---|
| VCC | 5V |
| GND | GND |
| TRIG | D9 |
| ECHO | D10 |
The Arduino Uno works directly with the HC-SR04 because both devices operate at 5V logic.
ESP32 Wiring
The HC-SR04 is also compatible with the ESP32. Since the ESP32 uses 3.3V GPIO, the ECHO pin should be connected through a voltage divider or logic level converter to protect the microcontroller.
Arduino Example Code
const int trigPin = 9;
const int echoPin = 10;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.0343 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Common Applications
The HC-SR04 is commonly used in:
- Obstacle Avoidance Robots
- Smart Parking Systems
- Water Level Monitoring
- Automatic Dustbins
- Distance Measuring Devices
- Smart Home Automation
- Robotics Projects
- STEM Education
Tips for Better Accuracy
- Keep the sensor facing the object directly.
- Avoid soft surfaces that absorb ultrasonic waves.
- Use short jumper wires.
- Always share a common ground.
- Use a voltage divider on the ECHO pin when using ESP32.
Recommended Components
To build this project, you'll typically need:
- HC-SR04 Ultrasonic Distance Sensor
- Arduino Uno R3
- ESP32 Development Board
- Breadboard
- Jumper Wires
- SG90 Servo Motor
- L298N Motor Driver
Learn More
This article is a quick introduction to the HC-SR04.
For complete wiring diagrams, project images, troubleshooting, FAQs, and detailed explanations, visit the official Chip.pk Knowledge Hub.
Thanks for reading!
If you enjoy Arduino, ESP32, Raspberry Pi, robotics, IoT, and embedded systems, follow Chip.pk for more practical tutorials, project guides, and genuine electronic components.








Top comments (0)