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
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, False)
GPIO.output(11, True)
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"



  • vaibhav,

    Someone asked a similar question here:

    I found this for a single Ping.

    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.

    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.