Ultrasonic Module not working on the PiDog

I just built the PiDog and everything seems to be working but the ultrasonic module. I tried swapping it out with another I’ve had on hand and tired a different cable. The multimeter shows 3.3V when testing ground so the pins seem to be getting power. I tired using d2 and d3 with the test script. I know these pins work since the touch switch works using them.

The following is the output I get in the terminal:

paulhammond@raspberrypi:~ $ cd ~/pidog/examples
paulhammond@raspberrypi:~/pidog/examples $ sudo python3 4_response.py
config_file: /home/paulhammond/.config/pidog/pidog.conf
robot_hat init … done
imu_sh3001 init … done
rgb_strip init … done
dual_touch init … done
sound_direction init … done
sound_effect init … pinctrl set 20 op dh
done
ultrasonic init … fail
Process sensory_process:
Traceback (most recent call last):
File “/usr/lib/python3/dist-packages/gpiozero/pins/pi.py”, line 411, in pin
pin = self.pins[info]
~~~~~~~~~^^^^^^
KeyError: PinInfo(number=7, name=‘GPIO4’, names=frozenset({‘BOARD7’, ‘BCM4’, 4, ‘WPI7’, ‘J8:7’, ‘GPIO4’, ‘4’}), pull=‘’, row=4, col=1, interfaces=frozenset({‘’, ‘dpi’, ‘spi’, ‘uart’, ‘i2c’, ‘gpio’}))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/local/lib/python3.11/dist-packages/pidog-1.3.9-py3.11.egg/pidog/pidog.py”, line 583, in sensory_process_work
trig = Pin(‘D1’)
^^^^^^^^^
File “/usr/local/lib/python3.11/dist-packages/robot_hat/pin.py”, line 96, in init
self.setup(mode, pull, active_state)
File “/usr/local/lib/python3.11/dist-packages/robot_hat/pin.py”, line 134, in setup
self.gpio = OutputDevice(self._pin_num)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/gpiozero/devices.py”, line 108, in call
self = super().call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/gpiozero/output_devices.py”, line 74, in init
super().init(pin, pin_factory=pin_factory)
File “/usr/lib/python3/dist-packages/gpiozero/mixins.py”, line 75, in init
super().init(*args, **kwargs)
File “/usr/lib/python3/dist-packages/gpiozero/devices.py”, line 553, in init
pin = self.pin_factory.pin(pin)
^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/gpiozero/pins/pi.py”, line 413, in pin
pin = self.pin_class(self, info)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/gpiozero/pins/lgpio.py”, line 126, in init
lgpio.gpio_claim_input(
File “/usr/lib/python3/dist-packages/lgpio.py”, line 755, in gpio_claim_input
return _u2i(_lgpio._gpio_claim_input(handle&0xffff, lFlags, gpio))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/lgpio.py”, line 458, in _u2i
raise error(error_text(v))
lgpio.error: ‘GPIO busy’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/lib/python3.11/multiprocessing/process.py”, line 314, in _bootstrap
self.run()
File “/usr/lib/python3.11/multiprocessing/process.py”, line 108, in run
self._target(*self._args, **self._kwargs)
File “/usr/local/lib/python3.11/dist-packages/pidog-1.3.9-py3.11.egg/pidog/pidog.py”, line 590, in sensory_process_work
raise ValueError(e)
ValueError: ‘GPIO busy’
distance.value: -1.0 cm, touch N
distance.value: -1.0 cm, touch N
distance.value: -1.0 cm, touch N
distance.value: -1.0 cm, touch N

From the error message, the problem mainly lies in the failed initialization of the ultrasonic sensor, which prevents subsequent GPIO operations from proceeding normally.
Ensure that the ultrasonic sensor is properly connected to the GPIO pins of the Raspberry Pi.
Check for loose or damaged connecting wires.
Sometimes the state of the GPIO pins may remain occupied because they were not properly released by the previous program, restarting the Raspberry Pi can release all GPIO pins.

Hi, Thanks for your quick reply. I’ve done all of the above and get the same result. Not sure what to do next. I’m suspecting a hardware issue with the hat.

I have the same indication. Contacted the service they replied nicely with the check cable advice. I also tried with a new cable and another ultrasonic module (same as original) and it does not work. Contacted the service today…let’s see.

We tested the program and it seems to be fine. You mentioned before that you measured GND to get 3.3v, is that the GND of the ultrasonic module? Did you reverse the GND and 3v3 of the cable?
Also if you want to modify the pin test, you need to modify the pin of the touch module or not initialize the touch module, otherwise it will lead to “GPIO busy, in fact, you can run ultrasonic_test. py in ‘~/pidog/test/’ directory, it will only initialize the touch module, but it will not initialize the touch module, it will not initialize the touch module. In fact you can run ultrasonic_test.py in the “~/pidog/test/” directory, which will only initialize the ultrasonic module.

I ran the ultrasonic_test.py in the ~/pidog/test directory and got the error output in the attached document. I noticed there is no ultrasonic.py file in the robot-hat folder (see screenshot).

(Attachment ultrasonic_error.rtf is missing)

(attachments)

Not sure if the attachment came through. Here is the error I get from running ultrasonc_test.py. Also, as mentioned before there is no ultrasonic.py in the robot_hat directory.

%Run ultrasonic_test.py
Traceback (most recent call last):
File “/usr/lib/python3/dist-packages/gpiozero/pins/pi.py”, line 411, in pin
pin = self.pins[info]
KeyError: PinInfo(number=7, name=‘GPIO4’, names=frozenset({‘J8:7’, 4, ‘WPI7’, ‘4’, ‘BCM4’, ‘GPIO4’, ‘BOARD7’}), pull=‘’, row=4, col=1, interfaces=frozenset({‘’, ‘uart’, ‘i2c’, ‘gpio’, ‘dpi’, ‘spi’}))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/paulhammond/pidog/test/ultrasonic_test.py”, line 4, in
ultrasonic = Ultrasonic(Pin(“D1”), Pin(“D0”))
File “/usr/local/lib/python3.11/dist-packages/robot_hat/pin.py”, line 96, in init
self.setup(mode, pull, active_state)
File “/usr/local/lib/python3.11/dist-packages/robot_hat/pin.py”, line 134, in setup
self.gpio = OutputDevice(self._pin_num)
File “/usr/lib/python3/dist-packages/gpiozero/devices.py”, line 108, in call
self = super().call(*args, **kwargs)
File “/usr/lib/python3/dist-packages/gpiozero/output_devices.py”, line 74, in init
super().init(pin, pin_factory=pin_factory)
File “/usr/lib/python3/dist-packages/gpiozero/mixins.py”, line 75, in init
super().init(*args, **kwargs)
File “/usr/lib/python3/dist-packages/gpiozero/devices.py”, line 553, in init
pin = self.pin_factory.pin(pin)
File “/usr/lib/python3/dist-packages/gpiozero/pins/pi.py”, line 413, in pin
pin = self.pin_class(self, info)
File “/usr/lib/python3/dist-packages/gpiozero/pins/lgpio.py”, line 126, in init
lgpio.gpio_claim_input(
File “/usr/lib/python3/dist-packages/lgpio.py”, line 755, in gpio_claim_input
return _u2i(_lgpio._gpio_claim_input(handle&0xffff, lFlags, gpio))
File “/usr/lib/python3/dist-packages/lgpio.py”, line 458, in _u2i
raise error(error_text(v))
lgpio.error: ‘GPIO busy’

Same here. I get exactly the same error with the ultrasonic_test.py in the /test directory. But there is also the ultrasonic init fail message. Also with a new module and cable. Contacted the service. So far no answer.

We are very sorry that we have not reproduced the problem you are experiencing.
Confirm that the hardware module is connected correctly.
What model of Raspberry Pi are you currently using, what system are you using, and what is the system version, please let us know so that we can reproduce the problem.

I have verified all hardware is connected correctly according to the installation instructions. I am using a Raspberry PI 4. I swapped the PI 4 out with another one and still have the same issue. I reloaded the latest version of the Raspberry PI OS onto a new SD card and installed the required modules. As stated before, I tried a new ultrasonic module and cable. I verified both modules work using an Arduino Board. This leaves the robot hat module as the problem since I don’t have another one to test. I measured the voltage from ground to D0 and D1 and get a reading of only .6 volts. You won’t be able to recreate the issue if this is a hardware issue.

Please contact our after-sales email: service@sunfounder.com
We will resend you a new RobotHat.

I discovered what the problem is. I installed the Python3 related packages listed in step 1 in the Installing All Modules section of the Quick Guide. I shouldn’t have done that since that is only for the Lite version OS. Perhaps extra wording could be added to warn people using the full version to skip this step. Anyway, I did a fresh install and now everything works.

I am having the same problem.

Could you share how to actually solve this problem? What did you install instead?

Thanks a lot!

Dear Geoffrey,

May I ask which version of the Raspberry Pi system you’re currently using?

Here’s what you need to know about the installation process:

If you’re using the desktop version of Bookworm (64/32-bit), you don’t need to run this command during library installation:
sudo apt install git python3-pip python3-setuptools python3-smbus

However, if you’re using the lite version of Bookworm (64/32-bit), then you will need to execute the above command.

For detailed instructions, please refer to our tutorial:
PiDog Installation Guide

Let me know if you need any further clarification!

I did a clean install without installing the Python 3 related packages because I was not using the Lite version of Bookworm. If you install it on the full version you’ll have the same problem as I did.

Yes, it worked for me as well. Thank you so much for sharing the solution.

  1. I did a fresh install of the recommended Raspberry Pi OS
  2. I followed all the steps in the “Install all the modules” part of the quick guide, except for the 1st step, do not run: sudo apt install git python3-pip python3-setuptools python3-smbus

Now back to enjoying this amazing robot dog!! :smiley:

Hola a todos, yo tengo el mismo problema con el modulo de ultrasonido. Hice una reinstalacion del S. O. de la raspberry pi(P4 (64btis)de 2Mb RAM), tal como dicen, y no instale el primer paso : sudo apt install git python3-pip python3-setuptools python3-smbus.

Tambien conprobe el modulo ultrasonico, lo cambie por otro, ycambie los cables.

Sigue sin funcionar me da el siguiente fallo:

gos@PiGos:~/robot-hat $ cd ~/pidog/examples
sudo python3 0_calibration.py
config_file: /home/gos/.config/pidog/pidog.conf
robot_hat init … done
imu_sh3001 init … done
rgb_strip init … done
dual_touch init … done
sound_direction init … done
sound_effect init … done
ultrasonic init … fail
Exception in thread sensory_thread:
Traceback (most recent call last):
File “/usr/local/lib/python3.13/dist-packages/pidog/pidog.py”, line 596, in sensory_process_work
trig = Pin(‘D1’)
File “/usr/local/lib/python3.13/dist-packages/robot_hat/pin.py”, line 194, in init
self.setup(mode, pull, active_state)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.13/dist-packages/robot_hat/pin.py”, line 240, 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’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/lib/python3.13/threading.py”, line 1043, in _bootstrap_inner
self.run()
~~~~~~~~^^
File “/usr/lib/python3.13/threading.py”, line 994, in run
self._target(*self._args, **self._kwargs)
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.13/dist-packages/pidog/pidog.py”, line 603, in sensory_process_work
raise ValueError(e)
ValueError: ‘GPIO busy’

¿Que puedo hacer?

Gracias

Thank you very much for providing the complete error logs and for replacing the ultrasonic module and cable — this information helps us a lot in narrowing down the issue.

The lgpio.error: ‘GPIO busy’ in the log means the GPIO pin used by the ultrasonic module is already occupied, so the program cannot claim it. The error occurs during pin initialization, before any distance measurement begins, so we cannot yet conclude that the ultrasonic module is faulty.

The PiDog ultrasonic module uses GPIO17 (D0, Echo) and GPIO4 (D1, Trig). One likely cause is that 1-Wire is enabled on the system, since it uses GPIO4 by default and may conflict with the ultrasonic module. Please follow these steps:

Open the Raspberry Pi configuration tool:
sudo raspi-config
Go to Interface Options → 1-Wire and select No to disable it. Please keep I2C enabled, as PiDog needs it.

Reboot the Raspberry Pi:
sudo reboot

After rebooting, do not start the control App or any other PiDog program — run only the ultrasonic test on its own:
cd ~/pidog/test
sudo python3 ultrasonic_test.py
Please send us the complete terminal output.

If the test works, please run the calibration program as well.

If ‘GPIO busy’ still appears, please send us the complete error from this test, along with the output of the following command, so we can further identify what is occupying the pin:
sudo gpioinfo
If the command is not found, please install it first:
sudo apt install gpiod

In addition, please also confirm the wiring direction of the ultrasonic module (that GND and 3V3 are not reversed), and let us know your Raspberry Pi model, operating system type, and version number, so we can further reproduce and diagnose the issue.

Once we receive the above information, we will help you further determine whether this is a system configuration, wiring, or hardware issue, and follow up accordingly. Thank you again for your patience and cooperation — please feel free to contact us if you have any other questions.

Perdón por no contestar más pronto, no he podido.

He realizado el:

sudo raspi-config y he deshablitado 1-Wire y posteriormente reiniciado,y hecho la prueba, este es el resultado:

gos@PiGos:~/pidog/test $ sudo python3 ultrasonic_test.py
Distance: -1 cm
Distance: 30.08 cm
Distance: 21.84 cm
Distance: -2 cm
Distance: 33.67 cm
Distance: 33.84 cm
Distance: 31.4 cm
Distance: 15.92 cm
Distance: -2 cm
Distance: 8.66 cm
Distance: 10.08 cm
Distance: 11.86 cm
Distance: 24.27 cm
Distance: 29.68 cm
Distance: 33.28 cm
Distance: 33.28 cm
Distance: 36.66 cm

Funciona!!!

Gracias!!!