connecting ping sensor to raspberry pi

hello friends,
i want to connect multiple(3) ping sensors to the raspberry pi 3B+. anyone has any idea about that ?
i tried for single ping sensor with the following code its working fine , how can i change the code for multiple ping devices to raspberry pi?


import RPi.GPIO as GPIO
import time
try:
GPIO.setmode(GPIO.BOARD)
while(1):
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, False)
time.sleep(0.000002)
GPIO.output(11, True)
time.sleep(0.000005)
GPIO.output(11, False)
GPIO.setup(11, GPIO.IN)

while GPIO.input(11) == 0:
starttime = time.time()

while GPIO.input(11) == 1:
endtime = time.time()

duration = endtime - starttime
distance = duration * 34000 / 2
print distance, "cm"
time.sleep(0.5)

finally:
GPIO.cleanup()

Comments

  • vaibhav,

    Someone asked a similar question here:
    https://raspberrypi.stackexchange.com/questions/60269/how-to-connect-6-ultrasonic-sensors-to-a-raspberry-pi

    I found this for a single Ping.
    https://www.element14.com/community/community/stem-academy/blog/2014/12/21/ping-me

    In an internet search most links for an ultrasonic sensor with the Raspberry Pi and for the HC-SR04 which has an extra pin for Trigger but functions similarly to the Ping.

    Using the Ping is simple since you send a Trigger pulse and then wait for an Echo pulse which corresponds to the distance measured.
    Note the timing on Page 2.
    https://www.parallax.com/sites/default/files/downloads/28015-PING-Sensor-Product-Guide-v2.0.pdf

    I am not familiar with the Raspberry PI but for most microcontrollers the best way to use multiple sensors is one at a time.
    Create a GetDistance subroutine with Ping pin as a parameter and then have it send the Trigger pulse, wait for an Echo pulse, and either return the pulse length or a NoPulse error message.

    In your initialization routine you would set eat Ping pin as an output and leave them in a low (0V or Ground) state.
    Later in program you would call your GetDistance routine by sending the pin number of the Ping that you wanted to check.

    You could up an array variable of Ping pins and use a FOR...NEXT loop to cycle through the array and call GetDistance for each pin.

    You could also create a subroutine called PingSensors or something that will return the distance or error for each sensor back to your main program.
Sign In or Register to comment.