Ultrasonic Sensor HC-SR04 for IoT: Working, Applications, Circuit, and Integration Guide

Discover how the Ultrasonic Sensor HC-SR04 works and how to use it in IoT projects. This detailed guide covers its working principle, pin configuration, interfacing with Arduino, applications in smart systems, and complete circuit setup. Ideal for beginners and IoT developers looking to integrate distance sensing in real-time projects.

Jul 15, 2025
Sensors
647 words
Advertisement
Article Top Ad (728x90)
Ultrasonic Sensor HC-SR04 for IoT: Working, Applications, Circuit, and Integration Guide

Device Overview

Introduction

Ultrasonic sensors have become a key component in modern electronics and Internet of Things (IoT) applications. Among them, the HC-SR04 Ultrasonic Sensor is one of the most popular and affordable distance-measuring devices. It uses high-frequency sound waves to detect the distance between objects and is widely used in robotics, automation, water level monitoring, parking sensors, and more.

This guide provides complete insights into the Ultrasonic Sensor HC-SR04, including its working principle, specifications, interfacing with Arduino, applications, advantages, and integration tips for IoT projects.

What is Ultrasonic Sensor HC-SR04?

The HC-SR04 is an ultrasonic distance measuring sensor that calculates the distance to an object by sending and receiving sound waves. It uses the time-of-flight method to determine how long it takes for the sound to reflect back from a surface.

Working Principle

HC-SR04 works based on ultrasonic echolocation — similar to how bats and dolphins navigate.

  1. The Trigger pin sends an ultrasonic pulse (40kHz).

  2. This pulse travels through the air and hits an object.
  3. The reflected signal is captured by the Echo pin.

  4. The sensor calculates distance using the formula:
Distance = (Time × Speed of Sound) / 2
Speed of sound = ~343 m/s in air

Technical Specifications

Parameter Value
Model HC-SR04
Operating Voltage 5V DC
Operating Current 15mA
Frequency 40 kHz
Measuring Range 2 cm to 400 cm (4 meters)
Accuracy ±3 mm
Measuring Angle ≤ 15°
Trigger Input Signal 10µs TTL pulse
Echo Output Signal TTL pulse (width = distance)
Module Size 45mm × 20mm × 15mm
 

Pin Configuration

Pin Name Description
1 VCC +5V Power Supply
2 Trig Trigger Input (10µs pulse)
3 Echo Echo Output (pulse width)
4 GND Ground
 

Interfacing HC-SR04 with Arduino

Required Components:

  • 1x Ultrasonic Sensor HC-SR04
  • 1x Arduino Uno / Nano / Mega
  • Jumper wires
  • Breadboard (optional)

Circuit Diagram:

HC-SR04 Arduino
-------- -------
VCC → 5V
GND → GND
Trig → Digital Pin 9
Echo → Digital Pin 10

Sample Arduino Code

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

voidloop() {
    longduration;
    intdistance;

    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
   
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    duration=pulseIn(echoPin, HIGH);
    distance=duration*0.034/2;

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

    delay(500);
}

Output:

The distance (in cm) of the object from the sensor will be displayed in the Serial Monitor.

Applications of HC-SR04 Ultrasonic Sensor

  • Water level measurement
  • Smart dustbins
  • Parking assistance system
  • Obstacle detection in robotics
  • Industrial automation
  • IoT-based security systems
  • Blind assistive devices
  • Smart agriculture (e.g., irrigation level detection)
  • Home automation

Advantages of Using HC-SR04

  • Low cost and easily available
  • High accuracy (±3 mm)
  • Easy to interface with microcontrollers
  • Reliable and consistent output
  • Works in dark or non-visual environments

Limitations

  • Can be affected by temperature and humidity
  • Works best with flat, smooth surfaces
  • Limited angle (15° cone)
  • Not suitable for transparent or absorbent objects

Types of Ultrasonic Sensors

Although HC-SR04 is the most popular, here are other types used in IoT:
Sensor Name Features
HC-SR04 Standard, low-cost, most used
JSN-SR04T Waterproof, suitable for outdoor projects
MB1020 (MaxBotix) High precision, industrial grade
HY-SRF05 Enhanced range and accuracy over HC-SR04
 

Integration Tips for IoT Projects

  • Always add a small capacitor across VCC and GND to eliminate noise.
  • Use software or hardware filters to reduce false signals.
  • For long-range accuracy, prefer waterproof sensors like JSN-SR04T.
  • Combine with NodeMCU/ESP32 for WiFi-enabled IoT setups.
  • Ensure echo pin timing is handled properly to avoid incorrect distance readings.

Conclusion

The Ultrasonic Sensor HC-SR04 is an ideal choice for distance measurement in various DIY and professional IoT applications. With simple interfacing, accurate output, and affordable pricing, it remains a top pick among makers and engineers. Whether you’re building a smart water level detector or an obstacle-avoiding robot, the HC-SR04 is a reliable and efficient sensor to integrate.

 
 
Sponsored Content
In-Content Ad (300x250)

Where to Buy

Platform Price Action
Amazon
₹279 Buy Now

Prices may vary. Click on "Buy Now" to check current availability and pricing.

Sensors IoT Device Verified
Written by

Administrator

IoT Expert
Verified Author
Advertisement
Ad Banner
(300x250)
Advertisement
Ad Banner (728x90)

Frequently Asked Questions

Common questions about Ultrasonic Sensor HC-SR04 for IoT: Working, Applications, Circuit, and Integration Guide. Find answers to the most frequently asked questions.

The HC-SR04 is an ultrasonic distance measurement sensor that uses sound waves to detect objects. It sends out a 40kHz ultrasonic pulse and listens for the echo to calculate the distance to an object. In IoT projects, it's commonly used for obstacle detection, water level monitoring, and smart automation due to its accuracy and ease of interfacing with microcontrollers like Arduino, ESP8266, and ESP32.
The HC-SR04 can measure distances from 2 cm to 400 cm (4 meters) with an accuracy of approximately ±3 mm. It performs best on flat, solid surfaces and in indoor environments with minimal echo interference. For outdoor or waterproof applications, consider alternatives like JSN-SR04T.
Yes, the HC-SR04 sensor can be used with NodeMCU (ESP8266) or ESP32 microcontrollers. However, since these boards operate on 3.3V logic, you should use a logic level converter or a resistor voltage divider to protect the Echo pin. This integration allows real-time distance data to be transmitted over Wi-Fi for smart IoT applications.
No, the HC-SR04 is not waterproof and should only be used in dry indoor environments. If you need an ultrasonic sensor for outdoor or moisture-prone areas, the JSN-SR04T is a better choice as it comes with a waterproof probe and more robust casing suitable for harsh conditions.
One of the main challenges with the HC-SR04 is its sensitivity to environmental conditions such as temperature, humidity, and surface texture, which may slightly alter the accuracy. It also performs best when the target object is flat and directly in front of the sensor, as angled or irregular surfaces may deflect the ultrasonic waves and cause unstable readings, especially in open environments.
The HC-SR04 operates on a 5V power supply and uses TTL logic for its signal pins, making it fully compatible with 5V microcontrollers like Arduino Uno and Mega. However, when connecting it to 3.3V boards like NodeMCU or ESP32, a logic level shifter or resistor divider should be used on the echo pin to prevent potential damage, ensuring safe and stable performance in such configurations.

User Reviews & Comments

Share your experience with this IoT device. Your feedback helps our community make informed decisions!

1 review

Share Your Experience

Help others by sharing your thoughts about this IoT device.

0/1000 characters
Bavaliya Nayan

Bavaliya Nayan

1 week ago

Super 😍 bro it's working 💪

Related Devices

Explore more IoT devices in the same category

No Related Devices

Check out other categories for more IoT devices.

Advertisement
Ad Banner (728x90)
Advertisement
Footer Ad (728x90)