iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Arduino ultrasonic distance measurement using HC-SR04 requires four connections and a timeout-aware sketch: power the 5-V sensor, trigger it for at least 10 microseconds, measure the ECHO pulse, and divide the round-trip time by two. With an Arduino Uno, connect VCC to 5V, GND to GND, TRIG to D9, and ECHO to D10.
The HC-SR04 is a practical, inexpensive choice for indoor Arduino projects, but its nominal 2–400 cm range is not a guarantee of accuracy on every surface or in every environment. This guide covers the wiring, code, timing, voltage-safety issue for 3.3-V boards, filtering, calibration, troubleshooting, and alternatives.
Key takeaways
- The HC-SR04 uses a 40-kHz ultrasonic burst and reports distance through the width of its ECHO pulse; 40 kHz is the acoustic carrier frequency, not the recommended sampling rate.
- A 5-V Arduino Uno can connect directly to the traditional HC-SR04, but a 3.3-V board normally needs a voltage divider or logic-level shifter on ECHO.
- The datasheet’s nominal range is approximately 2–400 cm, while practical results depend on target size, angle, surface, temperature, mounting, and the particular module.
- A 10-microsecond TRIG pulse and a measurement interval of more than 60 milliseconds are suitable baseline timings for a beginner project.
- The code must give
pulseIn()a timeout and treat a returned zero as “no echo” or “out of range,” not as a real 0-cm measurement.
What do you need for Arduino ultrasonic distance measurement using HC-SR04?
For a basic Arduino ultrasonic distance measurement using HC-SR04, you need:
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →- An Arduino Uno, Nano, or another compatible 5-V Arduino board
- An HC-SR04 ultrasonic module
- Four jumper wires
- A USB cable
- A computer with the Arduino IDE or another compatible development environment
A breadboard is optional. A voltage divider or logic-level shifter is required when the HC-SR04 is connected to a 3.3-V GPIO. An LCD, OLED, buzzer, or LED is also optional; the Serial Monitor is the simplest way to verify that the sensor works.
#1 Best Overall
- NON-CONTACT DISTANCE SENSING: Add object detection to robot navigation, parking-distance prototypes, automatic lids, counters and interactive projects; each HC-SR04 uses a 40 kHz ultrasonic burst and echo timing to estimate distance
- 5-PACK FOR REPEATABLE PROTOTYPING: Use multiple HC-SR04 modules across builds, compare sensor positions or keep spares for testing and replacement; each module integrates an ultrasonic transmitter, receiver and control circuit
- 5 V MODULE WITH 3-450 CM RANGE: Connect VCC, Trig, Echo and GND, use a 10 µs trigger pulse and measure Echo duration; resolution is 0.3 cm with an effective angle under 15°, while the controller board and external power source are not included
- PROTECT 3.3 V GPIO: The HC-SR04 operates from 5 V and its Echo output is 5 V, so use a voltage divider or suitable level shifting with 3.3 V inputs; keep the module dry and use it for prototyping rather than calibrated measurement
- FOR ROBOTICS & STEM PROJECTS: Suitable for distance measurement, object detection, automatic lids, parking alerts, robot navigation and other hands-on electronics builds
The HC-SR04 is a four-pin ultrasonic time-of-flight module. The module contains an ultrasonic transmitter, receiver, and control circuitry, so the Arduino does not read an analog voltage. The Arduino starts a measurement and measures the duration of a digital ECHO pulse. The HC-SR04 datasheet specifies a nominal 40-kHz carrier, approximately 15° measuring angle, approximately 15 mA operating current, and an approximate 2–400 cm range.
How does the HC-SR04 measure distance?
Arduino ultrasonic distance measurement using HC-SR04 is a round-trip time measurement. The Arduino sends a trigger pulse, the module emits sound, and the module keeps ECHO HIGH for the time required for the ultrasonic reflection to return.
- The Arduino drives TRIG LOW briefly to establish a known starting state.
- The Arduino drives TRIG HIGH for at least 10 microseconds.
- The Arduino drives TRIG LOW again.
- The HC-SR04 emits an eight-cycle, 40-kHz ultrasonic burst.
- The HC-SR04 raises ECHO HIGH while waiting for the reflected sound.
- The HC-SR04 lowers ECHO when the round-trip measurement ends.
- The Arduino measures the ECHO HIGH duration with
pulseIn(). - The sketch converts the duration into a distance.
The fundamental calculation is:
distance = echo time × speed of sound ÷ 2
The division by two is essential because the sound travels from the sensor to the target and then back to the receiver. The datasheet’s convenient approximations are:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsdistance in centimeters = echo time in microseconds ÷ 58
distance in inches = echo time in microseconds ÷ 148
At approximately 20 °C, an explicit calculation uses approximately 343 metres per second for the speed of sound:
distanceCm = durationUs * 0.0343 / 2.0;
Air temperature changes the speed of sound, so an HC-SR04 result is normally a distance estimate rather than an invariant physical measurement. Wind, airflow, temperature gradients, and outdoor conditions can also make readings less stable.
What are the HC-SR04 pins?
The standard HC-SR04 exposes four pins, but cloned modules can vary in PCB layout and labeling. Read the markings on the actual module before applying power.
| Pin | Function | Arduino Uno connection |
|---|---|---|
| VCC | 5-V power input | 5V |
| TRIG | Trigger input | D9 |
| ECHO | Pulse-width output | D10 |
| GND | Ground | GND |
The HC-SR04 is not an analog distance sensor. TRIG is a digital input, and ECHO is a digital pulse whose duration represents the measured round-trip travel time.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11How do you wire an HC-SR04 to an Arduino Uno?
For an Arduino Uno R3, connect VCC to 5V, GND to GND, TRIG to D9, and ECHO to D10. The Arduino Uno Rev3 specifications identify the Uno as a 5-V board, making this direct logic connection appropriate for the traditional 5-V HC-SR04.
| HC-SR04 | Arduino Uno | Purpose |
|---|---|---|
| VCC | 5V | Sensor power |
| GND | GND | Shared electrical reference |
| TRIG | D9 | Arduino measurement trigger |
| ECHO | D10 | Pulse-width measurement input |
The D9 and D10 assignment is only an example. Any suitable digital output and digital input can be used if the pin numbers in the sketch are changed to match the wiring.
Can you connect an HC-SR04 directly to a 3.3-V board?
Do not normally connect the traditional HC-SR04 ECHO output directly to a 3.3-V GPIO. The traditional module is powered at 5 V and its ECHO signal is described as a 5-V TTL-level output, so use a voltage divider or logic-level shifter unless the specific board and sensor documentation explicitly says direct connection is safe.
Rank #2
- HC-SR04 Ultrasonic Sensor:This is a device that can use sound waves to measure the distance of an object. It measures distance by emitting a sound wave of a specific frequency and listening to the bounce of that sound wave. The distance between the sonar sensor and the object can be calculated by recording the time elapsed between the generation of the sound wave and the bounce of the sound wave
- Working Voltage: 5V DC;Quiescent current: less than 2mA
- Ranging Distance:2cm - 450 cm;High precision: 0.3 cm
- Effectual Angle: <15°
- Test mode :Test distance = ((Duration of high level)*(Sonic :340m/s))/2
A common resistor divider is:
HC-SR04 ECHO ── 1 kΩ ── 3.3-V GPIO input ── 2 kΩ ── GND
A 5-V ECHO signal becomes approximately 3.33 V with that divider. Confirm the GPIO voltage limits for the exact board before wiring. This warning applies to many Arduino MKR boards, Arduino Due, ESP32 boards, RP2040 boards, and Raspberry Pi boards; board names alone are not a substitute for checking the electrical specifications.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →The Adafruit HC-SR04 product documentation specifically warns about the 5-V echo signal and includes two 10-kΩ resistors with its product for lowering the signal to a level suitable for 3-V devices. Do not power a traditional HC-SR04 from 3.3 V unless the documentation for the particular module explicitly supports 3.3-V operation. Similar-looking modules are not necessarily electrically identical.
What Arduino code reads the HC-SR04?
The following no-library sketch works as a straightforward starting point for an Arduino Uno. The sketch uses built-in Arduino functions, including pinMode(), digitalWrite(), delayMicroseconds(), pulseIn(), and Serial; an external distance library is not required.
const byte TRIG_PIN = 9;
const byte ECHO_PIN = 10;
const unsigned long ECHO_TIMEOUT_US = 30000UL;
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
digitalWrite(TRIG_PIN, LOW);
}
void loop() {
// Ensure the trigger starts LOW.
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Start one measurement with a 10-us trigger pulse.
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the HIGH time of the echo pulse.
unsigned long durationUs =
pulseIn(ECHO_PIN, HIGH, ECHO_TIMEOUT_US);
if (durationUs == 0) {
Serial.println("No echo / out of range");
} else {
float distanceCm = durationUs * 0.0343f / 2.0f;
float distanceIn = distanceCm / 2.54f;
Serial.print("Distance: ");
Serial.print(distanceCm, 1);
Serial.print(" cm / ");
Serial.print(distanceIn, 1);
Serial.println(" in");
}
// Avoid immediate retriggering and acoustic interference.
delay(60);
}
The 30,000-microsecond timeout prevents a missing echo from leaving the measurement waiting indefinitely. The Arduino pulseIn() reference documents the timeout form and the zero return when the requested pulse is not detected before the timeout expires.
The 60-millisecond delay is deliberately conservative. The HC-SR04 documentation recommends a measurement cycle of more than 60 ms to reduce the chance that a later trigger is confused with a lingering echo. The delay is not related to the sensor’s 40-kHz carrier frequency.
How do you upload the sketch and see the distance?
- Connect the Arduino to the computer with USB.
- Open the Arduino IDE.
- Select the correct board model under the board-selection menu.
- Select the serial port associated with the Arduino.
- Paste the sketch into a new project.
- Verify and upload the sketch.
- Open Serial Monitor.
- Set the Serial Monitor speed to 9600 baud.
- Place a broad, flat target such as a book or cardboard panel approximately 20–100 cm in front of the transducers.
A working setup should produce output similar to:
Distance: 42.7 cm / 16.8 in
Distance: 42.8 cm / 16.9 in
Distance: 42.6 cm / 16.8 in
The exact values will fluctuate. The example output demonstrates the format, not a guaranteed accuracy level.
How do you convert HC-SR04 readings to different units?
The duration returned by pulseIn() is in microseconds. The following formulas convert that duration into common units:
| Unit | Formula | Example code |
|---|---|---|
| Centimetres | echo time ÷ 58 | float distanceCm = durationUs / 58.0f; |
| Centimetres at approximately 20 °C | echo time × 0.0343 ÷ 2 | float distanceCm = durationUs * 0.0343f / 2.0f; |
| Inches | echo time ÷ 148 | float distanceIn = durationUs / 148.0f; |
| Millimetres | echo time × 0.343 ÷ 2 | float distanceMm = durationUs * 0.343f / 2.0f; |
Floating-point output gives decimal readings that are easier to inspect. Integer arithmetic can be adequate when the application only needs whole centimetres or when code simplicity is more important than fractional display values.
How can you make HC-SR04 readings more stable?
Unstable readings usually reflect acoustics, geometry, timing, movement, or electrical noise rather than a problem with the distance formula.
Improve the physical target
- Point both transducers directly at the target.
- Use a broad, hard, relatively flat target during testing.
- Avoid cloth, thick foam, soft curtains, narrow rods, open grilles, mesh, and highly angled surfaces.
- Mount the sensor rigidly and leave the transducer faces unobstructed.
- Keep the module away from vibrating motors and other loud acoustic sources.
- Do not measure through a narrow tube unless the tube was designed for the acoustic path.
The manufacturer documentation notes that target area and surface smoothness affect the result. The sensor measures along its acoustic axis; it does not necessarily report the shortest geometric distance to an arbitrary object.
Rank #3
- EPLZON HC-SR04 Ultrasonic ranging transducer sensor
- Test mode: Use IO to trigger high-level signals. (Not less than 10us), the module automatically sends 8 40kHz and detects whether there is a pulse signal return.
- Detection area: 0.78~196 in/(2cm~500cm); high precision: up to 0.12 inch/(0.3 cm), effective angle: less than 15°; Trigger input pulse width: 10uS
- Power supply: 5V DC; Quiescent current: less than 2mA;Dimension: 1.77 x 0.78 x 0.59 inches/45mm x 20mm x 15mm(length*width*height)
- Test distance=((high level duration)*(sound wave: 340m/s))/2
Filter occasional outliers
A median filter is useful when one or two samples occasionally jump to an implausible value. Take five valid readings, sort them, and use the middle reading. A moving average smooths random noise but responds more slowly when the target moves.
For an application with a known operating region, reject implausible values:
if (distanceCm >= 2.0f && distanceCm <= 400.0f) {
// Accept as plausible for this application
}
This check does not prove that every value inside 2–400 cm is accurate. It only rejects values outside the module’s nominal range or outside the chosen application limits.
Use hysteresis for alarms
If an alarm should respond to an obstacle, use separate on and off thresholds. For example, turn the alarm on below 25 cm and turn it off only above 30 cm. Hysteresis prevents rapid switching when readings fluctuate near one threshold.
Coordinate multiple sensors
Multiple ultrasonic modules can hear one another’s bursts. Trigger sensors sequentially and wait for the previous measurement to finish before starting the next one. Do not fire several modules simultaneously unless the system has a deliberate cross-talk strategy.
For robotics, a fixed delay(60) may block other work. More advanced designs can schedule measurements with a nonblocking state machine, timer or interrupt-based capture, a dedicated library, or hardware input capture where the platform supports it.
What are the HC-SR04 range and accuracy limitations?
The commonly quoted approximately 2–400 cm specification is a nominal module range, not a guarantee that every target can be measured accurately throughout that interval. Practical performance is often lower for small, soft, angled, absorbent, or irregular targets. Adafruit describes approximately 10–250 cm as a more useful range for ordinary projects using its product.
Free tools Windows power users keep installed
One-click scans. No signup required.
| Specification or condition | What it means in practice |
|---|---|
| Approximately 2 cm minimum | There is a close-range blind zone; do not promise reliable readings right against the transducers. |
| Approximately 400 cm nominal maximum | Long-range results require a suitable, broad, reflective target and favourable conditions. |
| Approximately 15° measuring angle | Treat the angle as a nominal specification, not a hard boundary for every object. |
| Claimed or specified accuracy up to approximately 3 mm | Qualify the figure as a module specification under suitable conditions, not a universal guarantee. |
| 40-kHz carrier | The number describes the ultrasonic burst frequency, not a recommended 40-readings-per-second update rate. |
Temperature changes the speed of sound. Strong airflow can also destabilize or shift measurements. Outdoor installations add wind, rain, condensation, temperature gradients, and mechanical vibration.
The standard HC-SR04 is not waterproof. It is a poor choice for rain, splashing, condensation, permanently exposed outdoor equipment, or tank-level monitoring unless the sensor is protected in a way that does not interfere with its acoustic path. Use a waterproof ultrasonic module or a different sensing technology when the environment requires it.
Glass, plastic, liquids, and transparent or reflective surfaces may behave differently from broad solid targets because the result depends on acoustic reflection and geometry.
Rank #4
- Power supply: 5 V DC; static current: < 2 mA; Effective angle: < 15 °; Level output: bottom 0V; Recognition distance: 2 cm ~ 450 cm; Resolution: 0.3 cm.
- Test removal: high timeline of the sound (340 m /s) /2
- On-board wiring methods: VCC, trig (control terminal), echo (receiving terminal), out (empty pin), GND.
- Equipped with an anti-reverse pin socket, which makes the cabling much closer and more convenient.
- Complete set, with 3PCS HC-SR04 Ultrasonic sensor module and 3 sets of Mounting Bracket and Cable.
How should you calibrate an HC-SR04?
Calibration can correct a repeatable offset or scale error in a fixed installation, but calibration cannot remove multipath reflections, blind-zone behaviour, bad target geometry, or unsuitable environmental conditions.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
- Place a large, flat, rigid target perpendicular to the sensor.
- Test at several known distances, such as 20, 50, 100, and 150 cm.
- Compare each reported value with a trusted ruler or tape measure.
- Look for a consistent offset or scale error.
- Apply a correction only if the mounting, target type, and environment will remain similar.
- Repeat the tests after changing temperature, enclosure, mounting angle, or target material.
A fixed offset correction can look like this:
correctedCm = measuredCm + offsetCm;
If testing demonstrates a scale error, a correction model can be:
correctedCm = measuredCm * scaleFactor + offsetCm;
Do not describe a corrected reading as universally accurate after calibration in one setup.
Why does the HC-SR04 return zero or an incorrect distance?
Why does the Serial Monitor show “No echo / out of range”?
The sketch prints “No echo / out of range” when pulseIn() returns zero after its 30,000-microsecond timeout. The most common causes are incorrect wiring, missing common ground, no 5-V supply, an unsuitable target, or a target outside the practical range.
- Check VCC, GND, TRIG, and ECHO against the wiring table.
- Confirm that the physical pin numbers match
TRIG_PIN = 9andECHO_PIN = 10, or change the code. - Measure the sensor supply voltage.
- Test with a large, flat book or cardboard target at 20–100 cm.
- Print
durationUsdirectly to determine whether an echo pulse is arriving. - On a 3.3-V board, verify the voltage-divider or level-shifter wiring.
- Try another jumper wire or module if the electrical checks pass.
Why is the reading always extremely large?
An extremely large result can indicate a floating or incorrectly connected ECHO line, a target that is absent or too far away, an invalid trigger signal, or a faulty level-conversion circuit on a 3.3-V board. Recheck the ECHO pin first and test against a nearby flat target.
Why is the reading stuck at one value?
A fixed reading can result from TRIG being held HIGH, ECHO being connected to a fixed HIGH or LOW signal, code that never returns TRIG LOW, a sensor pointed at a nearby enclosure wall, or an upload made to the wrong board or serial port.
Why do readings jump around?
Jitter commonly comes from angled or soft targets, narrow objects, reflections from nearby surfaces, sensor movement, vibrating hardware, electrical noise, or another ultrasonic module transmitting at the same time. Use a broad perpendicular target, rigid mounting, slower measurements, filtering, and sequential triggering.
Why does the Arduino reset?
Unexpected resets can result from a poor USB cable or supply, motor noise, long unshielded wires, incorrect power wiring, or ground noise. Use a stable regulated supply, improve grounding, add supply decoupling near the sensor, separate motor and logic wiring where practical, keep wires short, and stay within the board’s pin-current limits.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Is the HC-SR04 suitable for your project?
The HC-SR04 is a strong fit for a low-cost indoor beginner project with a 5-V Arduino board and a reasonably reflective target. The HC-SR04 is a poor fit when the project requires waterproofing, compact I2C hardware, guaranteed precision, long-range professional measurement, or direct 3.3-V operation without signal conversion.
Recommended Free Tools
| Project requirement | Recommended direction | Reason |
|---|---|---|
| Lowest-cost indoor beginner project | HC-SR04 | Simple trigger/echo interface and many Arduino examples |
| Arduino Uno or another 5-V board | HC-SR04 | Direct 5-V logic compatibility |
| 3.3-V board without a divider | US-100 or a verified 3.3-V-compatible module | Designed for 3–5-V operation and compatible logic options |
| Waterproof or outdoor measurement | JSN-SR04T-class waterproof sensor | Separate waterproof transducer and outdoor-oriented construction |
| Short-range compact I2C sensing | VL53L0X, VL53L4CD, or Arduino Modulino Distance | Compact digital time-of-flight hardware can avoid ultrasonic reflections |
| Long-range or professional ultrasonic measurement | URM06-class UART sensor | More robust housing and UART output, at substantially higher cost |
Waterproof modules vary by model, construction, wiring, and operating mode. Verify the documentation for the exact JSN-SR04T-type product instead of assuming that every listing behaves identically.
Best Value
- HC-SR04 Ultrasonic Sensor:Compatible with for Arduino R3 UNO MEGA Mega2560 Duemilanove XBee Nano Robot With 5Pcs mounting bracket
- Working Voltage: 5V DC; Quiescent current: Less than 2mA
- Ranging Distance:2 - 450 cm;High precision:0.3 cm;Effectual Angle: < 15°
- Test distance=((high level duration)*(sound wave: 340m/s))/2
- Merchandise included:5Pcs HC-SR04 Ultrasonic Sensor;5Pcs Mounting bracket;20Pcs Mounting screw;10Pcs Female to Female Wire; 10Pcs Male to Female Wire
What are the best HC-SR04 alternatives?
US-100
The US-100 is useful when a project needs 3–5-V operation, 3.3-V-compatible logic, an HC-SR04-style trigger/echo mode, optional UART mode, or temperature readings. Adafruit lists a nominal 2–450 cm detection range on its US-100 product page. The US-100 is not a drop-in replacement for every sketch because its modes and code differ.
RCWL-1601
The RCWL-1601 is an HC-SR04-compatible alternative designed for 3-V or 5-V use. The Adafruit RCWL-1601 documentation makes it worth considering when avoiding a level shifter matters more than using the original module. The RCWL-1601 remains an ultrasonic sensor and therefore retains similar target, reflection, and environmental limitations.
Optical time-of-flight sensors
A sensor such as the Adafruit VL53L0X or Arduino Modulino Distance may be preferable for a small target, a compact I2C design, a short-range application, or a 3.3-V ecosystem where ultrasonic reflections are problematic. These sensors are not drop-in replacements: they use different hardware, wiring, libraries, and measurement principles.
URM06
The Arduino URM06 is a substantially more expensive UART ultrasonic sensor for longer-range or more robust applications. The Arduino URM06 specifications list a 20 cm–10 m detection range, UART interface, and 6–12-V working voltage. The URM06 is not appropriate as a casual upgrade for most beginners because it costs more, uses UART rather than simple trigger/echo timing, and may require a different power arrangement.
What should you buy for an HC-SR04 project?
For the basic tutorial, buy the HC-SR04 and use an Arduino Uno or another known 5-V-compatible board. Prefer a reputable vendor when documentation and predictable pin behaviour matter. Generic modules can cost less, but cloned HC-SR04 boards may differ in pin labels, PCB revision, quality, logic behaviour, and documentation.
| Product | Observed price or signal | Best fit | Important limitation |
|---|---|---|---|
| Adafruit HC-SR04 with two 10-kΩ resistors | $3.95 single-unit price; observed August 18, 2026 | Beginner projects and 3.3-V setups needing divider resistors | Not waterproof or industrial |
| SparkFun HC-SR04 | $5.25; shown in stock; observed August 18, 2026 | Readers who value a documented retail source | Still 5-V and non-waterproof |
| Arduino Uno Rev3 | €29.30 including VAT on the observed regional page; observed August 18, 2026 | Official 5-V Arduino platform | Poor value if a compatible board is already available |
| Adafruit US-100 | $6.95; observed August 18, 2026 | 3–5-V logic, optional UART, and temperature output | Different modes and code |
| Adafruit RCWL-1601 | $3.95; observed August 18, 2026 | 3-V or 5-V HC-SR04-compatible projects | Still subject to ultrasonic reflection limitations |
| Arduino Modulino Distance | €13.10 including VAT; observed August 18, 2026 | Plug-and-play 3.3-V Arduino I2C projects | Requires compatible hardware, cable, and library |
| Arduino URM06 | $77.80; observed August 18, 2026 | Longer-range, UART-based robust applications | Excessive cost and complexity for this beginner use case |
Prices, stock, VAT, shipping, and regional availability can change. The listed prices are time- and region-sensitive observations, not permanent prices or claims that an official vendor offers the lowest price.
Do you need an Arduino HC-SR04 library?
No external library is necessary for the basic method because the sketch can use the built-in Arduino API directly. Arduino also lists a third-party DistanceSensor library for HC-SR04 modules, but a library is optional and can hide the trigger, echo, timeout, and conversion steps that are useful to understand first.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Frequently Asked Questions
What is the correct HC-SR04 wiring for an Arduino Uno?
Connect HC-SR04 VCC to Arduino 5V, GND to GND, TRIG to D9, and ECHO to D10. The pin assignment can be changed, but the sketch constants must match the physical wiring.
Why does the HC-SR04 Arduino sketch return zero?
A zero result means the timeout expired without detecting the requested ECHO pulse. Check power, common ground, TRIG/ECHO orientation, pin numbers, target geometry, practical range, and any voltage-divider wiring on a 3.3-V board.
Is the HC-SR04 waterproof?
No. The standard HC-SR04 is not waterproof and is unsuitable for unprotected rain, splashing, condensation, and many outdoor or tank-level installations.
Can an HC-SR04 ECHO pin connect directly to an ESP32 or other 3.3-V GPIO?
Normally no. The traditional HC-SR04 produces a 5-V ECHO signal, so use a suitable voltage divider or logic-level shifter and confirm the exact GPIO voltage limits.
Recommended Free Tools
Does 40 kHz mean that the HC-SR04 can measure distance 40 times per second?
No. 40 kHz is the frequency of the ultrasonic carrier burst, not the recommended distance-update rate. The HC-SR04 documentation recommends a measurement cycle of more than 60 milliseconds to reduce interference from lingering echoes.
The Bottom Line
The HC-SR04 remains an excellent first ultrasonic sensor for an indoor, 5-V Arduino project: it is inexpensive, simple to wire, and requires no external library. Use the timeout-aware sketch, wait more than 60 ms between measurements, test with a broad perpendicular target, and treat 2–400 cm as a nominal range rather than a universal accuracy promise. For 3.3-V, waterproof, compact I2C, or professional applications, choose a sensor designed for that requirement instead.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

