How to use multiple HC-SR04 distance sensors

For a project I need to use many distance sensors HC-SR04 using less pins possible of a Pi Zero W. I was able to do a test program in python3 with one sensor connected using `gpiozero` library and it worked. Then, I wanted to use a multiplexer 5v with two sensors. I used a Toshiba TC74HC4051AP. This is the connection that I have used:

• VCC from pin 2 and GND from pin 9 to connect the multiplexer and two sensors;
• pin 11 (gpio 17) to connect the trigger in common to the sensors;
• sensor 1 echo to the multiplexer 1st in, sensor 2 echo to the multiplexer 2nd in;
• pins 12, 10, 8 (gpio 18, 15, 14) 40, 38, 36 (gpio 21, 20, 16) for the selection bits of the multiplexer;
• on the output of the multiplexer I put the 330ohm resistor that went into pin 7 (gpio 4) and in parallel to the pin 7 the 470ohm resistor to ground.

simulate this circuit – Schematic created using CircuitLab

My python code is this:

``````from gpiozero import DistanceSensor, OutputDevice
from time import sleep

def select_number(num):
binary = f'{num:03b}'
a.on() if int(binary[2]) else a.off()
b.on() if int(binary[1]) else b.off()
c.on() if int(binary[0]) else c.off()

a = OutputDevice(21)
b = OutputDevice(20)
c = OutputDevice(16)
while True:
distance = []
for i in range(2):
select_number(i)
with DistanceSensor(echo=4, trigger=17, max_distance=4, queue_len=2) as sensor:
sleep(5)
distance.append(0 + sensor.distance)
print(f'Distance to nearest object is {distance[0]:.4f} - {distance[1]:.4f}')
``````

All seems fine to me, I only put in the echo out the multiplexer, however even if I try different sleep times the distances of the sensors are wrong: sometimes I get the same distance, sometimes they are swapped. I also tried to connect the trigger with the multiplexer with a common echo out but it was worse. Does anyone have any idea how I can sort it out without using different pins for every sensor?

Edit

I spotted one error in the code, thanks to the tool linked by joan, it was in the selector of the multiplexer: I had to initialize the 3 pins globally and not inside the function (I have updated the code) because otherwise they come back to their initial state.

Then, I tried other configurations, I used the multiplexer for trigger following the suggestion of Milliways with one common echo input but it resulted most of the times in no echo or no updating.

I tried to connect only one sensor and detach the other and I saw that it didn't have that problem. So I tought that the echo was absorbed by the turned off sensor when attached both.

I had at home some 1N4007 diodes that they have a resistance around 600ohm. I replaced the 330ohm with one of those diodes for each sensor and then added in series to the 470ohm resistor a 390ohm resistor to keep the same voltage for the input pin (5v -> 3.49v (600 - 860ohm) instead of 3.51v (330 - 470ohm)). However, that configuration didn't send any echo (yes, the diodes were connected in the correct way).

The last configuration was to use the multiplexer for the trigger and another one for the echo using a common selector, so the sensors' lines are at the maximum separated. This nearly worked, but sometimes the script get stuck because it doesn't receive any echo or it isn't able to read one even if they arrive to the raspberry.

• No reason why this should not work in principle. I suggest you use piscope to see if you can spot what is going wrong. abyz.me.uk/rpi/pigpio/piscope.html Check the trigger and echo lines are as expected.
– joan
Commented Dec 18, 2021 at 16:00
• @joan I tested the voltage on the 470 resistor and it changes when I select the inputs in the multiplexer (I tried a short and a long distance to see the difference). I will give a try what piscope says. From what I briefly saw I can use it from my computer connecting in remote because I don't have a desktop interface in the PI I am using. Thanks so far Commented Dec 18, 2021 at 16:30
• @Ripper346 I did not suggest "multiplexer for trigger". This could work, but I would just use a normal GPIO (at least for testing). You will need an OR gate for combining echo pins (although simple diode gate should suffice. Commented Dec 19, 2021 at 23:58
• "1N4007 diodes that they have a resistance around 600ohm" diodes DO NOT have a resistance - you are putting 5V on your GPIO! Commented Dec 19, 2021 at 23:59