This tutorial will show be a quick recap of the steps I have followed to connect three Ultrasonic sensors ( HC-SR04) to the Raspberry Pi to measure distance.
This video is a continuation of my previous video where I have shown how to connect my first ultrasonic sensor. If you haven't watched it I recommend that you do so to follow along.
Here below is the link to my previous videos
One Ultrasonic Sensor to Raspberry Pi Connection
• Raspberry Pi 4: The Ultimate Ultrason...
🤗~ Subscribe & Click "🔔"
/ @raspberrypimadeeasy
Connect two ultrasonic sensors
• Connecting Two Ultrasonic sensors to ...
Connect one ultrasonic sensor
• Raspberry Pi 4: The Ultimate Ultrason...
Equipment you need
Three 1 kilo-Ohm resistor
Three 2 kilo-Ohm resistor
24 Female-Male Jumper Wire
Raspberry Pi 3 or 4
Code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
TRIG =20
ECHO = 21
TRIG1= 23
ECHO1= 24
TRIG2 =
ECHO2 = 33
print "Distance Measurement In Progress"
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.setup(TRIG1,GPIO.OUT)
GPIO.setup(ECHO1,GPIO.IN)
GPIO.setup(TRIG2,GPIO.OUT)
GPIO.setup(ECHO2,GPIO.IN)
GPIO.output(TRIG, False)
GPIO.output(TRIG1, False)
GPIO.output(TRIG2, False)
print "Waiting For Sensor 1 To Send Signal"
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
print "Reading Sensor 1"
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 Sensor 1:" , distance,"cm"
print "Waiting for Sensor 1 to send signal"
time.sleep(2)
GPIO.output(TRIG1, True)
time.sleep(0.00001)
GPIO.output(TRIG1, False)
print "Reading Sensor 2"
while GPIO.input(ECHO1)==0:
pulse_start = time.time()
while GPIO.input(ECHO1)==1:
pulse_end = time.time()
pulse_duration =pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round( distance,2)
print " Distance Sensor 2:" , distance,"cm"
print "Waiting for Sensor 2 to send signal"
time.sleep(2)
GPIO.output(TRIG2, True)
time.sleep(0.00001)
GPIO.output(TRIG2, False)
print "Reading Sensor 3"
while GPIO.input(ECHO2)==0:
pulse_start = time.time()
while GPIO.input(ECHO2)==1:
pulse_end = time.time()
pulse_duration =pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round( distance,2)
print " Distance Sensor 3:" , distance,"cm"
print "Waiting for Sensor 3 to send signal"
GPIO.cleanup()
References
https://thepihut.com/blogs/raspberry-...