HC-SR04 Ultrasonic Sensor
Measures approximate distance using ultrasonic pulse travel time.
What is the HC-SR04 Ultrasonic Sensor and what is it used for?
Measures approximate distance using ultrasonic pulse travel time. Makers use it to build a non-safety-critical obstacle alert.
Educational distance measurement
Sensors convert a physical quantity into an electrical signal. Calibrate, verify module supply, interfaces and environmental limits.
Key specifications
| Sensor spec | Value |
|---|---|
| Measures | Distance by ultrasonic echo (40 kHz) |
| Range | 2 to 400 cm (reliable to about 250 cm) |
| Accuracy | About ±3 mm in good conditions |
| Interface | Trigger pulse in, echo pulse out |
| Supply voltage | 5 V, about 15 mA |
| Operating temperature | 0 to 60 °C typical |
Pinout
How to use it
Distance in cm = echo time in µs ÷ 58. On 3.3 V boards, divide the Echo line (for example 1 kΩ and 2 kΩ) or use the HC-SR04P version.
const int TRIG = 9, ECHO = 10;
void setup() { Serial.begin(9600); pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); }
void loop() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long t = pulseIn(ECHO, HIGH, 30000);
Serial.println(t / 58);
delay(100);
}Applications
Builds that use it
Ultrasonic distance alert
Ultrasonic distance display
Obstacle-avoiding robot
Touchless smart dustbin
Ultrasonic room intrusion
Ultrasonic theremin with real tone
Society dustbin full-level reporter
Roaming pet toy robot
Common questions
What is the HC-SR04 Ultrasonic Sensor used for?
In products: educational distance measurement. In maker projects: build a non-safety-critical obstacle alert.
How does the HC-SR04 Ultrasonic Sensor work?
Sensors convert a physical quantity into an electrical signal. Calibrate, verify module supply, interfaces and environmental limits.
What voltage does the HC-SR04 Ultrasonic Sensor need?
5 V, about 15 mA.
How do I connect the HC-SR04 Ultrasonic Sensor to an Arduino or ESP32?
Trigger pulse in, echo pulse out. Pins: VCC (5 V), Trig (10 µs HIGH pulse starts a reading), Echo (HIGH for the round-trip time (5 V signal)), GND (Ground).
