Ultrasonic distance breakout coding

Error in Ultrasonic Distance Sensor code – The UDS is Pimoroni 4TR-ULTRABKT

I have made a robot with a Raspberry Pi 3B, the Cam EduKit, a small I2C Breakout Garden and using gpiozero. I am now attaching the UDS mentioned above.

I am doing the FutureLearn course: Robotics with Raspberry Pi: build and program your first robot buggy.

I have an error which I don’t know how to solve. When I purchased my UDS, it said in the instructions that it used pin 04 for both the trig and the echo.

I only attached one jumper from pin 04 on the breakout garden, which my UDS sits on, to pin 04 on top of my motor controller board.

I put these statements in my program:

trig = OutputDevice(4)
echo = InputDevice(4)

but the code is not working. Here is the error message:

gpiozero.exc.GPIOPinInUse: pin 4 is already in use by <gpio.zero.OutputDevice object on GPIO4, active_high = True, is_active = False>

Not sure how I can remedy this. Here is a photo of my set-up.

The full code is in the file attached: Ruthy_uds-detect-1_script.py
Can’t see how to attach file so I’ll copy and paste into another post.

Full code:

from gpiozero import Robot, InputDevice, OutputDevice
from time import sleep, time

trig = OutputDevice(4)
echo = InputDevice(4)

ruthy = Robot(left=(7, 8), right=(9, 10))

duration = 10
end_time = time() + duration
running = True


def get_pulse_time():

while echo.is_active == False:
    pulse_start = time()

while echo.is_active == True:
    pulse_end = time()

return pulse_end - pulse_start

def calculate_distance(duration):
speed = 343
distance = speed*duration/2
return distance

while running:
duration = get_pulse_time()
distance = calculate_distance(duration)
if distance<0.2:

if time >= end_time:
    running = False

Some indents didn’t come out correctly when I copied it into MS Word but that is not the problem here.

To use code tags here wrap your code in two `. It’s the key with the ~ on it right under the esc key on a US keyboard.
You can use any GPIO you want by the way. 4 was used because that’s what the breakout garden uses. Those breakouts go 3.3V, SDA, SDL, GPIO 4 GND.
Just wire it to a different GPIO not already in use, and change the (4) in your code to match what GPIO you use.

1 Like

Thank you very much K @alphanumeric. I will use 4 and 17 as 17 is not being used later for the line follower.

BTW, on a UK keyboard ` is under the Esc sign as in the US, but has ¬ and another sign on it.

Re: Code tags: I assume you mean that if I wrap the code in two ` , then the indents will behave properly. No need to reply unless I’ve got it wrong. Ah, I wonder if I should put my code in Pastebin? I used that once before and then I get a web link to put here.

I do believe that is the correct key. Yes it saves the indents etc which is important in python.

I put my build pictures and some of my code in my public OneDrive folder. It makes it easy to post link to my pictures. It also gives me a chuckle using Microsoft’s web space for all my Raspberry Pi related stuff. Plus its free and accessible even if my PC is off.
Some forums like the Pi Foundation Forum, have IMHO ridiculous restrictions on image sizes etc for posting. Here its easy peasy you just drag and drop. I do that if I only want to post the one picture. If its a bunch I just post a link to my OneDrive folder and maybe one picture from that folder. I don’t want to abuse a good thing. ;)

1 Like

I haven’t got any further with this ultra distance sensor because when I use one wire to pin 4 it gives the error message in my first post:

and when I use two wires, on pin 4 and pin 17, the UDS doesn’t seem to be reading the echo. I do get a value for the trig reading alright. I wonder if - Is gpiozero set up to have the trig and echo off the same pin? Maybe not. In which case I perhaps have to use a different library such as the RPi.GPIO library instead? That will take me a lot more coding. Any ideas?

You need to use trig and echo on the one Pin, GPIO 17. You don’t want anything from that sensor wired to GPIO 4. You just have to make sure you Python code is set to 17 instead of 4 and it should work.
Be careful with the terms GPIO and Pin.
For most people Pin refers to the physical pin number, for example GPIO 17 is physical pin 11.
When you say GPIO that’s usually taken to mean the BCM number, 17.

Hi K, Thank you for the pinout guide. Yes I’m using the correct pins because I’m using a proper pinout guide that you can place over the pins and I’m using the BCM/GPIO side of it. The 17 I am using is BCM 17, the 11th pin along counting left to right from the top (the SD card end) of my Pi. The 4 I am using is BCM 4, the 7th pin along. It says use GPIO pin 4 in the instructions for the:

so that’s what I’ve been using. I’ll have another go. I may buy a conventional HC-SR04P sensor that just uses 4 jumper leads, to attach to the motor controller, then if I can get that one working I’ll come back to the pretty one, 4TR-UTRABKT, later.

It’s saying use Pin 4 because that’s the Pin the Breakout garden Breakouts use. It’s assuming you have it plugged into the Breakout Garden. You don’t “have” to use that pin though if you don’t want to. You just have to change the example code to the Pin your using. ;) Don’t use GPIO 4 at all for that sensor, if you do your going to get conflicts and errors.

OK thanks. I’ll have another go in two or three hours. I’m sure with your help I’ll get there in the end. I’m learning a lot!

Had no luck again yesterday K.
Have just had an idea. I’ll remove the motor controller from my Pi and put the breakout garden with the UD sensor directly onto it. This won’t test its motor avoidance skills - but will give me a chance to see if my code works and gives suitable trig and echo readings when I place an object in front of the UDS. Then I’ll try it with motors again. BTW, Pimoroni sent me this which will help: https://pinout.xyz/pinout/breakout_garden_hat#

Use GPIO 4 when you have it plugged into the Breakout Garden. That’s the Pin it uses for Interrupt etc on breakouts.

I use that GPIO Pinout site a lot. I have the Mini Breakout Garden which also has an SPI socket. It’s missing from the list of Pimoroni Breakouts though.

I have the version of the Mini Breakout Garden where all three are I2C. I’ll get back to you when/if I make progress. Here’s what I’m trying now. Hope I’ve got it the correct way round on the garden: https://drive.google.com/file/d/1-bz7P0qXkimYbbdqBZfEA3bqS2YvwzUH/view?usp=sharing

Looks correct to me. As far as I know nothing bad happens if you have them in backwards, they just don’t work. That’s what they say for the Pimoroni made Breakout Garden Breakouts, they are reverse voltage protected.

1 Like

Thanks. That’s really helpful to know I can’t fry it by placing it the incorrect way around.

Hi K @alphanumeric ,


John, my husband and I solved the main problem with the UDS I’ve got. Both the trig and echo are on BCM pin 04. But this caused a problem in the coding I’d used. I used the RPi.GPIO library and I set up the sensor with these two statements (shown below) before I sent the pulse to the trigger.

GPIO.setup(pinTrigger, GPIO.OUT)
GPIO.setup(pinEcho, GPIO.IN)

But you can’t setup the echo until the trigger pulse code has happened because it can’t travel out and in on the same pin. When I put the second line (shown above) after the pulse code had happened, the remainder of the program started to work.

We haven’t got everything going perfectly yet but I wanted to report this because it was the main difficulty in the coding - so we have made a lot of progress.

Some of this stuff can really leave you scratching your heed.
It looks like this is what I used for my sensor. Had to go hunting for it, forgot where I had put it, lol.

Thanks K @alphanumeric . We have made great progress since solving this Echo pin problem - but the distances, between the UDS and the wall, are not correct yet. However, we’re sure we can solve that now - so I’m going to close this thread down. I’m going to blog it on https://geekgran.wordpress.com/ when we get it right. Thanks again. Anne

It actually starts to be “fun” when you start to see light at the end of the tunnel. Assuming there is no train whistle blowing in the background, lol. =)

1 Like