0

I am doing a project with raspberry pi. In it, i use two ultrasonic sensors and two buzzers. When one sensor finds an obstacle, within 50 - 450cm range sensor will beep and print the distance to the obstacle every time if it gets echo. When I do it with one sensor, it is running fine. But when I try to run it with two sensors using the same way, it gives error. I am giving the the code with one sensor which worked. TRIG1 and ECHO1 are of the sensor I use. Please give me code to do it with both sensors.

import time
import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

TRIG=15
ECHO=21
TRIG1=12
ECHO1=13
print"Ultrasonic measurement"
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.setup(TRIG1,GPIO.OUT)
GPIO.setup(ECHO1,GPIO.IN)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)

GPIO.output(TRIG,False)
GPIO.output(TRIG1,False)
time.sleep(0.5)
while True:

              GPIO.output(TRIG1,True)
              time.sleep(0.00004)

              GPIO.output(TRIG1,False)
              while GPIO.input(ECHO1)==0:
                            three=time.time()




              while GPIO.input(ECHO1)==1 or time.time()==three+0.05000000:
                            two=time.time()



              one=two-three


              distance1 = one*17150

              print distance1



              if distance1 > 50 and distance1 < 450:
                            GPIO.output(24,True)
              else:
                            GPIO.output(24,False)


GPIO.cleanup()

I am giving code which gave me error and the error was "elapsed = stop-start. start is not defined"

import time
import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

TRIG=15
ECHO=21
TRIG1=12
ECHO1=13
print"Ultrasonic measurement"
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.setup(TRIG1,GPIO.OUT)
GPIO.setup(ECHO1,GPIO.IN)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)

GPIO.output(TRIG,False)
GPIO.output(TRIG1,False)
time.sleep(0.5)
while True:
              GPIO.output(TRIG,True)
              GPIO.output(TRIG1,True)
              time.sleep(0.00004)
              GPIO.output(TRIG,False)
              GPIO.output(TRIG1,False)
              while GPIO.input(ECHO1)==0:
                            three=time.time()
              while GPIO.input(ECHO)==0:
                            start=time.time()


              while GPIO.input(ECHO)==1 or time.time()==start+0.0500000:
                            stop=time.time()
              while GPIO.input(ECHO1)==1 or time.time()==three+0.0500000:
                            two=time.time()

              elapsed = stop-start

              one=two-three

              distance = elapsed*17150
              distance1 = elapsed1*17150
              print  distance
              print  distance1

              if distance > 50 and distance < 450:
                            GPIO.output(3,True)
              else:
                            GPIO.output(3,False)

              if distance1 > 50 and distance1 < 450:
                            GPIO.output(24,True)
              else:
                            GPIO.output(24,False)

GPIO.cleanup()
  • You will want a loop to detect both echos rather than detect ECHO1 and THEN ECHO2. You will most likely be able to do this with flags and expressions such as: while(echo1NotStarted && echo2NotStarted) { if (ECHO1==HIGH) { echo1NotStarted = false; } if (ECHO2==HIGH) { echo2NotStarted = false; } } – Kolban Dec 19 '15 at 10:34
  • There is a python library developed especially for this sensor called hcsr04sensor . There are examples and it is very easy to use. – Tawan Thampipattanakul Dec 12 '17 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.