Stufin
Home Quick Cart Profile

HC-SR04 Ultrasonic Distance Sensor Module (Pack of 20)

Buy Now

Supply Voltage

5V

Current Consumption

15mA

Frequency

40 kHz

Measuring Range

2 cm to 4 meters (0.79 in to 13.12 ft)

Accuracy

3 mm

Resolution

1 cm

Trigger Pulse Width

10 s

Echo Pulse Width

150 s to 25 ms

Operating Temperature

-15C to 70C (5F to 158F)

Storage Temperature

-40C to 100C (-40F to 212F)

Package Contents

20 x HC-SR04 Ultrasonic Distance Sensor Modules

Documentation and datasheet

Applications

The HC-SR04 Ultrasonic Distance Sensor Module is suitable for a wide range of applications, including

Robotics and autonomous systems

Smart home automation and security systems

Industrial automation and monitoring systems

Medical and healthcare devices

Automotive and vehicle systems

IoT and wearable devices

Conclusion

The HC-SR04 Ultrasonic Distance Sensor Module is a reliable and accurate component for distance measurement applications. Its simplicity, low power consumption, and compact design make it an ideal choice for various industries and applications.

Pin Configuration

  • HC-SR04 Ultrasonic Distance Sensor Module Documentation
  • Pinout Explanation:
  • The HC-SR04 Ultrasonic Distance Sensor Module has a total of 4 pins, which are:
  • 1. VCC (Voltage Supply):
  • Function: Provides power to the sensor module.
  • Voltage: Typically 5V, but can operate from 4.5V to 5.5V.
  • Connection: Connect to the positive terminal of the power supply (e.g., Arduino's 5V pin).
  • 2. Trig (Trigger):
  • Function: Initiates the ultrasonic signal transmission.
  • Signal: Digital output, typically pulled high (VCC) to trigger the sensor.
  • Connection: Connect to a digital output pin on your microcontroller (e.g., Arduino's digital pin).
  • 3. Echo (Output):
  • Function: Provides the distance measurement output.
  • Signal: Digital output, typically pulled low (GND) when no object is detected, and pulses high (VCC) when an object is detected.
  • Connection: Connect to a digital input pin on your microcontroller (e.g., Arduino's digital pin).
  • 4. GND (Ground):
  • Function: Provides a common ground reference for the sensor module.
  • Connection: Connect to the negative terminal of the power supply (e.g., Arduino's GND pin).
  • Connection Structure:
  • To connect the HC-SR04 Ultrasonic Distance Sensor Module to your microcontroller (e.g., Arduino):
  • Connect VCC to the positive terminal of the power supply (e.g., Arduino's 5V pin).
  • Connect GND to the negative terminal of the power supply (e.g., Arduino's GND pin).
  • Connect Trig to a digital output pin on your microcontroller (e.g., Arduino's digital pin 2).
  • Connect Echo to a digital input pin on your microcontroller (e.g., Arduino's digital pin 3).
  • Example Connection Diagram:
  • ```
  • HC-SR04 Ultrasonic Distance Sensor Module
  • |
  • |-- VCC --|-----------|-> Arduino 5V pin
  • |
  • |-- Trig --|-----------|-> Arduino Digital Pin 2
  • |
  • |-- Echo --|-----------|-> Arduino Digital Pin 3
  • |
  • |-- GND --|-----------|-> Arduino GND pin
  • ```
  • Note: Make sure to use a breadboard or a PCB to connect the sensor module to your microcontroller, and ensure that the connections are secure and not loose.

Code Examples

HC-SR04 Ultrasonic Distance Sensor Module Documentation
Overview
The HC-SR04 Ultrasonic Distance Sensor Module is a popular and affordable sensor used to measure distances up to 4 meters with high accuracy. It operates on the principle of ultrasonic sound waves and is widely used in robotics, automation, and IoT projects.
Pinout
The module has four pins:
1. VCC: Supply voltage (typically 5V)
2. GND: Ground
3. Trig: Trigger pin (output)
4. Echo: Echo pin (input)
Working Principle
The sensor works by sending out ultrasonic sound waves from the Trig pin and measuring the time it takes for the waves to bounce back from an object and return to the Echo pin. The distance is then calculated based on the speed of sound and the time delay.
Code Examples
### Example 1: Basic Distance Measurement using Arduino
This example demonstrates how to use the HC-SR04 sensor to measure the distance to an object using an Arduino board.
```c
const int trigPin = 2;  // Trigger pin
const int echoPin = 3;  // Echo pin
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop() {
  int duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration  0.034 / 2; // Convert time to distance (cm)
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(1000);
}
```
### Example 2: Obstacle Detection using Raspberry Pi (Python)
This example demonstrates how to use the HC-SR04 sensor to detect obstacles using a Raspberry Pi and Python.
```python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
trig_pin = 17
echo_pin = 23
GPIO.setup(trig_pin, GPIO.OUT)
GPIO.setup(echo_pin, GPIO.IN)
def measure_distance():
    GPIO.output(trig_pin, GPIO.LOW)
    time.sleep(0.1)
    GPIO.output(trig_pin, GPIO.HIGH)
    time.sleep(0.00001)
    GPIO.output(trig_pin, GPIO.LOW)
    start_time = time.time()
    while GPIO.input(echo_pin) == 0:
        start_time = time.time()
    while GPIO.input(echo_pin) == 1:
        stop_time = time.time()
    duration = stop_time - start_time
    distance = duration  34000 / 2  # Convert time to distance (cm)
    return distance
while True:
    distance = measure_distance()
    if distance < 20:  # Define obstacle detection threshold (cm)
        print("Obstacle detected!")
    else:
        print("No obstacle detected.")
    time.sleep(1)
```
### Example 3: Integration with ESP32 Board (C++)
This example demonstrates how to use the HC-SR04 sensor to measure distance and display it on an ESP32 board's serial monitor.
```c
#include <WiFi.h>
const int trigPin = 2;  // Trigger pin
const int echoPin = 4;  // Echo pin
void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop() {
  int duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration  0.034 / 2; // Convert time to distance (cm)
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(1000);
}
```
Note: The above examples are for illustration purposes only and may require modifications to work with your specific setup. Make sure to adjust the pin connections and sensor settings according to your board's specifications.