# Obstacle Avoidance

**URL:** <https://forum.sunfounder.com/t/obstacle-avoidance/4965>\
**Category:** Robotic kit for Raspberry Pi\
**Created:** [August 31, 2026, 10:48am UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965 "2026-08-31T10:48:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashishjain](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@ashishjain](https://forum.sunfounder.com/u/ashishjain)\
**Post date:** [August 31, 2026, 10:48am UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965/1 "2026-08-31T10:48:19Z")

</div>

My new raspeberry picar 3b+ was working well until I run sudo python3 4.obstacle\_avoidance.py , it keeps spamming the phrase “distance:-1” and the Picar-x keeps going backwards in a circle continously.

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [September 1, 2026, 10:27am UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965/2 "2026-09-01T10:27:37Z")

</div>

Based on your description, the ultrasonic sensor is continuously outputting “distance:-1”, which usually indicates that the sensor is not reading distances correctly. The most common cause is a wiring issue or poor contact.

Please follow these steps to troubleshoot:

1. **Check and re-plug the ultrasonic sensor** : Make sure the Trig pin of the ultrasonic sensor is connected to the D2 pin on the Robot HAT, and the Echo pin is connected to the D3 pin. We recommend unplugging the sensor’s cable and plugging it back in firmly to ensure good contact.

2. **Send us photos** : Please take a photo of the ultrasonic sensor wiring, as well as a photo of the screen output when running the obstacle avoidance example, and send them to us. This will help us determine whether it’s a wiring issue or a faulty module.

If it still outputs -1 after re-plugging and the wiring is correct, the ultrasonic sensor itself is likely faulty. We will confirm based on the photos you provide, and if it’s a hardware issue, we will send you a replacement ultrasonic module.

---

<div class="post-metadata">

**Author:** ![Xus](https://avatars.discourse-cdn.com/v4/letter/x/59ef9b/32.png) [@Xus](https://forum.sunfounder.com/u/Xus)\
**Post date:** [September 2, 2026, 5:55pm UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965/3 "2026-09-02T17:55:21Z")

</div>

Hola, yo también tengo un problema con el detector electrónico en el Pidog V2. Según las instrucciones de montaje Trig se conecta a pin 4 y Echo al pin 17 al conector DIGITAL me da como resultado lo mismo que a Ashishjain. No detecta nada, he cambiado el sensor y ocurre lo mismo.

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [September 3, 2026, 10:36am UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965/4 "2026-09-03T10:36:29Z")

</div>

We are sorry to hear that your PiDog V2 ultrasonic sensor is not working properly.

Please start by running the standalone ultrasonic test program:

cd ~/pidog/test  
sudo python3 ultrasonic\_test.py  
Before running the test, please confirm that the wiring is correct:

Trig → D4

Echo → D17

After running the test, please send us the complete output and let us know whether it consistently shows distance: -1 or if there are any other errors.

If the issue persists, please also provide photos showing the ultrasonic sensor wiring and the program output.

---

<div class="post-metadata">

**Author:** ![Xus](https://avatars.discourse-cdn.com/v4/letter/x/59ef9b/32.png) [@Xus](https://forum.sunfounder.com/u/Xus)\
**Post date:** [September 7, 2026, 4:33pm UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965/5 "2026-09-07T16:33:40Z")

</div>

Comprobado el cableado,todo bien.

Este es el resultado:

Traceback (most recent call last):  
File “/home/gos/pidog/test/ultrasonic\_test.py”, line 4, in  
ultrasonic = Ultrasonic(Pin(“D1”), Pin(“D0”))  
~~~^^^^^^  
File “/usr/local/lib/python3.13/dist-packages/robot\_hat/pin.py”, line 77, in **init**  
self.setup(mode, pull, active\_state)  
~~~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^^^^^  
File “/usr/local/lib/python3.13/dist-packages/robot\_hat/pin.py”, line 123, in setup  
lgpio.gpio\_claim\_output(Pin.\_chip, self.\_pin\_num, 0, pull\_mode)  
~~~~~~~~~~~~~~~~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
File “/usr/lib/python3/dist-packages/lgpio.py”, line 781, in gpio\_claim\_output  
return \_u2i(\_lgpio.\_gpio\_claim\_output(handle&0xffff, lFlags, gpio, level))  
File “/usr/lib/python3/dist-packages/lgpio.py”, line 458, in \_u2i  
raise error(error\_text(v))  
lgpio.error: ‘GPIO busy’

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [September 8, 2026, 9:27am UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965/6 "2026-09-08T09:27:28Z")

</div>

Regarding your PiCar-4WD issue where the ultrasonic sensor continuously outputs “distance:-1” and the car keeps moving backward in circles, this typically indicates that the sensor is not reading distances correctly. The most common cause is a faulty cable or poor contact.

Please follow these steps to troubleshoot:

1. Replace the ultrasonic sensor’s connecting cable: We recommend replacing it with a new cable directly. A worn or internally broken cable can prevent the signal from transmitting properly, and replacing the cable often resolves this type of issue.

2. If the issue persists, please provide the following information so we can further diagnose:

We will confirm based on the photo and output you provide. If it is a hardware issue, we will handle it further.

Additionally, regarding the PiDog V2 GPIO busy error, we recommend restarting the device and then running the example program again to see if the issue persists.

Finally, we would like to confirm: Are we discussing two different issues for two different products (PiCar and PiDog V2)? Please confirm so we can follow up on each separately.

---

<div class="post-metadata">

**Author:** ![Xus](https://avatars.discourse-cdn.com/v4/letter/x/59ef9b/32.png) [@Xus](https://forum.sunfounder.com/u/Xus)\
**Post date:** [September 10, 2026, 9:32am UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965/7 "2026-09-10T09:32:49Z")

</div>

> [@SunFounder\_Moderator](#):
>
> problema de hardware, lo analizaremos con más detalle.

Mi problema es con PiDog V2 en el sensor ultasonico.

He comprobado el cableado y esta perfecto.

Segue dando el poblema:

gos@PiGos:~/pidog/test $ sudo python3 ultrasonic\_test.py  
Traceback (most recent call last):  
File “/home/gos/pidog/test/ultrasonic\_test.py”, line 4, in  
ultrasonic = Ultrasonic(Pin(“D1”), Pin(“D0”))  
~~~^^^^^^  
File “/usr/local/lib/python3.13/dist-packages/robot\_hat/pin.py”, line 77, in **init**  
self.setup(mode, pull, active\_state)  
~~~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^^^^^  
File “/usr/local/lib/python3.13/dist-packages/robot\_hat/pin.py”, line 123, in setup  
lgpio.gpio\_claim\_output(Pin.\_chip, self.\_pin\_num, 0, pull\_mode)  
~~~~~~~~~~~~~~~~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
File “/usr/lib/python3/dist-packages/lgpio.py”, line 781, in gpio\_claim\_output  
return \_u2i(\_lgpio.\_gpio\_claim\_output(handle&0xffff, lFlags, gpio, level))  
File “/usr/lib/python3/dist-packages/lgpio.py”, line 458, in \_u2i  
raise error(error\_text(v))  
lgpio.error: ‘GPIO busy’  
gos@PiGos:~/pidog/test $ sudo python3 ultrasonic\_iic\_test.py  
Traceback (most recent call last):  
File “/home/gos/pidog/test/ultrasonic\_iic\_test.py”, line 28, in  
bus.write\_byte(address, cmd)  
~~~~~~~~~~~~~~ ^^^^^^^^^^^^^^  
OSError: [Errno 5] Input/output error

He reiniciado varias veces y siempre da el mismo problema.

Gracias

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [September 10, 2026, 10:27am UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965/8 "2026-09-10T10:27:57Z")

</div>

Thank you very much for the detailed information, and we’re sorry for the inconvenience.

First, an important correction: the ultrasonic module that comes with the PiDog V2 uses D1 and D0 as the Trig/Echo pins — it is NOT an I²C module. Therefore ultrasonic\_iic\_test.py does not apply to this module, and the I²C input/output error it produces can be ignored for now — there’s no need to troubleshoot that part.

What we need to focus on is this error:

lgpio.error: ‘GPIO busy’

This error means that one of the GPIO pins used by the ultrasonic test program is already occupied by another program, a system service, or a device-tree configuration. Based on where the error occurs, the program fails while initializing D1, which corresponds to GPIO4.

You have already restarted multiple times but the problem persists, so it is very likely that some Python program or service starts automatically on boot and re-occupies that GPIO. Please run the following commands and send us the complete output:

ps aux | grep python | grep -v grep  
sudo lsof /dev/gpiochip0  
grep -nE “gpio-fan|w1-gpio|gpiopin=4” /boot/firmware/config.txt

If it says lsof is not installed, please run this first:

sudo apt update  
sudo apt install lsof

Also, please close the PiDog APP and any other running PiDog or Python programs first, then run only the correct ultrasonic test:

cd ~/pidog/test  
sudo python3 ultrasonic\_test.py

Please note that this error does not yet prove that the ultrasonic module has a hardware fault — the program stops while trying to occupy the GPIO, before it actually starts reading the ultrasonic module. Once we receive the output of the commands above, we can further identify exactly which program or system configuration is occupying GPIO4 and provide you with the corresponding solution.

Thank you again for your cooperation.

---

<div class="post-metadata">

**Author:** ![Xus](https://avatars.discourse-cdn.com/v4/letter/x/59ef9b/32.png) [@Xus](https://forum.sunfounder.com/u/Xus)\
**Post date:** [September 17, 2026, 9:00am UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965/9 "2026-09-17T09:00:44Z")

</div>

Hola, perdón por no contestar más pronto, pero no he podido.

Quería decir que sigo con el mismo problema. He hecho todo lo que usted me ha dicho pero sigue sin funcionar el detector de ultrasónico del PiDog V2.¿Qué puedo hacer? Gracias.

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [September 17, 2026, 10:06am UTC](https://forum.sunfounder.com/t/obstacle-avoidance/4965/10 "2026-09-17T10:06:34Z")

</div>

Please provide your detailed address and phone number and order number if available to [service@sunfounder.com](mailto:service@sunfounder.com) to get an ultrasonic module.
