Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

Connecting an HC‑SR04 Ultrasonic Sensor to a Raspberry Pi – A Practical Guide

In earlier projects we covered temperature sensors, PIR motion modules, and basic GPIO interfacing. The HC‑SR04 ultrasonic range finder is a popular choice for distance measurement, but its 5 V output must be reduced to 3.3 V to protect the Raspberry Pi’s GPIO pins. This tutorial walks you through the physics, electronics, and Python code required to safely connect and use the sensor.

What You’ll Need

How Ultrasonic Sensors Work

Sound propagates as pressure waves; the pitch is determined by the frequency of these waves. Ultrasonic sensors emit high‑frequency sound that is inaudible to humans, bounce off nearby objects, and then capture the echo. By timing the round‑trip of the pulse, the sensor’s onboard circuitry calculates the distance using the speed of sound (≈343 m/s in air).

HC‑SR04 Pinout and Functionality

The sensor outputs a 5 V pulse on ECHO that lasts from the moment the echo is detected until the sensor is ready for the next trigger. Because a Raspberry Pi GPIO pin only tolerates 3.3 V, we must step down this voltage.

Voltage Divider Circuit

A simple voltage divider uses two resistors in series. With the sensor’s 5 V ECHO as the input, a 1 kΩ resistor (R1) followed by a 2 kΩ resistor (R2) will produce a 3.3 V output at the junction:

Vout = Vin × (R2 / (R1 + R2))

Plug the 1 kΩ resistor between the ECHO line and the Raspberry Pi GPIO, then connect the 2 kΩ resistor from that junction to ground. This keeps the Pi safe while allowing accurate measurement.

Hardware Wiring (Breadboard Method)

Use the following Raspberry Pi pins:

  1. Connect the HC‑SR04 pins to the breadboard: Red (Vcc), Blue (TRIG), Yellow (ECHO), Black (GND).
  2. Tie the breadboard’s positive rail to the Pi’s 5 V and the negative rail to the Pi’s GND.
  3. Wire TRIG to GPIO 23. You may connect it directly or route through a breadboard rail.
  4. For the ECHO line, place the 1 kΩ resistor on the rail from the HC‑SR04’s ECHO pin, then add the 2 kΩ resistor to ground. Connect GPIO 24 to the junction between the two resistors.
  5. Double‑check all connections before powering the Pi.

Python Code to Read Distance

The following script demonstrates how to trigger the sensor and interpret the echo pulse width to calculate distance in centimeters.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24

GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

GPIO.output(TRIG, False)
time.sleep(2)  # Allow sensor to settle

while True:
    # Trigger the sensor: 10 µs high pulse
    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    GPIO.output(TRIG, False)

    # Wait for echo to go high
    pulse_start = time.time()
    while GPIO.input(ECHO) == 0:
        pulse_start = time.time()

    # Wait for echo to go low
    pulse_end = time.time()
    while GPIO.input(ECHO) == 1:
        pulse_end = time.time()

    pulse_duration = pulse_end - pulse_start
    distance = pulse_duration * 17150  # Speed of sound in cm/s
    print("Distance: %0.2f cm" % distance)
    time.sleep(1)

GPIO.cleanup()

Running this script will display real‑time distance readings. Adjust the delay or trigger interval to suit your application.

Safety Tips

By following these steps, you’ll have a robust ultrasonic distance measurement system that’s both safe for your Raspberry Pi and accurate for a wide range of robotics and automation projects.

Manufacturing process

  1. Professional Raspberry Pi Temperature Monitoring with DS18B20
  2. Integrating Sensor Data into a Raspberry Pi: A Hands‑On Guide
  3. Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
  4. Control an LED with a PIR Motion Sensor on Raspberry Pi
  5. Directly Connect a Lego Mindstorms NXT Ultrasonic Sensor to a Raspberry Pi
  6. Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
  7. Integrating the Acconeer A111 Pulsed Radar with a Raspberry Pi: A Practical Guide
  8. Integrating the MAX30100 Pulse Sensor with Arduino: A Step-by-Step Guide
  9. Build an Ultrasonic Range Detector Using Arduino UNO and SR04 Sensor
  10. Mastering the HC‑SR04 Ultrasonic Sensor with Arduino: A Complete Tutorial