Documentation

ShillehTek HC-SR04 Ultrasonic Distance Sensor Module 4-Pin for Arduino, Raspberry Pi | ShillehTek Product Manual
Documentation / ShillehTek HC-SR04 Ultrasonic Distance Sensor Module 4-Pin for Arduino, Raspberry Pi | ShillehTek Product Manual

ShillehTek HC-SR04 Ultrasonic Distance Sensor Module 4-Pin for Arduino, Raspberry Pi | ShillehTek Product Manual

manualshillehtek

Overview

The HC-SR04 is a popular ultrasonic distance sensor used in Arduino and Raspberry Pi projects. It measures distance by sending ultrasonic pulses and timing their echo return. With a range of 2-400 cm and operating frequency of 40 kHz, it's an affordable and reliable solution for obstacle detection, distance measurement, and robotics applications.

At a Glance

Operating Voltage
5V DC
Range
2-400 cm
Frequency
40 kHz
Trigger Pulse
10 us
Pin Count
4 pins
Pins
VCC, Trig, Echo, GND

Specifications

Parameter Value
Operating Voltage 5V DC
Operating Current 15 mA
Frequency 40 kHz
Maximum Range 400 cm (13 ft)
Minimum Range 2 cm (0.8 in)
Measuring Angle 15 degrees
Trigger Input Signal 10 us TTL pulse
Echo Output Signal TTL pulse proportional to distance
Dimensions 45 x 20 x 15 mm

Pinout Diagram

HC-SR04 VCC (+5V) Trig (Input) Echo (Output) GND (Ground)

Wiring Guide

Arduino Wiring

The HC-SR04 connects directly to Arduino with standard digital pins. No special voltage conversion is needed because Arduino accepts 5V logic levels.

HC-SR04 Pin Arduino Pin
VCC 5V
Trig Digital Pin 9
Echo Digital Pin 10
GND GND
Warning: The Echo pin outputs 5V logic, which is safe for Arduino but NOT safe for ESP32, Raspberry Pi, or Pico. These platforms use 3.3V GPIO and will be damaged by 5V input. Use a voltage divider for those platforms.

ESP32 Wiring

The ESP32 operates at 3.3V GPIO logic levels. The HC-SR04 Echo pin outputs 5V, so you must use a voltage divider on the Echo line to protect the ESP32.

HC-SR04 Pin ESP32 Pin Details
VCC VIN (5V)
Trig GPIO 5
Echo GPIO 18 Via voltage divider
GND GND
Warning: ESP32 GPIO pins are 3.3V only. You MUST use a voltage divider on the Echo pin. Connect a 1k resistor from Echo to GPIO 18, then a 2k resistor from GPIO 18 to GND. This divides the 5V signal down to approximately 3.3V.
Tip: The ESP32 Trig pin outputs 3.3V, which is enough to trigger the HC-SR04 (minimum trigger voltage is ~2V). No level shifting needed on the Trig line.

Raspberry Pi Wiring

Raspberry Pi GPIO operates at 3.3V, so the 5V Echo output from the HC-SR04 must be reduced using a voltage divider circuit with 1k and 2k resistors.

HC-SR04 Pin Raspberry Pi Pin Details
VCC Pin 2 (5V)
Trig Pin 16 (GPIO 23)
Echo Pin 18 (GPIO 24) Via voltage divider
GND Pin 6 (GND)
Warning: The Echo pin outputs 5V. Raspberry Pi GPIO is 3.3V only. You MUST use a voltage divider on the Echo pin. Connect 1k resistor from Echo to GPIO 24, then 2k resistor from GPIO 24 to GND. This divides 5V down to approximately 3.3V.
Tip: Double-check your pin numbers! Raspberry Pi pin numbering can be confusing. Use the GPIO numbers (GPIO 23, GPIO 24) rather than physical pin positions to avoid mistakes.

Raspberry Pi Pico Wiring

Like Raspberry Pi, the Pico uses 3.3V GPIO. The Echo output must be protected with a voltage divider circuit.

HC-SR04 Pin Pico Pin Details
VCC VSYS (5V)
Trig GP15
Echo GP14 Via voltage divider
GND GND
Warning: Like Raspberry Pi, the Pico GPIO is 3.3V. You must use a voltage divider on the Echo pin. Connect 1k resistor from Echo to GP14, then 2k resistor from GP14 to GND.

Code Examples

Arduino

hc_sr04_arduino.ino
// HC-SR04 Ultrasonic Distance Sensor - Arduino Example
// Trig Pin: 9, Echo Pin: 10

const int trigPin = 9;
const int echoPin = 10;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Send a 10 microsecond pulse to trigger the sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure the duration of the echo pulse
  long duration = pulseIn(echoPin, HIGH);

  // Calculate distance in cm (sound travels 34300 cm/s)
  // distance = duration / 2 / 29.1
  float distance = duration * 0.034 / 2;

  // Print the distance
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);
}

Raspberry Pi (Python)

hc_sr04_rpi.py
#!/usr/bin/env python3
# HC-SR04 Ultrasonic Distance Sensor - Raspberry Pi Example
# Trig Pin: GPIO 23, Echo Pin: GPIO 24

import RPi.GPIO as GPIO
import time

trigPin = 23
echoPin = 24

GPIO.setmode(GPIO.BCM)
GPIO.setup(trigPin, GPIO.OUT)
GPIO.setup(echoPin, GPIO.IN)

try:
    while True:
        # Send trigger pulse
        GPIO.output(trigPin, GPIO.LOW)
        time.sleep(0.00001)
        GPIO.output(trigPin, GPIO.HIGH)
        time.sleep(0.00001)
        GPIO.output(trigPin, GPIO.LOW)

        # Wait for echo to start
        while GPIO.input(echoPin) == 0:
            pulse_start = time.time()

        # Wait for echo to end
        while GPIO.input(echoPin) == 1:
            pulse_end = time.time()

        # Calculate distance
        pulse_duration = pulse_end - pulse_start
        distance = pulse_duration * 17150  # cm

        print("Distance: {:.2f} cm".format(distance))
        time.sleep(0.5)

except KeyboardInterrupt:
    print("Measurement stopped by user")
finally:
    GPIO.cleanup()

Raspberry Pi Pico (MicroPython)

hc_sr04_pico.py
# HC-SR04 Ultrasonic Distance Sensor - Pico MicroPython Example
# Trig Pin: GP15, Echo Pin: GP14

from machine import Pin, time_pulse_us
import time

trigPin = Pin(15, Pin.OUT)
echoPin = Pin(14, Pin.IN)

while True:
    # Send trigger pulse
    trigPin.value(0)
    time.sleep_us(2)
    trigPin.value(1)
    time.sleep_us(10)
    trigPin.value(0)

    # Measure pulse duration
    try:
        pulse_duration = time_pulse_us(echoPin, 1, 30000)
    except OSError:
        print("Timeout waiting for echo")
        continue

    # Calculate distance in cm
    distance = pulse_duration * 0.0343 / 2

    print("Distance: {:.2f} cm".format(distance))
    time.sleep(0.5)

Frequently Asked Questions

How do I wire the HC-SR04 to an Arduino?
Connect VCC to the Arduino 5V pin, Trig to digital pin 9, Echo to digital pin 10, and GND to the ground pin. No voltage divider is needed for Arduino because it accepts 5V logic levels.
What is the distance range of the HC-SR04?
The HC-SR04 can measure distances from 2 cm to 400 cm (about 13 feet). Real-world results depend on the surface texture, measurement angle, and environmental conditions. Hard, flat surfaces at perpendicular angles give the most accurate readings.
Does the HC-SR04 work in bright sunlight or complete darkness?
Yes, the HC-SR04 uses ultrasonic sound waves (40 kHz) for measurements, not light. It works equally well in bright sunlight, darkness, and everything in between. It is not affected by ambient light conditions.
Can the HC-SR04 detect glass, mirrors, or dark surfaces?
Yes. The HC-SR04 detects objects based on ultrasonic echoes, not infrared or light reflectivity. It can detect glass, mirrors, dark surfaces, light surfaces, and most solid materials. It will not work well on soft, porous materials that absorb sound.
Why do I need a voltage divider for ESP32, Raspberry Pi, and Pico but not Arduino?
The HC-SR04 Echo pin outputs 5V logic signals. Arduino operates at 5V and accepts 5V input directly. ESP32, Raspberry Pi, and Pico GPIO operate at 3.3V and will be damaged by 5V input. A voltage divider using 1k and 2k resistors safely reduces the 5V signal to approximately 3.3V for these platforms.
What library or function should I use to read the HC-SR04?
Arduino: Use the built-in pulseIn() function or the NewPing library for simpler code. Python (Raspberry Pi): Use RPi.GPIO or gpiozero libraries with time.time() to measure pulse duration. MicroPython (Pico): Use the machine module with time_pulse_us() to measure the Echo pulse duration.

Related Tutorials