If you’ve ever wanted to give your Arduino projects "vision," the HC-SR04 Ultrasonic Sensor is the perfect place to start. Whether you're building an obstacle-avoiding robot or a digital tape measure, this little module is a staple in the maker community.
Today, I’m going to break down how this sensor works and show you how to get it up and running with your Arduino.
What is the HC-SR04?
The HC-SR04 is an affordable sensor that uses sonar to determine the distance to an object—much like a bat or a dolphin. It has a solid detection range of 2 cm to 400 cm (about 13 feet), which is more than enough for most DIY projects.
How it Works: The "Echo" Principle
The module consists of two main parts: an ultrasonic transmitter and a receiver. Here is the simple physics behind it:
- The Trigger: The transmitter sends out a high-frequency sound wave (40 kHz).
- The Bounce: If there is an object in front, the sound wave hits it and bounces back.
- The Echo: The receiver detects the returning wave and records the time it took for the round trip.
- By knowing the speed of sound, we can calculate the distance with a bit of simple math!
By knowing the speed of sound, we can calculate the distance with a bit of simple math!
Pros and Cons
Before we start coding, it’s important to know the sensor's limitations:
- The Good: Unlike infrared sensors, the HC-SR04 isn't bothered by sunlight or dark-colored objects.
- The Bad: It can struggle with soft materials like cloth or wool, which tend to absorb sound waves rather than reflecting them.
- The Variable: Keep in mind that temperature and humidity can slightly change the speed of sound, which might affect your accuracy if you're looking for precision.
Hooking it up to Arduino
To get started, you'll need to connect the four pins: VCC (5V), Trig (Trigger), Echo, and GND.
HC-SR04 Ultrasonic sensor Technical Specification
![]() |
| HC-SR04 |
power supply :- 5DC
Quiescent Current :- 2mA
Working Current :-15mA
Effect Angle :<15 degrees
Distance :- 2cm to 400cm
Resolution :-0.3cm
Trigger input pulse width :-10us
Ultrasonic Frequency :-40KHz
Pins
VCC :- arduino 5v pin
TRIG :- arduino Digital pin
ECHO :- arduino Digital pin
GND :- arduino GND pin
The TRIG pin is used to trigger the transmission of the ultrasonic sound pulse, while the ECHO pin is used to listen for the returning signal.
Example
int triggerPin= 8;int echoPin= 7;
long pulsDuration =0;
long distanceCM =0;
long distanceINC =0;
void setup() {
// put your setup code here, to run once:
pinMode(triggerPin,OUTPUT);
pinMode(echoPin,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(triggerPin,LOW);
delayMicroseconds(5);
digitalWrite(triggerPin,HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin,LOW);
pulsDuration =pulseIn(echoPin,HIGH);
distanceCM = (pulsDuration/2) / 29.1;
distanceINC = (pulsDuration/2) / 74;
Serial.println("CM");
Serial.println(distanceCM);
Serial.println("INCH");
Serial.println(distanceINC);
delay(1000);
Serial.println("**********************************************");
}
