5V
5V
15mA
40kHz
2cm to 400cm
1 cm
1 cm
Digital (HIGH/LOW)
-15C to +70C
20g
Applications
The HC-SR-04 Ultrasonic Sensor is suitable for a wide range of applications, including |
Robotics and automation
IoT projects
Smart home systems
Industrial automation
Medical devices
Security systems
Proximity detection systems
Packaging and Availability
The HC-SR-04 Ultrasonic Sensor is available in a pack of 25 sensors, providing an economical solution for projects that require multiple sensors. Each sensor is individually packaged and comes with a set of pins for easy connection to your project.
HC-SR-04 Ultrasonic Sensor Documentation
Overview
The HC-SR-04 Ultrasonic Sensor is a popular and widely used proximity sensor in robotics and IoT projects. It provides a non-invasive way to measure distances up to 4 meters with an accuracy of 1 cm. This sensor is a cost-effective and easy-to-use solution for obstacle detection, distance measurement, and object tracking.
Pinout
The HC-SR-04 Ultrasonic Sensor has four pins:
1. VCC (Red wire): Power supply pin, typically connected to 5V.
2. GND (Black wire): Ground pin, connected to the ground of the microcontroller or power supply.
3. TRIG (Yellow wire): Trigger pin, used to send an ultrasonic pulse.
4. ECHO (Green wire): Echo pin, receives the reflected ultrasonic pulse.
Working Principle
The HC-SR-04 Ultrasonic Sensor works by sending an ultrasonic pulse through the TRIG pin and measuring the time it takes for the pulse to bounce back and return to the ECHO pin. The distance is calculated based on the speed of sound and the time delay between the sent and received pulses.
Code Examples
### Example 1: Basic Distance Measurement using Arduino
This example demonstrates how to use the HC-SR-04 Ultrasonic Sensor to measure the distance to an object using an Arduino board.
```cpp
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;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(50);
}
```
### Example 2: Object Detection using Raspberry Pi and Python
This example demonstrates how to use the HC-SR-04 Ultrasonic Sensor to detect objects using a Raspberry Pi and Python.
```python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
trigPin = 17
echoPin = 23
GPIO.setup(trigPin, GPIO.OUT)
GPIO.setup(echoPin, GPIO.IN)
def distance Measurement():
GPIO.output(trigPin, False)
time.sleep(0.01)
GPIO.output(trigPin, True)
time.sleep(0.00001)
GPIO.output(trigPin, False)
start_time = time.time()
while GPIO.input(echoPin) == 0:
start_time = time.time()
while GPIO.input(echoPin) == 1:
end_time = time.time()
duration = end_time - start_time
distance = duration 17000
return distance
try:
while True:
dist = distanceMeasurement()
if dist < 20:
print("Object detected!")
else:
print("No object detected.")
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()
```
### Example 3: Obstacle Avoidance using ESP32 and MicroPython
This example demonstrates how to use the HC-SR-04 Ultrasonic Sensor to detect obstacles and control a robot using an ESP32 board and MicroPython.
```python
import machine
import utime
trigPin = machine.Pin(18, machine.Pin.OUT)
echoPin = machine.Pin(19, machine.Pin.IN)
def distanceMeasurement():
trigPin.value(0)
utime.sleep_ms(2)
trigPin.value(1)
utime.sleep_us(10)
trigPin.value(0)
start_time = utime.ticks_us()
while echoPin.value() == 0:
start_time = utime.ticks_us()
while echoPin.value() == 1:
end_time = utime.ticks_us()
duration = utime.ticks_diff(end_time, start_time)
distance = duration 0.034 / 2
return distance
while True:
dist = distanceMeasurement()
if dist < 20:
print("Obstacle detected! Stopping motor.")
# Stop motor
else:
print("No obstacle detected. Moving forward.")
# Move motor forward
utime.sleep(0.5)
```
These examples demonstrate the basic usage of the HC-SR-04 Ultrasonic Sensor in various contexts. The sensor can be used in a wide range of applications, including robotics, home automation, and industrial control systems.