Search code examples
arduinoraspberry-pimicrocontrollersensorsarduino-ultra-sonic

Can two Ultrasonic sensors share the Trigger cable


I'm creating a drink dispenser that needs 6 ultrasonic sensors (HC-SR04) as shown in the image to check whether the user has placed their cup in the right spot. However, I do not have enough digital pins on my Microcontroller to connect the trigger and echo pins of all of them.

This is what the general code for these sensors looks like.

My question is, can I use the same line to connect all the trigger pins for the ultrasonic sensors as shown in the diagram? Or do the trigger pins have to be separate?

Theoretically, to me it should work, because all I need is the echo value of the sensor next to the right drink that they have chosen. Electric circuit diagram

Is there a more preferred way to use less pins instead? I need 6 digital pins for 6 relays connected to motors


Solution

  • generally, it is best to have a unique pin for every interrupt so that you can set up a unique ISR for each pin.

    in this instance, as every sensor is identical, it should be ok. you might want to check that it is ok to connect all the outputs of the sensor together though, if one output is low will that cause a short if the others remain high?

    if that's fine then you will need to add a bit of code that will basically loop through each sensor to see which one has caused the trigger

    also, you will need to identify the sensor that has caused the trigger and make sure that it is reset as soon as possible after the trigger event. if not, the Arduino could miss another trigger event from another sensor if the first sensor isn't pulled high