# Need some help with multiplexing 4 pins into 2

#### bytraper

Joined Sep 28, 2010
127
Hi guys and gals!
I've got a project where I have 4 outputs of a R/C controller (like a garage door opener keyfob)
And It has 4 data outputs, D1 to D4, each outputting 20ma of current into its own output.
I'm running low on pins of my microcontroller and so would like to convert these 4 separate outputs into 2 microcontroller inputs!

Ideally, I'd like the outputs to be as such:
0-0
0-1
1-0
1-1

Has anybody got a circuit drawn up or could possibly point me in the right direction here?

Thanks!

#### absf

Joined Dec 29, 2010
1,949
It's called priority encoder. see here

Allen

#### bytraper

Joined Sep 28, 2010
127
Hmm, thanks dude that's exactly what I was looking for.

I've been trying to draw one up in a great little logic simulator called Logicly but they only have gates with standard 2 pin inputs and one output, so I don't exactly know HOW to draw the 3, 4 or 5 inputs into the circuit. Even that link you posted for me shows the logic in the block diagram, but when you try to build the circuit to simulate it, you see that in the block diagram showing the gates, the switch input A2 isn't even connected to anything!

How am I supposed to draw that!

#### absf

Joined Dec 29, 2010
1,949
I simulated it in Proteus and with a little mod, it seems to work fine....

As there are 4 inputs and 2 outputs, how do you differentiate between "No key pressed" and "First key is pressed"?
I add in a non-inverting buffer to make the outputs tri-state when no key is pressed and "oo" when the first key is pressed...

Allen

#### absf

Joined Dec 29, 2010
1,949
If you think the above is too much trouble for a simple job to reduce 4w to 2w, then use 74HC148 instead.

Allen

#### bytraper

Joined Sep 28, 2010
127
Ooh! BRILLIANT!
Thank you so much for taking the time to help me out Allen, that just solved a problem I've been trying to get my head around for a good two weeks!
I didn't even consider no buttons pressed, because I was so focused on trying to solve the 4 into 2 problem, that issue didn't even remotely occur to me and undoubtedly would have had me banging my head against a wall for another unknown amount of time!
I really appreciate your help and time dude, thank you so much!

#### absf

Joined Dec 29, 2010
1,949
I've got a project where I have 4 outputs of a R/C controller (like a garage door opener keyfob)
And It has 4 data outputs, D1 to D4, each outputting 20ma of current into its own output.
I'm running low on pins of my microcontroller and so would like to convert these 4 separate outputs into 2 microcontroller inputs!
Looking at your opening post again. I was wondering how your uC is going to detect a tri-state input when there is no key-pressed.

I would use a third wire called "KeyReady" or "KeyEnabled" to notify the uC that a key has been pressed. But that would add one more wire to the 2-wire solution which I think would defeat your original intentions. So unless you have more wires to encode (e.g. 16 to 4), a 4-2 encoder is not worth all the troubles or the best solution in this situation.

Why not just get a bigger uC with more I/O pins and solve it once forever....

Allen

#### bytraper

Joined Sep 28, 2010
127
Hey Allen,
I've been thinking long and hard about this project for about 2 months. It basically uses the keyfob to control an electronic LED in a candle that I built to help me fall asleep (I have sleeping problems) and depending what button I press on the remote, I get a unique RGB flash or flicker from the LED which, inside the candle looks like a little flame or relaxingly cycles through all different colours!

The keyfob issue I've been pondering for a few weeks now and finally got to the point I realised I'd be best to throw my question in here because I was just getting nowhere fast, and of course frustration was starting to seep in, which would have ultimately resulted in my project ending up in the too hard basket or likely abandoned.

I'm using an attiny45, so not too many pins to begin with. The chip really only has 5 I/O and one reset line, but if I use the reset line which I don't actually need, I can open up that magical third input!

The keyfob I've chosen is a 4 channel unit, but after reading your solution, and looking at the 8 to 3 IC you suggested, I thought I would free up that reset pin and use the extra pin to work with that encoder.
Also, thanks to your input and suggestion, I started looking at a 6 channel keyfob which is even better than the 4 channel one I originally planned for as now I can have more functions thanks to those extra inputs!

The keyfob is only really a single channel device so looking at that page you sent me, I think I can use a "simple" encoder rather than a "priority" encoder because while the keyfob has 6 buttons, it can only send one at a time, it can't deal with multiple keypresses.

So now I am looking at this 74HC148 those, which will leave me two free inputs which means I can have one as a no button pressed state!
I just need to work out how to use the IC which should only be 3 pins and I shouldn't need to use any of the other stuff on the IC!!

I think you have actually provided me the ultimate solution!

#### Colin55

Joined Aug 27, 2015
519
If you are only going to detect one line at a time, you only need one input for the 4 or 6 lines.
No-one here has done what they are talking about and have offered overly complex solutions to a very simple problem.
Here is what I did:

Last edited:

#### Picbuster

Joined Dec 2, 2013
1,025
If you are only going to detect one line at a time, you only need one input for the 4 or 6 lines.
No-one here has done what they are talking about and have offered overly complex solutions to a very simple problem.
Here is what I did:

View attachment 103577
Place a resistor over the 100nF. Mandatory avoid DC restoration over 100nf ( avoiding an 'open/floating' GP0 input.)
Diode serial resistors valued 2^n (n=1 to n=4) use adc @ GP0 to detect activated pin.
Picbuster

#### Colin55

Joined Aug 27, 2015
519
"Place a resistor over the 100nF. Mandatory avoid DC restoration over 100nf"

You don't know what you are talking about.

#### bytraper

Joined Sep 28, 2010
127
Thanks guys, I'll do some building this week and see what works best!
Colin, that's an embarrassingly good idea, I didn't consider that and you're right its so simple! The reason I didn't think of it because the original keyfob i was using was multi channel so I knew It wouldn't work... The new one I've settled on though is only one channel and for some reason, I'd started looking at the more complex options !

I would need to test how good the reliability is, but this is great, I've gotten some good information from this thread that will help me multiplex much easier than anything I'd considered.

I appreciate all the help everyone has given!

#### Picbuster

Joined Dec 2, 2013
1,025
"Place a resistor over the 100nF. Mandatory avoid DC restoration over 100nf"

You don't know what you are talking about.
Please read your basic electronics books and think twice before you make this type of remarks to an electronic designer(MSc) with 40 years of experience.

#### Colin55

Joined Aug 27, 2015
519
Please read your basic electronics books and think twice before you make this type of remarks to an electronic designer(MSc) with 40 years of experience.

What has 40 years taught you ???

Firstly the input is not "floating" with 100n across it.
Secondly, how is the input going to reach 66% of rail voltage with 100k across it ??????

#### absf

Joined Dec 29, 2010
1,949
Though I've not used attiny45 before but I have bought some and I am going to learn AVR in the near future. I have used the Arduino Uno but only programmed in its C language.

Thanks guys, I'll do some building this week and see what works best!
Colin, that's an embarrassingly good idea, I didn't consider that and you're right its so simple! The reason I didn't think of it because the original keyfob i was using was multi channel so I knew It wouldn't work... The new one I've settled on though is only one channel and for some reason, I'd started looking at the more complex options !

I would need to test how good the reliability is, but this is great, I've gotten some good information from this thread that will help me multiplex much easier than anything I'd considered.

I appreciate all the help everyone has given!
If you want to go the way of doing thing as suggested in post #9. Then you need a comparator in your attiny45. But since you have 4 channels of ADC in your tiny, it would be up to you to use either the COMP or ADC.

Read the application note attached. Mr colin55 was using the method #2 in page 1 of the Ap Note.

Allen

#### Attachments

• 481.1 KB Views: 1

#### Colin55

Joined Aug 27, 2015
519
You don't have to use either the COMP or ADC. Just a HIGH recognition and time the occurrence.

#### Picbuster

Joined Dec 2, 2013
1,025
Please read your basic electronics books and think twice before you make this type of remarks to an electronic designer(MSc) with 40 years of experience.

What has 40 years taught you ???

Firstly the input is not "floating" with 100n across it.
Secondly, how is the input going to reach 66% of rail voltage with 100k across it ??????
What did I taught me? ( That you move into a strange position not able to accept positive remarks and that you are convinced of being superior to mankind.)

And yes input is floating with only a cap connected to it. ( it only increase it's own input capacity by 100nF)

But; how to discharge cap after loading it via the diode? ( it remains high until discharged by mpu's input impedance.
And charged when electro magnetic field occurs (mobile phone or other field EMF occurs) . If the input weak pull-up is active it will stay high forever.
Anyway good luck with your design.
Picbuster

#### Colin55

Joined Aug 27, 2015
519
Where do you get this waffle from:

"And yes input is floating with only a cap connected to it."
"But; how to discharge cap after loading it via the diode? ( it remains high until discharged by mpu's input impedance."
These two statements are entirely INCORRECT.
Do a little bit of microcontroller programming before coming on the Forum and feeding this rubbish to the readers.

#### Colin55

Joined Aug 27, 2015
519
An input is considered to be floating when it can pick up stray voltages and rise and fall very quickly and the micro detects values that it cannot deal with. When a 100n is connected to the input, it charges and discharges very slowly and does not charge via stray voltages or interference from electromagnetic waves.
The 100n capacitor is discharged by making the pin an OUTPUT and taking it LOW to quickly discharge the 100n.

#### dannyf

Joined Sep 13, 2015
2,197