Recommended Free Tools
To use an HC-SR04 with Zephyr, connect its trigger and echo signals to two suitable GPIOs, pulse trigger high for about 10 µs, and measure how long echo stays high. The pulse width gives the round-trip travel time of the sound; divide the sound’s travel distance by two to get the sensor-to-target distance. Choose the GPIOs and timer or edge-capture method for your specific board, and check the electrical specifications of both the sensor module and board before connecting them.
How the HC-SR04 measurement works
The module expects a short trigger pulse. Its timing diagram specifies a 10 µs TTL trigger and an eight-cycle ultrasonic burst. After transmitting the burst, the module raises its echo output for a duration proportional to the range. Your application measures that high interval.
Because the sound travels to the target and back, the measured interval represents a round trip. If t is the echo-high duration in seconds and c is the speed of sound in metres per second, calculate distance as distance = t × c ÷ 2. Use consistent units: for example, with t in seconds and c in metres per second, the result is in metres. The speed of sound varies with conditions, so treat the result as an estimate rather than an exact distance.
Declare the two GPIOs for your board
Zephyr’s GPIO API provides a hardware-agnostic way to configure and operate pins. In a board devicetree overlay, define a node with separate trigger-gpios and echo-gpios properties. A Zephyr source test fixture uses the compatible string hc-sr04 with those two properties, which is a useful example of the node shape. It does not, by itself, establish that your Zephyr build includes a ready-to-use HC-SR04 driver or binding.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
#1 Best Overall
- 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
Choose actual pins using the devicetree and pin-controller conventions for your selected board; there is no universally correct GPIO number or overlay that works across Zephyr boards. Configure the trigger pin as an output and the echo pin as an input. Zephyr’s devicetree-backed gpio_dt_spec objects can represent those pin descriptions for use with the GPIO API.
Before wiring, check the exact HC-SR04 module and development-board electrical specifications, especially the echo signal level and the input limits of the chosen board pin. The available information does not establish a universal voltage-level rule for every module and Zephyr target. Do not assume direct connection is electrically safe.
Rank #2
- 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
Generate the trigger and measure echo
- Set up GPIOs. Obtain the trigger and echo pin descriptions from the board’s devicetree configuration. Configure trigger for output and echo for input, and handle any GPIO configuration errors before starting measurements.
- Start from a known state. Drive trigger low and wait until the sensor and application are ready to begin a measurement.
- Send the trigger pulse. Drive trigger high for about 10 µs, then drive it low. That pulse initiates the module’s ultrasonic burst.
- Capture the echo interval. Detect echo’s rising edge, start a timer or cycle counter, then detect its falling edge and stop the measurement. The elapsed high time is the value used in the distance calculation.
- Convert the interval. Apply
distance = echo_high_time × speed_of_sound ÷ 2, using matching time and distance units. Keep the result’s precision consistent with the timer resolution and the conditions of the measurement. - Handle missing or invalid edges. Use a finite timeout so an absent echo cannot leave the application waiting forever. Report a timeout or invalid reading separately from a measured distance; do not treat a missing pulse as zero distance.
Choose a pulse-capture method
| Approach | When it may fit | Trade-off |
|---|---|---|
| Polling the echo GPIO | A simple application or a target where the expected timing and workload make repeated reads acceptable. | The application spends time checking the pin, and the timing precision depends on how often it checks it. |
| GPIO edge interrupts | An application that should react to echo transitions without continuously polling. | Edge handling and timestamp precision still depend on the target’s interrupt and timing facilities. |
| Timer or cycle-counter capture | Measuring the elapsed high interval with a board-appropriate timing source. | The available facilities and their resolution are target-specific; select and validate them for the board in use. |
Polling is often the most straightforward approach to understand, but a busy-wait loop can occupy the CPU and may miss an edge if the polling interval is too coarse. Interrupt-based edge detection can reduce polling, but it does not remove the need for a suitable timestamp source. Select based on required timing precision, CPU load, and the selected board’s facilities; the GPIO API supplies portable pin operations, not one universal pulse-capture implementation.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Use the Zephyr sensor API only when it fits
For a small application, direct GPIO control and timing may be the simplest way to use this module. If you are building a reusable sensor driver, Zephyr’s sensor subsystem provides sample-fetch, channel-get, and trigger-handler interfaces. A driver can expose a reading through those interfaces, but it still needs a target-appropriate way to generate the trigger and measure echo.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Rank #3
- 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
Zephyr documents sensor trigger handlers as running in a thread context. That can support deferred work, but it does not make the HC-SR04 timing independent of the board’s GPIO and timer behavior. Use the sensor API when integration with other Zephyr sensor consumers is useful; avoid adding that layer merely to perform one application’s measurement.
Quick Recap
Best Value
- 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
Rank #4
- Test mode :Using IO trigger for high level signal.( Not less that 10us),The Module sends eight 40 kHz automatically and detect whether there is a pulse signal back.
- The detection zone: 0.78~196 in/ (2cm~500cm); High precision: up to 0.12 in/(0.3 cm) Effectual angle: less than 15°.
- Power supply: 5V DC; Quiescent current: less than 2mA.
- Test distance = ((Duration of high level)*(Sonic :340m/s))/2.
- Package included: 5 x HC-SR04 Ultrasonic Module.
Diagnose readings that fail or vary
- No rising edge: Check the trigger pulse, GPIO configuration, wiring, and sensor power. Confirm that the chosen pins match the board overlay and that the echo signal can reach the configured input safely.
- Rising edge but no falling edge: Ensure the measurement has a timeout and returns an invalid or timed-out result instead of waiting indefinitely. Check that the echo input is not left in the wrong state by the wiring or configuration.
- Inconsistent distances: Verify that the timer starts at the rising edge and stops at the falling edge, and that both timestamps use the same timer units. Polling too slowly can miss transitions or reduce timing precision.
- Application stalls during measurements: A blocking wait or busy polling loop can consume execution time. Consider edge capture or another board-appropriate timing mechanism, with bounded waiting in either case.
- Unexpected electrical behavior: Disconnect the module and re-check its and the board’s electrical specifications before trying another pin or wiring arrangement. The module name alone does not establish signal compatibility.
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.

