SensorsQUICK REFERENCE
HC-SR04
Turn an echo pulse into distance, with the level shifting that matters.
01Four connections
- VCC / GND
- 5 V supply / common ground for the standard HC-SR04.
- TRIG
- MCU output; send a HIGH pulse of at least 10 microseconds.
- ECHO
- Pulse-width output. Standard 5 V module needs level shifting into 3.3 V GPIO.
02Measurement
- Sequence
- TRIG low → 10 us high → low → time ECHO high. Leave at least 60 ms between triggers.
- Formula
- distance_cm = echo_us × 0.0343 / 2 (speed of sound near 20 °C).
- Range
- Nominal 2–400 cm; soft, small or angled surfaces can return poor echoes.
- Timeout
- Set a finite timeout (~30 ms). A zero/timeout means no valid measurement, not zero distance.
033.3 V connection
- Divider
- ECHO → 1 kohm → GPIO node → 2 kohm → GND gives about 3.33 V from 5 V.
- Validate
- Use a rated level shifter if supply/tolerance may exceed GPIO limits. Check trigger compatibility.