How can I get reading from 3 ultrasonic sensors?

I have built a simple robot car with an ultrasonic sensor.

However I wanted 2 more since it can't detect distance when it's driving not straight to a wall. So I added 2 more, but it seems like only one of them are reading distance (and it's not the first one)

#!/usr/bin/python3
import RPi.GPIO as GPIO
import time

try:
while True:
GPIO.setmode(GPIO.BOARD)

PIN_TRIGGER = 7
ECHOS = [11, 13, 15]
for e in ECHOS:
GPIO.setup(e, GPIO.IN)

GPIO.setup(PIN_TRIGGER, GPIO.OUT)

GPIO.output(PIN_TRIGGER, GPIO.LOW)

print ("Waiting for sensor to settle")

time.sleep(2)

print ("Calculating distance")

GPIO.output(PIN_TRIGGER, GPIO.HIGH)

time.sleep(0.00001)

GPIO.output(PIN_TRIGGER, GPIO.LOW)

while GPIO.input(e) == 0:
pulse_start_time = time.time()
while GPIO.input(e) == 1:
pulse_end_time = time.time()

pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
if distance < 30:
print("Object close")
#print ("Distance:",distance,"cm")

finally:
GPIO.cleanup()

All 3 are working if I change the for loop to one of the ECHO numbers (11, 13, 15)

You seem to be trying to read each sensor at the same time. That will not work (did the echo come from the sending sensor or another one).

You need to fire each sensor in succession.

I.e. rather than

trigger A, B, C
get echo A, B, C
calculate distance A, B, C

do

trigger A
get echo A
Calculate distance A

trigger B
get echo B
Calculate distance B

trigger C
get echo C
Calculate distance C

This is pseudo code for what I mean.

echo=[5, 11, 10] # example GPIO for echo lines
trigger=[7, 8, 23] # example GPIO for trigger

for i in range(len(echo)): # for each sensor
send trigger[i]
time echo[i]
calculate distance for sensor i
• Ok Thanks for the quick answer, I thought That I could use a for loop. Thanks for the help May 19 '19 at 15:29
• @Kungbjucha Yes, do use a for loop, but use different trigger and echo GPIO for each sensor. Also as you need to power the sensors from 5V make sure you have a voltage divider to 3V3 on the echo GPIO.
– joan
May 19 '19 at 15:32
• If I understand correctly I shall keep the for loop but add triggers for all sensors ?(they share the same now) May 19 '19 at 17:26

I got it to work now, however I could not get it to work with the for loops so instead I have 3 functions one for each sensor that are all in a While True loop and when one of them detects something it will report it I probably would have looked nicer code wise with for loops so Im gonna see if I can get it to work

Thanks for the help
{"ft_lang_label":"__label__en","ft_lang_prob":0.8448883,"math_prob":0.75086766,"size":1065,"snap":"2022-05-2022-21","text_gpt3_token_len":296,"char_repetition_ratio":0.14043355,"word_repetition_ratio":0.0,"special_character_ratio":0.29577464,"punctuation_ratio":0.20454545,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9824026,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-26T05:28:25Z\",\"WARC-Record-ID\":\"<urn:uuid:42c94177-0dc2-4cec-8358-bff4ceb05b04>\",\"Content-Length\":\"148243\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:90220e28-b1a3-408f-b632-4c4136013473>\",\"WARC-Concurrent-To\":\"<urn:uuid:4e177357-ed3f-4dad-bfa9-029cbabaaad9>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://raspberrypi.stackexchange.com/questions/98746/how-can-i-get-reading-from-3-ultrasonic-sensors\",\"WARC-Payload-Digest\":\"sha1:X5DZ6O4PXEY6PKTUZJI6LQ464C52E45F\",\"WARC-Block-Digest\":\"sha1:R2ICQWWEXJVB3VKBRCTQW6WX4V4OZ37Y\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320304915.53_warc_CC-MAIN-20220126041016-20220126071016-00032.warc.gz\"}"}