I'm trying to make an Obstacle avoidance obstacles.
It's a very simple project. I tried with one ultra sonic sensor, it worked. Gave me the distance which i could further manipulate to avoid distance.
It wasn't very good, it collided a lot specially when it came at an angle to the wall or an object.
I then tried to mesh a network of Sensors (3 Ultra sonic sensor and 2 IR sensors) to work in combination for better obstacle avoidance. Seemed simple enough as well.
But I'm not able to run the other two ultra sonic sensors. Which is weird considering i set them up exactly the same.
I ran the same code just changing the GPIO pin number's that i have connected the ECHO and TRIG pin to.
the working pins were (GPIO.BOARD) 7, 11
I connected the TRIG and ECHO pins on 13,15 && 16,18
They don't seem to work. If i connect any sensors's pin to 7,11 they work. Other than these two pins I'm getting no response. I tried all 3 Sensors and the all work at 7,11 pins.
SO,
- The sensors work
- The connections are correct (VCC, GND)
- The code works
Need help. I have this feeling that the solution would be something very simple.
GPIO_TRIGGER = 7
GPIO_ECHO = 11
If i change them to
GPIO_TRIGGER = 13
GPIO_ECHO = 15
It doesnt work.. the same sensor that was working with the same code on 7,11
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
def distance(): GPIO.setmode(GPIO.BOARD)
GPIO_TRIGGER = 13
GPIO_ECHO = 15
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.output(GPIO_TRIGGER, False)
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
starttime = time.time()
stoptime = time.time()
while GPIO.input(GPIO_ECHO) == 0:
starttime = time.time()
while GPIO.input(GPIO_ECHO) == 1:
stoptime = time.time()
timeElapsed = stoptime - starttime
distance = (timeElapsed * 34300)/2
return distance
try:
while True:
far = distance()
far = far - 1
print('Distance = %.lf cm*' %far)
except:
KeyboardInterrupt
print('Stop sucessfull')
GPIO.cleanup()
The code works
how do you know? Does your code work work when you connect a single sensor to any of the alternative pin combinations? Maybe you can post (minimal) code that shows the problem. – Dirk Dec 7 '18 at 14:57except
statement will also raise an error. You also should not put setup statements inside a function that is called many times (first 5 lines ofdistance
). Using the same name for a function and a variable could also cause problems. I suggest you clean up your code first and then try again. – Dirk Dec 7 '18 at 15:42