HC-SR04 Rangefinder

How to setup a HC-SR04 sensor with the Raspberry Pi.

PARTS:
RPI 3 – https://amzn.to/2VA9pQY
4 Amp Power Adapter – https://amzn.to/2CTptWu
16GB micro SD – https://amzn.to/2SFMwd3
120 pcs jumper cable: https://ebay.to/2VAb9cY
HC-SR04 Sensors – https://amzn.to/2Ryw0hH
Resistor Assortment – https://amzn.to/2CV8vHa
HC-SR04 Brackets (optional) – https://amzn.to/2C6iwzY

SCHEMATIC:

CODE:

import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)

def ping():
	"""Get reading from HC-SR04"""
	GPIO.setmode(GPIO.BCM)
	 
	TRIG = 23 
	ECHO = 18
	 
	GPIO.setup(TRIG,GPIO.OUT)
	GPIO.setup(ECHO,GPIO.IN)
	 
	GPIO.output(TRIG, False)
	time.sleep(1)
	 
	GPIO.output(TRIG, True)
	time.sleep(0.00001)
	GPIO.output(TRIG, False)
	 
	while GPIO.input(ECHO)==0:
	  pulse_start = time.time()
	 
	while GPIO.input(ECHO)==1:
	  pulse_end = time.time()
	 
	pulse_duration = pulse_end - pulse_start
	 
	distance = pulse_duration * 17150
	 
	distance = round(distance, 2)
	 
	print "Distance:",distance,"cm"
	 
	GPIO.cleanup()

print "Reading Distance \n"

while True:
	ping()