Ultrasonic Sensor (HC-SR04) + Raspberry Pi

Hardware

  • Raspberry pi 3
  • Ultrasonic Sensor(s) – HC-SR04
  • A set of resistors for each sensor you are connecting
    • 330Ω and 470Ω
  • Jumper wires to connect the sensor(s) to the pi
  • Breadboard to connect the sensor(s) to the pi

Wire Setup

Pins

There are four pins (labeled) on the sensor that need to be connected to the pins on the Raspberry pi

  1. VCC to Pin 2 (5v – power)
  2. GND to Pin 6 (ground)
  3. TRIG to Pin 12 (GPIO18)
  4. The 330Ω resistor to ECHO

– On its end, connect it to Pin 18 (GPIO24)- Through a 470Ω resistor, connect it also to Pin6 (ground).- This is done because GPIO pins only tolerate maximal 3.3V

Breadboard

Use the breadboard as a connection between the sensor and the pi as done in this circuit diagram

  • An additional sensor can be connected to the pi by mirroring this exact set up on the other half of the breadboard
    • Connect the VCC and GND to the same pins (2 and 6)
    • Choose any two GPIO pins for the TRIG and the ECHO connection
      • Just be sure to account for the correct GPIO pins in your code

Software

Python


Create a new script

  • Choose Menu → Programming → Click on Python 3 to create a new script
  • The script below will print the distance of the object in front of the sensor at the time that you run the code
  • All variables have a “1” after them because this code can easily be manipulated to add another sensor
    • Simply copy and paste each section of code and rename variables with a “2”
    • Be sure to set up a TRIG2 and ECHO2 to two new GPIO Pins on the pi and mirror the circuit diagram on the other half of the breadboard
import RPi.GPIO as GPIO
import time
 
GPIO.setmode(GPIO.BCM)

TRIG1 = 18
ECHO1 = 24

#print ("Distance Measurement In Process")
GPIO.setup(TRIG1, GPIO.OUT)
GPIO.output(TRIG1, False)

GPIO.setup(ECHO1, GPIO.IN)

#print ("Waiting For Sensor1 To Settle") 
time.sleep(.1)
GPIO.output(TRIG1, True)
time.sleep(0.00001)
GPIO.output(TRIG1, False)

while GPIO.input(ECHO1) == 0:
    pass
    pulse_start1 = time.time()
 
while GPIO.input(ECHO1) == 1:
    pass
    pulse_end1 = time.time()

pulse_duration1 = pulse_end1 - pulse_start1

distance1 = pulse_duration1 * 17150
distance1= round(distance1, 2)
print ("Distance1:",distance1, "cm")

time.sleep(10)

GPIO.cleanup()

Save your script as ultrasonic_distance.py

  • Go to File and click on Save as
  • In the field Save in browse for the C: drive and then select a folder to save in
  • For the field File name type in ultrasonic_distance.py
  • In the field Save as type select All Files
  • Click on Save

Use the terminal to run the script

  • Click on the monitor icon on the top of the screen, this will open the terminal
  • Type cd “folder name” to change directory to your pythonpractice folder, and hit Enter
  • Type ultrasonic_distance.py and hit Enter to run your program

Source: Ultrasonic Sensor (HC-SR04) + Raspberry Pi


About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top