Stufin
Home Quick Cart Profile

HC-SR-04 Ultrasonic Sensor ( Pack of 25)

Buy Now

Supply Voltage

5V

Supply Current

15mA

Frequency

40kHz

Range

2cm to 400cm

Accuracy

1 cm

Resolution

1 cm

Output

Digital (HIGH/LOW)

Operating Temperature

-15C to +70C

Weight

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.

Pin Configuration

  • HC-SR-04 Ultrasonic Sensor Documentation
  • Pinouts and Connection Guide
  • The HC-SR-04 Ultrasonic Sensor is a popular and widely used distance measurement module in IoT and robotics applications. It consists of four pins, which are explained below:
  • Pin 1: VCC (Power Supply)
  • Function: Provides power to the sensor module
  • Description: Connect to a 5V power supply (typically from a microcontroller or a power source)
  • Connection: Connect VCC pin to a 5V power source (e.g., Arduino's 5V pin or a breadboard power rail)
  • Pin 2: TRIG (Trigger Signal)
  • Function: Sends a trigger signal to initiate the ultrasonic pulse
  • Description: This pin is used to trigger the sensor to send out an ultrasonic pulse and measure the distance
  • Connection: Connect TRIG pin to a digital output pin of a microcontroller (e.g., Arduino's digital pin)
  • Pin 3: ECHO (Echo Signal)
  • Function: Receives the echo signal from the ultrasonic pulse
  • Description: This pin outputs a pulse that corresponds to the time it takes for the ultrasonic pulse to bounce back from an object
  • Connection: Connect ECHO pin to a digital input pin of a microcontroller (e.g., Arduino's digital pin)
  • Pin 4: GND (Ground)
  • Function: Provides a ground connection for the sensor module
  • Description: This pin is used to complete the circuit and provide a reference point for the sensor
  • Connection: Connect GND pin to a ground pin of a microcontroller (e.g., Arduino's GND pin) or a breadboard ground rail
  • Connection Structure:
  • To connect the HC-SR-04 Ultrasonic Sensor to a microcontroller (e.g., Arduino), follow this structure:
  • ```
  • HC-SR-04 | Microcontroller (e.g., Arduino)
  • ----------- | -------------------------
  • VCC | 5V Power Source (e.g., Arduino's 5V pin)
  • TRIG | Digital Output Pin (e.g., Arduino's digital pin 2)
  • ECHO | Digital Input Pin (e.g., Arduino's digital pin 3)
  • GND | GND Pin (e.g., Arduino's GND pin)
  • ```
  • Important Notes:
  • Make sure to use a 5V power supply for the HC-SR-04 Ultrasonic Sensor.
  • The TRIG and ECHO pins should be connected to digital pins of the microcontroller that can handle input and output signals.
  • Use a breadboard or a PCB to connect the sensor module to the microcontroller, and ensure proper voltage and ground connections.
  • Refer to the microcontroller's documentation for specific pinouts and programming guides to utilize the HC-SR-04 Ultrasonic Sensor.

Code Examples

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.