5V
5V
15mA
40 kHz
2 cm to 4 meters (0.79 in to 13.12 ft)
3 mm
1 cm
10 s
150 s to 25 ms
-15C to 70C (5F to 158F)
-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.
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.