HC-SR04 Ultrasonic Distance Sensor: The Complete Guide

HC-SR04 Ultrasonic Distance Sensor: The Complete Guide

Introduction

The HC-SR04 ultrasonic distance sensor is one of the most popular and widely used sensors in robotics, automation, and IoT applications. It is a low-cost and efficient way to measure distances by using sound waves. With a measuring range of 2cm to 400cm, high accuracy, and simple interfacing, the HC-SR04 is a go-to choice for hobbyists and engineers alike.

In this detailed blog, we will explore:

  • HC-SR04 specifications
  • How it works
  • Features and capabilities
  • How it compares to other sensors
  • Better or worse alternatives
  • Applications
  • How to use it with Arduino and Raspberry Pi
  • Limitations and considerations

HC-SR04 Specifications

The HC-SR04 ultrasonic sensor has the following specifications:

FeatureSpecification
Operating Voltage5V DC
Operating Current<2mA
Frequency40kHz
Measuring Range2cm – 400cm
Resolution3mm
Measuring Angle15 degrees
Trigger Pulse10µs
Signal OutputDigital Pulse
Response Time<1ms
Interface4 pins (VCC, Trig, Echo, GND)

How the HC-SR04 Works

The HC-SR04 ultrasonic sensor operates based on the principle of sound wave reflection (also called echo ranging). Here’s how it works step by step:

1. Triggering the Sensor

  • The microcontroller sends a 10-microsecond pulse to the Trig pin.

2. Emitting the Ultrasonic Wave

  • The sensor emits an 8-cycle ultrasonic burst at 40kHz through the transmitter.

3. Echo Reception

  • The sound wave travels through the air, and if it hits an object, it bounces back to the receiver.

4. Measuring the Time Delay

  • The sensor records the time taken for the sound to return.

5. Distance Calculation

  • Using the time delay, the distance is calculated using the formula: Distance = (Time × Speed of Sound) / 2 Since the speed of sound is 343 meters per second in air: Distance (cm) = (Time in microseconds × 0.0343) / 2

Features of the HC-SR04

1. High Accuracy

  • The HC-SR04 has an accuracy of 3mm, making it a reliable choice for most distance-sensing applications.

2. Long Range

  • It can measure distances from 2cm to 400cm, making it useful for short and medium-range applications.

3. Low Power Consumption

  • With a current consumption of less than 2mA, it is highly efficient.

4. Simple Digital Output

  • The sensor outputs a digital pulse that can be easily read by microcontrollers like Arduino and Raspberry Pi.

5. Non-Contact Measurement

  • Since it uses sound waves, it is perfect for measuring distances in environments where physical contact is impractical.

Pinout and Wiring

The HC-SR04 has 4 pins:

PinFunction
VCCPower Supply (5V)
TrigTrigger Signal Input
EchoEcho Signal Output
GNDGround

Comparison: HC-SR04 vs Other Distance Sensors

FeatureHC-SR04IR SensorLIDAR Sensor
TechnologyUltrasonic WavesInfrared LightLaser
Range2cm – 400cm2cm – 30cm1m – 40m+
Accuracy±3mmModerateHigh
SpeedFastVery FastFast
Environment SensitivityAffected by temperatureAffected by lightingLess affected
CostLowLowHigh

Better or Worse Alternatives

Better Alternatives

  • VL53L0X Lidar Sensor: More precise, faster response time, better for small-distance applications.
  • TFmini LiDAR: Greater range (up to 12m), better accuracy.

Worse Alternatives

  • IR Sensors: Poor accuracy, limited range.
  • Mechanical Contact Sensors: Requires physical contact.

Applications of HC-SR04

  1. Obstacle Avoidance in Robots
  2. Automatic Parking Systems
  3. Water Level Measurement
  4. Security and Intruder Detection
  5. Smart Traffic Systems
  6. Industrial Automation
  7. Blind Assistance Devices

Using HC-SR04 with Arduino

Wiring

HC-SR04 PinArduino Pin
VCC5V
TrigAny Digital Pin
EchoAny Digital Pin
GNDGND

Sample Code

const int trigPin = 9;
const int echoPin = 10;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  long duration = pulseIn(echoPin, HIGH);
  float distance = (duration * 0.0343) / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(500);
}

Limitations of HC-SR04

  • Sensitive to temperature and humidity changes.
  • Cannot detect soft or sound-absorbing objects.
  • Wide beam angle (not ideal for small object detection).

Conclusion

The HC-SR04 ultrasonic distance sensor is an affordable and effective solution for distance measurement applications. It offers a good balance between cost, range, and accuracy. While it may not be as precise as LiDAR sensors, it is still widely used in robotics, automation, and industrial applications.

If you’re working on a project that requires affordable and reliable distance measurement, the HC-SR04 is a great choice!

Would you like more tutorials on using it with different microcontrollers? Let us know in the comments!

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *