Ultrasonic Sensor

What is HC-SR04?
The HC-SR04 is the industry-standard ultrasonic module for beginners, providing non-contact distance measurement for robotics. It uses sound waves to measure distance by emitting a high-frequency sound pulse (40kHz). Perfect for basic Arduino projects.
The sensor works on the principle of Echolocation, similar to how bats and dolphins navigate. It can measure distances from 2cm to 400cm with ยฑ3mm accuracy. Learn more about Arduino Uno to get started.
Key Features
Wide Range
Measures distances from 2cm to 400cm (0.8 inches to 13 feet) with high accuracy.
Low Power
Operates at 5V DC with only 15mA current consumption during operation.
High Accuracy
Provides measurements with ยฑ3mm accuracy, perfect for precise applications.
15ยฐ Detection Angle
Narrow beam angle ensures focused detection without interference from nearby objects.
Fast Response
Measurement cycle time of 50ms allows for real-time distance tracking.
Easy Interface
Simple 4-pin interface works with any microcontroller using digital I/O pins.
Pin Configuration
VCC
5V power supply input. Connect to Arduino 5V pin. Do not use 3.3V as it will reduce range.
Trig (Trigger)
Input pin. Send a 10ฮผs HIGH pulse to start measurement. Connect to any digital pin.
Echo
Output pin. Returns HIGH pulse width proportional to distance. Pulse width = distance in ฮผs / 58.
GND
Ground connection. Connect to Arduino GND pin for common ground reference.
How It Works
The Science Behind It
The Ultrasonic sensor works on the principle of Echolocation, similar to how bats and dolphins navigate. It uses a transducer to send out a high-frequency sound pulse (40kHz) that is inaudible to humans.
The Time of Flight (ToF) principle is used to calculate distance: The sensor triggers a pulse, waits for the echo to return, and calculates the distance based on the speed of sound (approximately 343 m/s at 20ยฐC).
Distance Calculation Formula:
Distance (cm) = (Echo Pulse Width ร Speed of Sound) / 2
Distance (cm) = (Echo Time in ฮผs ร 0.0343) / 2
Distance (cm) = Echo Time in ฮผs / 58Why divide by 2? The sound travels to the object and back, so we divide by 2 to get the one-way distance.
Wiring Tips & Best Practices
Power Supply
Always use 5V power. Lower voltage will reduce range and accuracy significantly.
Wire Length
Keep wires short (< 30cm) to reduce noise and signal degradation in measurements.
Pin Selection
Use any digital pins for Trig and Echo. Avoid pins 0 and 1 (used for Serial).
Voltage Protection
Echo pin outputs 5V. Use voltage divider (2:1) for 3.3V boards like ESP32.
Mounting
Mount sensor perpendicular to target surface for best accuracy and reliability.
Interference
Avoid soft materials (foam, fabric) that absorb sound waves and reduce range.