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)