How to Connect an Ultrasonic Sensor to a Raspberry Pi

Ben
Ben
@benjislab

Ultrasonic sensors are incredibly useful in Raspberry Pi projects for measuring distances or detecting objects. These sensors work by emitting sound waves at ultrasonic frequencies and measuring the time it takes for the echo to return. In this tutorial, we’ll cover how to connect an HC-SR04 ultrasonic sensor, a popular model, to a Raspberry Pi and write a simple Python script to read distances.

Equipment Needed

  • Raspberry Pi (any model with GPIO pins)
  • HC-SR04 Ultrasonic Sensor
  • 1kΩ and 2kΩ resistors
  • Breadboard and jumper wires
  • GPIO header or adapter

Wiring the Sensor

  1. Power Connections:

    • Connect the VCC pin of the ultrasonic sensor to a 5V pin on the Raspberry Pi.
    • Connect the GND pin of the sensor to one of the GND pins on the Raspberry Pi.
  2. Data Connections:

    • The HC-SR04 sensor has a trigger pin and an echo pin. The trigger pin is used to send the ultrasonic pulse, and the echo pin receives the pulse after it bounces off an object.
    • Connect the trigger pin of the sensor to a GPIO pin on the Raspberry Pi (for example, GPIO 23).
    • The echo pin must be connected through a voltage divider because it operates at 5V, while the Raspberry Pi’s GPIO operates at 3.3V. Connect the echo pin to a 1kΩ resistor. Connect another 2kΩ resistor from the other side of the 1kΩ resistor to GND. Connect the point between the two resistors to a GPIO pin on the Raspberry Pi (for example, GPIO 24).

Programming the Raspberry Pi

  1. Set Up Your Raspberry Pi:

    • If not already done, set up your Raspberry Pi with Raspberry Pi OS and ensure Python 3 is installed.
  2. Create a Python Script:

    • Open a terminal and create a new Python file:
nano ultrasonic_distance.py
  • Enter the following Python script:
import  RPi.GPIO  as  GPIO  import  time GPIO.setmode(GPIO.BCM) TRIG =  23  ECHO =  24GPIO.setup(TRIG, GPIO.OUT) GPIO.setup(ECHO, GPIO.IN) GPIO.output(TRIG,  False) time.sleep(2)  def  get_distance(): GPIO.output(TRIG,  True) time.sleep(0.00001) GPIO.output(TRIG,  False)  while  GPIO.input(ECHO) ==  0: start_time = time.time()  whileGPIO.input(ECHO) ==  1: end_time = time.time() duration = end_time - start_time distance = duration *  17150  distance =  round(distance,  2)  return  distance  try:  whileTrue: dist = get_distance()  print("Distance:", dist,  "cm") time.sleep(1)  exceptKeyboardInterrupt: GPIO.cleanup()
  • Save and close the file by pressing CTRL+X, then Y, and finally Enter.

Step 3: Running Your Script

  • In the terminal, run your script:
python3 ultrasonic_distance.py
  • You should see the distance measurements displayed in the terminal. Adjust the sensor’s orientation or the objects in front of it to see how the readings change.

Conclusion

Connecting an ultrasonic sensor to a Raspberry Pi opens up a range of possibilities for DIY projects, from robotics to home automation systems. With the basic setup and script provided in this guide, you can start integrating distance measurement into your Raspberry Pi projects. Always ensure your connections are secure and your code is properly debugged for best results.