enter image description hereRaspberry pi 3 b+

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.


  • 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.


If i change them to


It doesnt work.. the same sensor that was working with the same code on 7,11

import RPi.GPIO as GPIO

import time


def distance(): GPIO.setmode(GPIO.BOARD)


GPIO.output(GPIO_TRIGGER, False)



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
while True:
    far = distance()
    far = far - 1
    print('Distance = %.lf cm*' %far)

print('Stop sucessfull')
  • 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
    Commented Dec 7, 2018 at 14:57
  • My code works on the 7, 11 pins. If i connect any of the three sensors to these pins i get distance. If i connect the same sensor to the other pins 13,15 OR 16,18. I don't get the Distance from the Sensor. I was just testing initially whether all the sensors work or not
    Commented Dec 7, 2018 at 15:01
  • Yes, but you cannot say your code works until you can make one or more sensors working on the other pins.
    – Dirk
    Commented Dec 7, 2018 at 15:06
  • The two lines you posted just now don't add any info. What if the code uses fixed values somewhere else?
    – Dirk
    Commented Dec 7, 2018 at 15:08
  • The code as posted will never work. Too many problems with it. I can see indentation problems and the except statement will also raise an error. You also should not put setup statements inside a function that is called many times (first 5 lines of distance). 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
    Commented Dec 7, 2018 at 15:42

1 Answer 1


Can you add your circuit diagram? Which sensor are you using? If it is the HC-SR04, the echo pin is rated at 5V and you will need a voltage divider. Not mentioned in your description if you used the divider. The Raspberry Pi GPIO pins are not 5V tolerant and can be damaged. See: https://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi.

Edit to Add: Most likely the problem is due to the fact that the voltage divider as you have it right now is only putting out 1.67 V which is less than the 2V needed.

  • I used the above mentioned schematic. I used the voltage divider to make the voltage 3.3 v
    Commented Dec 7, 2018 at 15:34
  • Thanks. 🤔 Wonder if the Pi is faulty. Do you have a DVM? Maybe you can check the voltages.
    – rprr
    Commented Dec 7, 2018 at 15:45
  • Just noticed that divider is incorrect assuming all resistors are the same.
    – rprr
    Commented Dec 7, 2018 at 15:51
  • Yes i was thinking the same thing. Maybe the PI is faulty. Unfortunately i don't have a DVM handy at the moment. It's just wired and makes no sense, a simple code should work and i just change the pin numbers and connect the sensors pin to them. changing nothing else.
    Commented Dec 7, 2018 at 15:54
  • Oh. that can be the case. I'll check them too
    Commented Dec 7, 2018 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.