Components / Sensors / HC-SR04 Ultrasonic Sensor

HC-SR04 Ultrasonic Sensor

Measures approximate distance using ultrasonic pulse travel time.

MeasuresDistance by ultrasonic echo (40 kHz)
Range2 to 400 cm (reliable to about 250 cm)
AccuracyAbout ±3 mm in good conditions
InterfaceTrigger pulse in, echo pulse out
SensorsReviewedMPN HC-SR04Used in 38 projects
In short

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.

Soft or angled surfaces return weak echoes. For water tanks use the waterproof JSN-SR04T.
In the real world

Educational distance measurement

How it works

Sensors convert a physical quantity into an electrical signal. Calibrate, verify module supply, interfaces and environmental limits.

Key specifications

Sensor specValue
MeasuresDistance by ultrasonic echo (40 kHz)
Range2 to 400 cm (reliable to about 250 cm)
AccuracyAbout ±3 mm in good conditions
InterfaceTrigger pulse in, echo pulse out
Supply voltage5 V, about 15 mA
Operating temperature0 to 60 °C typical

Pinout

VCC5 V
Trig10 µs HIGH pulse starts a reading
EchoHIGH for the round-trip time (5 V signal)
GNDGround

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

Buy at

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).