Stufin
Home Quick Cart Profile

HC-SR-04 Ultrasonic Distance Sensor Module

Buy Now on Stufin

Component Name

HC-SR-04 Ultrasonic Distance Sensor Module

Description

The HC-SR-04 Ultrasonic Distance Sensor Module is a popular and widely used proximity sensor module in the field of robotics, automation, and IoT applications. This module is designed to measure the distance of an object from the sensor using ultrasonic sound waves. It is a non-contact, non-invasive sensor that is capable of detecting objects within a certain range, making it an essential component in various applications such as obstacle avoidance, proximity detection, and distance measurement.

Functionality

The HC-SR-04 Ultrasonic Distance Sensor Module works by transmitting ultrasonic sound waves at a frequency of 40 kHz through the transmitter (TRIG pin). These sound waves bounce off the object and return to the sensor as echoes, which are then received by the receiver (ECHO pin). The time taken by the sound wave to travel to the object and back is directly proportional to the distance of the object from the sensor. The module calculates this time-of-flight and outputs a digital signal indicating the distance of the object.

Key Features

  • High Accuracy: The HC-SR-04 module has a high accuracy of 1 cm, making it suitable for applications requiring precise distance measurements.
  • Wide Range: The module can detect objects within a range of 2 cm to 400 cm (0.8 inches to 157 inches).
  • Simple Interface: The module has a simple 4-pin interface, consisting of VCC, GND, TRIG, and ECHO pins, making it easy to connect to microcontrollers or other electronic devices.
  • Low Power Consumption: The module operates at a low voltage of 5V and draws a current of around 15 mA, making it suitable for battery-powered devices.
  • Compact Design: The module is compact in size, measuring only 43 mm x 20 mm, making it ideal for applications where space is limited.
  • Robust and Reliable: The module is built with robust and reliable components, ensuring a long lifespan and minimizing the risk of failure.

Operating Voltage

5V

Current Consumption

15 mA

Frequency

40 kHz

Range

2 cm to 400 cm (0.8 inches to 157 inches)

Accuracy

1 cm

Response Time

50 ms

Trigger Input Pulse Width

10 s

Output High Level

4.5V

Output Low Level

0.5V

Applications

The HC-SR-04 Ultrasonic Distance Sensor Module is widely used in various applications, including

Obstacle avoidance systems

Proximity detection systems

Distance measurement systems

Robotics and automation

IoT projects

Smart home systems

Industrial automation

Overall, the HC-SR-04 Ultrasonic Distance Sensor Module is a reliable and accurate component for measuring distances, making it an essential component in various applications.

Pin Configuration

  • HC-SR-04 Ultrasonic Distance Sensor Module Pinout
  • The HC-SR-04 Ultrasonic Distance Sensor Module is a popular sensor used for measuring distances in IoT projects. It has four pins, which are explained below:
  • Pin 1: VCC (Power Supply)
  • Function: This pin supplies power to the module.
  • Connection: Connect this pin to a positive voltage source (usually 5V) from a microcontroller or a power supply.
  • Note: Ensure the voltage supply is within the recommended range of 4.5V to 5.5V.
  • Pin 2: Trig (Trigger)
  • Function: This pin is used to trigger the ultrasonic signal transmission.
  • Connection: Connect this pin to a digital output pin of a microcontroller (e.g., Arduino's digital pin).
  • Note: A high pulse (typically 10s) on this pin initiates the measurement process.
  • Pin 3: Echo (Output)
  • Function: This pin provides the output signal indicating the distance measured by the sensor.
  • Connection: Connect this pin to a digital input pin of a microcontroller (e.g., Arduino's digital pin).
  • Note: The pulse width of the output signal is proportional to the distance measured.
  • Pin 4: GND (Ground)
  • Function: This pin provides the ground reference for the module.
  • Connection: Connect this pin to a ground pin of a microcontroller or a power supply.
  • Note: Ensure a solid ground connection to prevent noise and inaccurate readings.
  • Connecting the Pins:
  • 1. Connect the VCC pin to a 5V power source.
  • 2. Connect the Trig pin to a digital output pin of a microcontroller (e.g., Arduino's digital pin).
  • 3. Connect the Echo pin to a digital input pin of a microcontroller (e.g., Arduino's digital pin).
  • 4. Connect the GND pin to a ground pin of a microcontroller or a power supply.
  • Example Connection Diagram:
  • Here's an example connection diagram using an Arduino Uno board:
  • HC-SR-04 VCC -> Arduino 5V
  • HC-SR-04 Trig -> Arduino Digital Pin 2
  • HC-SR-04 Echo -> Arduino Digital Pin 3
  • HC-SR-04 GND -> Arduino GND
  • Note: When connecting the HC-SR-04 module, ensure that the pins are securely connected to prevent loose connections, which can lead to inaccurate readings or module damage.

Code Examples

HC-SR-04 Ultrasonic Distance Sensor Module Documentation
Overview
The HC-SR-04 Ultrasonic Distance Sensor Module is a popular, low-cost sensor used to measure distances using ultrasonic waves. It operates by emitting high-frequency sound waves and measuring the time it takes for the waves to bounce back from an object, allowing for accurate distance measurements.
Pinout
The HC-SR-04 module has four pins:
VCC: +5V power supply
 GND: Ground
 Trig: Trigger input (digital)
 Echo: Echo output (digital)
Operating Principle
1. The Trigger pin is set high for at least 10s to initiate the measurement process.
2. The module sends out an ultrasonic wave (40kHz) from the transmitter and receives the reflected wave using the receiver.
3. The Echo pin outputs a high signal for the duration of the round-trip time of the ultrasonic wave.
4. The distance is calculated by measuring the duration of the high signal on the Echo pin.
Code Examples
### Example 1: Basic Distance Measurement using Arduino
```c++
const int trigPin = 2;  // Trigger pin connected to digital pin 2
const int echoPin = 3;  // Echo pin connected to digital pin 3
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration  0.034) / 2;  // Convert duration to distance (cm)
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(500);
}
```
### Example 2: Distance-based Obstacle Avoidance using Raspberry Pi (Python)
```python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 17
ECHO = 23
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
while True:
    GPIO.output(TRIG, GPIO.HIGH)
    time.sleep(0.00001)
    GPIO.output(TRIG, GPIO.LOW)
while GPIO.input(ECHO) == 0:
        pulse_start = time.time()
while GPIO.input(ECHO) == 1:
        pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
    distance = (pulse_duration  34000) / 2  # Convert duration to distance (cm)
if distance < 20:  # Obstacle detected within 20cm
        print("Obstacle detected!")
    else:
        print("No obstacle detected")
time.sleep(0.5)
```
### Example 3: Distance-based LED Indication using ESP32 (MicroPython)
```python
import machine
import utime
TRIG = machine.Pin(32, machine.Pin.OUT)
ECHO = machine.Pin(33, machine.Pin.IN)
while True:
    TRIG.value(1)
    utime.sleep_us(10)
    TRIG.value(0)
pulse_start = utime.time()
    while ECHO.value() == 0:
        pulse_start = utime.time()
while ECHO.value() == 1:
        pulse_end = utime.time()
pulse_duration = pulse_end - pulse_start
    distance = (pulse_duration  34000) / 2  # Convert duration to distance (cm)
if distance < 20:  # Obstacle detected within 20cm
        machine.Pin(25, machine.Pin.OUT).value(1)  # Turn on LED
    else:
        machine.Pin(25, machine.Pin.OUT).value(0)  # Turn off LED
utime.sleep(0.5)
```
Note: These examples are for illustrative purposes only and may require modifications for specific use cases. Always ensure proper sensor calibration and consideration of environmental factors when using the HC-SR-04 Ultrasonic Distance Sensor Module.