HC-SR04 with Arduino: Step-by-Step Integration Tutorial (South Africa)
Step-by-step HC-SR04 ultrasonic sensor tutorial for Arduino: wiring, formulas, example timings, troubleshooting, and South Africa sourcing tips from Communica.

Wiring & Timing
Trigger with a 10 us pulse, read Echo time, and share ground with Arduino.
Distance Formula
Convert microseconds to cm: distance_cm = duration_us / 58 (adjust for temperature).
Local Project Notes
Check Communica stock and branch pickup to avoid delays in student and maker builds.
Overview: What the HC-SR04 Does
The HC-SR04 ultrasonic sensor measures distance by sending short 40kHz pulses and timing the echo return. This tutorial walks South African makers, students, and technicians through wiring the HC-SR04 to an Arduino, example code, calculation formulas, and real-world tips for reliable readings.
Typical applications
- Obstacle detection for robots and mobile platforms
- Level sensing for tanks or bins
- Proximity alarms and simple rangefinding in projects and labs
HC-SR04 key specs (summary)
| Parameter | Typical Value | Notes |
|---|---|---|
| Operating voltage | 5 V DC | Works directly with 5V Arduino boards |
| Frequency | ~40 kHz | Ultrasonic pulse band |
| Range | 2 cm to ~400 cm | Practical range depends on target and environment |
| Trigger input | TTL pulse | Pulse 10 us minimum |
Parts and local sourcing
You will need an HC-SR04 module, an Arduino (Uno, Nano, or similar), jumper wires, and a breadboard or mounting hardware. Communica stocks ultrasonic sensors, Arduino-compatible boards, prototyping supplies, and accessories; check the Sensors and Prototyping categories to find matching parts and verify stock at Samrand or other branches.
Note: If you are using a 3.3 V microcontroller, use a level shifter on the Echo pin or an Arduino-compatible board that accepts 5 V signals appropriately.
Wiring and simple ASCII diagram
Connect Trigger to a digital output pin and Echo to a digital input. Power the module at 5 V and common-ground with the Arduino.
HC-SR04 Arduino VCC ---> 5V GND ---> GND TRIG ---> D9 (digital out) ECHO ---> D10 (digital in)
If you prefer other pins, any digital pins work; for precise timing you can use pins with interrupt capability where applicable.
Basic code structure (concept)
The code sequence is: 1) send a 10 microsecond pulse on Trigger, 2) measure the time the Echo pin stays HIGH (microseconds), and 3) convert that time to distance using the speed of sound formula below. Use pulseIn or hardware timing for accuracy.
Related Sensors & Semiconductors Products
Discover quality sensors & semiconductors products related to HC-SR04 with Arduino: Step-by-Step Integration Tutorial (South Africa)
Dfrobot Sen0114 Arduino Compatible Soil Moisture Sensor
Dfrobot Sen0114 Arduino Compatible Soil Moisture Sensor
Rfid Contactless Smart Reader & Writer - 13.56mhz Nfc Acr122u
Rfid Contactless Smart Reader & Writer - 13.56mhz Nfc Acr122u
Unassembled Diy Heatsink And Fan Cooler Kit For Use With A Thermoelectric Peltier Module
Unassembled Diy Heatsink And Fan Cooler Kit For Use With A Thermoelectric Peltier Module
Sen0098 Arduino Interface Ac/dc Hall Effect Current Sensor 50a
Sen0098 Arduino Interface Ac/dc Hall Effect Current Sensor 50a
Ir Obstacle Avoidance Sensor Module - 2~4cm Detection Range
Ir Obstacle Avoidance Sensor Module - 2~4cm Detection Range
Am2301 Digital Temperature & Humidity Sensor
Am2301 Digital Temperature & Humidity Sensor
Mini Camera Module For Raspberry Pi - 5mp, 1080p Video, Tiny Board
Mini Camera Module For Raspberry Pi - 5mp, 1080p Video, Tiny Board
Sw-420 Arduino Compatible Digital Vibration Sensor Module Using Lm393 Comparator
Sw-420 Arduino Compatible Digital Vibration Sensor Module Using Lm393 Comparator
Distance calculation and formulas
Ultrasonic distance is based on the round-trip time for sound. The basic formula is:
distance = (time x speed_of_sound) / 2
Using speed of sound ~ 343 m/s at 20 C, and converting microseconds to centimetres gives a practical formula widely used in Arduino examples:
distance_cm = duration_us / 58
Note: speed of sound changes with temperature. For higher accuracy in South African conditions, adjust speed_of_sound using:
v = 331.4 + (0.6 x T) m/s where T is temperature in degrees Celsius. Example: at 30 C, v ~ 349.4 m/s; then distance_cm = duration_us * (v / 2) / 10000.
Example Arduino snippet (conceptual)
A minimal implementation uses digitalWrite to pulse Trigger and pulseIn to read Echo. For classroom use, wrap measurements in averaging to smooth noisy readings. When teaching, demonstrate raw vs averaged outputs to show sensor behaviour.
Practical example: measuring a 100 cm target
Expected round-trip time for 1 m (100 cm) at 20 C: distance one-way = 1.0 m, round-trip = 2.0 m. Time = 2.0 / 343 ~ 0.00583 s = 5830 us. Using distance_cm = duration_us / 58, 5830 / 58 ~ 100.5 cm (close to expected).
Troubleshooting & common issues
- False zeros or very large values: ensure the Trigger pulse is at least 10 us and Echo pin is read correctly; add a short delay between measurements (60 ms) to allow echoes to decay.
- Inconsistent readings: averaging multiple samples (median of 5-10) reduces spikes caused by noise or multi-path reflections.
- Dead zones and minimum range: close objects (<2 cm) may not be detected reliably-check module datasheet for minimum recommended distance.
- Temperature drift: for precise work, read local temperature (e.g., with a DS18B20) and apply the temperature compensation formula above.
- Mounting and angle: the HC-SR04's beam is conical; ensure the target is normal to the sensor face for best returns.
Integration tips for South African projects
- Lead times & stock: check Communica's Sensors and Prototyping pages to confirm stock and branch collection options (Samrand, Pretoria CBD, Cape Town) to avoid delays in student projects or procurement.
- Enclosures and mounting: use simple foam or 3D-printed mounts to reduce vibrations; Communica's enclosures and 3D printing supplies categories have suitable options.
- Power considerations: run the HC-SR04 from a stable 5 V supply; if using battery-powered robots, ensure voltage remains within tolerance to prevent misreads.
- Classroom kits: assemble sensor + Arduino + jumper lead bundles to simplify lab setups and include VAT invoices for tertiary procurement where required.
Advanced notes
For multi-sensor arrays, stagger trigger pulses to avoid cross-talk, or implement time-division so only one sensor emits at a time. For robotics, fuse readings with other sensors (IR, encoders) in software for robust obstacle avoidance.
Sources
Communica supplies semiconductors and electronic components to industries such as automotive, electrical, automation, mining, and education.
Disclaimer: This content is for educational purposes only. Product availability, pricing, and specifications are subject to change. Always verify current details on the retailer's website before making a purchase. We may earn affiliate commissions from qualifying purchases.
Frequently Asked Questions
Get answers to common questions about Sensors And Semiconductors
Why Choose Communica?
*Free Shipping
Free shipping for orders over ZAR 1,000.00.
7-Day Returns
Get a full refund if you had the product for less than 7 days.
Quick Delivery
Nationwide delivery within 2 - 4 working days.
























