HC-SR-04 Ultrasonic Distance Sensor Module
HC-SR-04 Ultrasonic Distance Sensor Module
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.
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.
5V
15 mA
40 kHz
2 cm to 400 cm (0.8 inches to 157 inches)
1 cm
50 ms
10 s
4.5V
0.5V
| 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.
HC-SR-04 Ultrasonic Distance Sensor Module DocumentationOverviewThe 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.PinoutThe HC-SR-04 module has four pins:VCC: +5V power supply
GND: Ground
Trig: Trigger input (digital)
Echo: Echo output (digital)Operating Principle1. 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 3void 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 timeGPIO.setmode(GPIO.BCM)TRIG = 17
ECHO = 23GPIO.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 utimeTRIG = 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 LEDutime.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.