I got this question from my teacher the other day and I can't figure it out:

If you have two sensors from two different outputs but want them to go into one input, what do you need, and how can you read the input?

enter image description here

In short, the project includes an Arduino Uno with two ultrasonic sensors.

  • Well, i guess one correct answer would be "Multiplex", you'll have to switch the outputs inside the loop(). One output have to be HIGH during the other one is LOW, so you always get the input from one sensor only. Ofc you'll have to check which sensor is active to get the correct sensors signal. Another way would be using analog input and different resistors for each sensor. So you get different voltage signals from the sensor. – nouseforname Aug 24 '15 at 14:41
  • I think those unit pull low to send a signal. So you could use just two diodes. Though I'm unsure where the out signal is going, as most ultrasonic units have only one SIG pin to use as both input and output. – Gerben Aug 24 '15 at 18:08
  • If you multiplex then you need another pin to control which multiplexer input is going to be selected. Please state which sensor you are planning to use. Without knowing any more the "two diodes" answer seems to be the most likely. – Nick Gammon Aug 24 '15 at 20:49

There are two basic solutions to your problem, an active one and a passive one, and which is best (or will even work) depends on the rest of your setup.

The active solution requires a device called a multiplexer. This is basically a switch and it selects one of its inputs to route through to its output, so it can be used to connect your input pin to the output of one of the sensors. It requires an extra IO pin, though, so is pretty pointless as you could just use that IO pin as an input from the second sensor. It does mean, though, that both sensors can be active at once and you just select one to read the output from.

The passive solution is called open drain or open collector (if you are using TTL not CMOS terminology). This means you convert each of the outputs of the sensors into a switch using a transistor (NPN BJT or N-Channel MOSFET) to connect your input pin to ground. When then transistors are off the input pin is pulled up through a pullup resistor (could be internal or external to the IO pin). When one sensor's output activates it turns on the transistor which then pulls the input pin low. This means that you can only have one sensor active at a time, so they have to be triggered by separate output pins.

enter image description here

Open Drain is how things like I2C support multiple devices on the same bus without any voltage conflicts.

  • Isn't it right that the Multiplexer is not needed as you can switch the outputs by yourself in code? – nouseforname Aug 24 '15 at 15:01
  • @nouseforname As long as you have separate outputs to control the sensors then the second (passive) option is for you. That is why that's the option that I put most detail into. A multiplexer may be needed if the signal is analog though (analog mux) since the OD won't work for that. – Majenko Aug 24 '15 at 15:02
  • understood, i am also new to this and just learned i can do the "multiplexing" in code...thats why i asked. Your solution seems to be the very perfect one for this case here – nouseforname Aug 24 '15 at 15:08
  • Is it a diode in a parallel with the MOSFET? – Nicklas Mandrup Frederiksen Aug 24 '15 at 16:22
  • That is called the body diode and is integral to all MOSFETs. – Majenko Aug 24 '15 at 16:23

This solution only actually uses one pin, but only works with simple things like push buttons (which are either open, or closed).

Connect a (different) resistor in series (end-to-end) with each switch. Connect the loose end of each resistor together to +5V. Connect the loose end of those two switches together, then via another resistor to ground. Where the two switches meet, connect them to an analog pin on your Arduino.

For example, you might use a 20k resistor with button A, and a 10k resistor with button B, and a 10k resistor connected to ground. They should be pretty close to the same value, and at least 1k each.

Use the ReadAnalogVoltage sketch to measure the voltage with different combinations of button presses (no button pressed, A, B, A+B), then, in your final sketch, you can compare the value read with the values in this table - pick the closest one, because actual readings may vary slightly. For the resistors above, you should get about: (no button = 0v; A=1.7v, B=2.5v, both 3v). Sort these numerically (which in this case they already are), and work out the values exactly between each pair (up to 0.85 is closest to 0=> no button; else up to 2.1=> button A; else up to 2.75 is closest to B; otherwise, both are pressed.

The circuit described looks like this:

Switch circuit

The wire leading off to the right goes to the analog pin on the Arduino. Switch A is on the left.

The results can be used either in a table, with a loop, or in an if/else if statement (I used this technique on a 12-key keypad, so the list was a lot longer).

Read up about voltage dividers https://learn.sparkfun.com/tutorials/voltage-dividers and, for both buttons being pressed at the same time, resistors in parallel https://learn.sparkfun.com/tutorials/series-and-parallel-circuits


If you have two sensors from two different outputs but want them to go into one input, what do you need, and how can you read the input?

If you want your teacher lose his mind answer his question with this:

What I need? :)

  • A wire

enter image description here

The solution is to use one trig for both ultrasonic sensor and two echos.

or one echo and two triggers.

ultrasonic sensor sends the pulse > pulse back > now echo pin is connected to second sensors trig > the Second Ultrasonic sensor sends the pulse then starts listening. When the second ultrasonic echo back, it sends to other Arduino pin, reverse it if you want it on one input pin :)

#define trigPin 6
#define echoPin 4
#define echoPin2 7

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
   pinMode(echoPin2, INPUT);

void loop() {
  long duration, distance, distance2;
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  duration = duration + pulseIn(echoPin2, HIGH);
  distance2 = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){ 
    Serial.println("Out of range");
  else {
    Serial.println(" cm");

that's it.


I ran into the same question, except I wanted to put 8 of the HC-SR04 sensors on pro-mini.

I used an I2C pin expander for the triggers, and an octal OR/NOR chip to multiplex the return.

Library, examples and pictures

https://github.com/arielnh56/SonarI2C http://redhunter.com/blog/2016/04/28/sonari2c-multiple-hc-sr04-sensors-on-arduino-i2c/


The OP does not specify whether the focus is on hardware or software.

So I will presume that what the professor might looking for is a solution using minimal circuitry; as close to the diagram as possible. Thus, the solution would be implemented in software.

Given all that, I'd propose that the trigger signal be sent out on sensor 1, and the reflection measured at the receiving pin.

It is time-gated (the time for the sensor to react to an object at maximum range). If detected, then set a variable saving the distance measured on sensor 1.

Then the same could be sent from the second sensor's trigger pin. And if there is a reflection within the time-gate then that can be recorded in another variable.

It would require creating two instances of the ultrasonic sensor, and they would just happen to share the same receive pulse pin.


enter image description here

Try the above logic use a 555 timer with C1: 10uF, R2: 300k and R1: 30k for 1.1 sec switching time,now for approx every one second data from USS1 goes to SIN(Arduino IN) when the timer OUT is high and similarly when timer OUT is LOW signal from USS2 goes to SIN(Arduino),,,

But you will not be able to know from which sensor is the output coming.

If you would like to know from which sensor the output is coming you can just add one INVERTER to one of the branch so so both the sensors will give different states i.e one will return HIGH and other will return LOW

hope this helps

Edited:Please note 555 timer design part is not complete ,,i am just giving an idea :)


A SPDT switch would fit nicely in your diagram where the question mark is.


The question is two Ultrasonic data get input on one. Its simple. You just need two IDs. Creat a IDs of each sensors. You can use I2C bus too. You need to hub who keep the two input in raw. And than digitally write with address and than do codding for read the data. Howz my idea guys?

  • Welcome to Arduino Stack Exchange. Please take the Tour at arduino.com/Tour to get the most out of this site. FYI - Normal ultrasonic detectors do not work on I2C - they wrok by pulsing a signal pin then monitoring an output pin. The library measures the time and infers the distance. – SDsolar Jul 8 '17 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.