Multiplexer (of sorts) needed

Thread Starter

Baart

Joined Jan 24, 2017
5
Hi, for my project, I need navigation keys ( up, down, left, right and enter) but I don’t have sufficient gpio pins on the soc I want to use (ESP32) after connecting the tft screen and external ADC so I was wondering if there is an if I could use. Basically, is there a device which would output a number when requested corresponding to its inputs. For example if it was 8 channel and non of the inputs were high, it would return 0. If all 8 inputs were high, it would return 255 or any other number which was a binary representation of the state if its inputs?

I have tried googling but I really don’t know what search terms I would use.

Thanks for your help. Steve.
 

crutschow

Joined Mar 14, 2008
38,503
The 8 inputs are already a binary number.
Do you want the number in some sort of serial format?
If so you could use a parallel-to-serial shift-register.
 

TeeKay6

Joined Apr 20, 2019
573
Hi, for my project, I need navigation keys ( up, down, left, right and enter) but I don’t have sufficient gpio pins on the soc I want to use (ESP32) after connecting the tft screen and external ADC so I was wondering if there is an if I could use. Basically, is there a device which would output a number when requested corresponding to its inputs. For example if it was 8 channel and non of the inputs were high, it would return 0. If all 8 inputs were high, it would return 255 or any other number which was a binary representation of the state if its inputs?

I have tried googling but I really don’t know what search terms I would use.

Thanks for your help. Steve.
@Baart
One approach would be to use a parallel-to-serial shift register such as 74HC165 or HEF4021 (there are many others). Control pins allow you to latch all 8 inputs at once and then read them into your microcontroller in a serial fashion as a byte. When searching, note that parallel input and parallel output devices are available; you want a parallel input device.
 

Papabravo

Joined Feb 24, 2006
22,082
@Baart
One approach would be to use a parallel-to-serial shift register such as 74HC165 or HEF4021 (there are many others). Control pins allow you to latch all 8 inputs at once and then read them into your microcontroller in a serial fashion as a byte. When searching, note that parallel input and parallel output devices are available; you want a parallel input device.
Nintendo game controllers have used this technique.
 

djsfantasi

Joined Apr 11, 2010
9,237
Wiring switches to resistors as a digital to analog converter is known as an R2R ladder circuit. There are also chips to do DAC as well. Not necessarily chosen for your application (chosen only as an example) is a TLC7524IN.

If you understand the process of turning the output of a set of switches to a analog value that is unique for every possible switch settings... Then reading that output on an analog GPIO pin... And finally convert (in software) back to the original switch settings (ADC on the micro)...

Then search for DAC.
 

TeeKay6

Joined Apr 20, 2019
573
@danadak
The tpic2810 device is parallel outputs only. The '595 device discussed in the Arduino forum is also a parallel outputs only device. To simply read switch states, parallel inputs are needed. However, in some specific cases the state of switches can be made discoverable using a parallel output device by essentially driving only one switch at a time and reading the status via a common (to all switches) single input line to the microcontroller.
 

djsfantasi

Joined Apr 11, 2010
9,237
@danadak
The tpic2810 device is parallel outputs only. The '595 device discussed in the Arduino forum is also a parallel outputs only device. To simply read switch states, parallel inputs are needed. However, in some specific cases the state of switches can be made discoverable using a parallel output device by essentially driving only one switch at a time and reading the status via a common (to all switches) single input line to the microcontroller.
What do the switches connect? Is one end the output and the other ground or Vcc?
 

TeeKay6

Joined Apr 20, 2019
573
What do the switches connect? Is one end the output and the other ground or Vcc?
@djsfantasi
If your question is directed at me, I have no answer. The TS's first post indicated a desire to read navigation keys and those could be hooked up in multiple configs. That is why I did not offer more detail on how to use parallel output devices. In fact, the TS has not even stated how many I/O lines are available for reading the navigation keys; are the available lines I, O, or I/O?
 

Thread Starter

Baart

Joined Jan 24, 2017
5
Hi guys, thanks for your input. I believe a parallel to serial shift register such as 74hc165 as suggested will be ideal. I had already considered myself using an ADC pin with a resistance network and thought about the permutations if more than one key was pressed and went off it.

@TeeKay6, you seem to be lambasting me for not giving sufficient info simply because you are defending what you’re not sure is an attack on you which I don’t think was either an attack or directed at you. I explained I had five navigation keys, therefore I need input, not output or input/output. I also said I didn’t have enough io pins, so less than five, does it really matter how many? I also stated that the keys could be high or low, it matters not if closing them pulls them high or low, that’s up to my software to say, “oh, it’s low therefore the switch is open” etc!

Remember, people of all levels of skill come here for advice, not insults. If they haven’t given sufficient information, ask them nicely to expand their question. Even if they ARE obvious dimwits. They will UNDOUBTEDLY know something you do not, even Alexa says “Sorry, I don’t know that one” and she has a brain the size of a planet!
 

djsfantasi

Joined Apr 11, 2010
9,237
Hi guys, thanks for your input. I believe a parallel to serial shift register such as 74hc165 as suggested will be ideal. I had already considered myself using an ADC pin with a resistance network and thought about the permutations if more than one key was pressed and went off it.
You do realize than an R2R ladder with eight switches will divide the reference voltage (Vcc) into 256 steps? Thus, it accounts for any combination of switches pressed. Each of the 256 voltage ranges represents a possible combination of switches.

So you read the analog voltages. You calculate which of the ranges it falls in. This is a number from 0-255. The bit positions that are set in that number correspond to the switches pressed.

One pin. Eight switches. Easy peasy.
 

djsfantasi

Joined Apr 11, 2010
9,237
Just as an exercise, I quickly coded an Arduino C example to illustrate how you could decode eight switches with one analog pin on an Arduino. (I didn’t compile it, so there may be typos! It’s not a complete sketch anyway)

Code:
unsigned int ThrowAway;
unsigned int SwitchAnalog;
byte SwitchStatus;
...
ThrowAway=analogRead(Apin);
SwitchAnalog=analogRead(Apin);
SwitchStatus=map(0,1023,0,255,SwitchAnalog);
for (byte mybit=o;mybit<8;mybit++){
    if(SwitchStatus && pow(2, mybit)){
        // Switch “mybit” is closed;
        // Do the needful !
        }
    }
Note the two successive analog reads. They may or may not be required, but it’s safe to do so. There are cases where the Arduino returns incorrect results if the double read is not done.

The map() function maps one range of integers onto another. Mathematically it’s eady, but having a function is easier.

The for loop tests each individual bit in the input result. Easy peasy.
 

djsfantasi

Joined Apr 11, 2010
9,237
@djsfantasi
If your question is directed at me, I have no answer. The TS's first post indicated a desire to read navigation keys and those could be hooked up in multiple configs. That is why I did not offer more detail on how to use parallel output devices. In fact, the TS has not even stated how many I/O lines are available for reading the navigation keys; are the available lines I, O, or I/O?
No, I doubt you’d know the TS circuit well enough to answer that question. It was directed at the TS.
 

Thread Starter

Baart

Joined Jan 24, 2017
5
@djsfantasi , I really can’t get my head around your solution at all. I’ve got 1/rt = 1/r1 + 1/r2 .. and v=ir .... in my head and what value resistors do I use and what if there’s 1,2,3 or 4 resistors in series (due to the number of keys pressed), how do I calculate it all? It’s too mind boggling. Thats why I gave it up in the first place. Now, getting a serial number from a chip, doing bitwise comparisons to see what switches are presses, I can get my head around easy. I guess I’m a digital person rather than an analogue one! Having said that, I’m a vibration analyst. My project is capturing time based vibration, turning them into frequency based spectras and analysing them!

Anyhow, once again, I thank all who responded and I believe I have my (digital) solution. Steve.
 

TeeKay6

Joined Apr 20, 2019
573
Hi guys, thanks for your input. I believe a parallel to serial shift register such as 74hc165 as suggested will be ideal. I had already considered myself using an ADC pin with a resistance network and thought about the permutations if more than one key was pressed and went off it.

@TeeKay6, you seem to be lambasting me for not giving sufficient info simply because you are defending what you’re not sure is an attack on you which I don’t think was either an attack or directed at you. I explained I had five navigation keys, therefore I need input, not output or input/output. I also said I didn’t have enough io pins, so less than five, does it really matter how many? I also stated that the keys could be high or low, it matters not if closing them pulls them high or low, that’s up to my software to say, “oh, it’s low therefore the switch is open” etc!

Remember, people of all levels of skill come here for advice, not insults. If they haven’t given sufficient information, ask them nicely to expand their question. Even if they ARE obvious dimwits. They will UNDOUBTEDLY know something you do not, even Alexa says “Sorry, I don’t know that one” and she has a brain the size of a planet!
@Baart
I am sorry that you feel you have been lambasted; you were not. I had no belief that @djsfantasi was attacking anyone; it seemed to me that he was asking a question. Since it was unclear to me(!) whether his question was directed specifically to me, I began my reply with "if" and that was why my response was specifically directed to djsfantasi, not to you. Questions are not invariably attacks and (all, not just AAC) forums' questions tend to be terse and direct.

As for the need for input pins, I believe my previous posts above make it abundantly clear that I(!) am aware of that. As you are obviously not aware, different solutions to your problem may require different numbers of pins and pin types (In, Out, In/Out, DAC, ADC, etc). If you had only 2 input-only logic pins remaining and you had to read 5 (SPST) switches, then most of the suggested solutions above would not be feasible (but other solutions could be). Having all forum readers attempting a solution to an incompletely stated problem is unfair to those readers as they waste time and effort on solutions that cannot be implemented for your specific circuit. My statement about readers not even knowing how many pins & types were available for a solution was directed to that issue and was intended to remind all readers of the current topic & posts that we did not yet have all needed info.

As for the effect of your navigation keys configuration (SPST?, close/open on user actuation?, switches connected to pos supply, ground, or other potential?), that configuration limits what solutions will be available. All my questions and those of djsfantisi were relevant to offering solutions to your problem.

As for your chosen solution, the 'HC165, you cannot implement that solution without at least one output pin--to clock the data from the shift register into your microcontroller. You also would likely need an output pin to specify when data is to be latched into the shift register. You would also need an input pin to read in the data from the shift register. Are all those pins available? How are forum commenters to know that? Different device types most often require different control lines.

As it happens, I had been impressed by your earlier responses to forum commenters. You seemed knowledgeable--much more than many others--and clearly had a definite goal in mind. I hope that I was not mistaken.
 

djsfantasi

Joined Apr 11, 2010
9,237
@djsfantasi , I really can’t get my head around your solution at all. I’ve got 1/rt = 1/r1 + 1/r2 .. and v=ir .... in my head and what value resistors do I use and what if there’s 1,2,3 or 4 resistors in series (due to the number of keys pressed), how do I calculate it all? It’s too mind boggling. Thats why I gave it up in the first place. Now, getting a serial number from a chip, doing bitwise comparisons to see what switches are presses, I can get my head around easy. I guess I’m a digital person rather than an analogue one! Having said that, I’m a vibration analyst. My project is capturing time based vibration, turning them into frequency based spectras and analysing them!

Anyhow, once again, I thank all who responded and I believe I have my (digital) solution. Steve.
Ok, I have some time tomorrow. I’ll set up an LTSpice simulation and schematic tomorrow. With resistor values.

Hopefully once you see the schematic, you’ll be able to wrap your head around it... And you’ll see how really simple it is.

‘Til the morrow!
 

djsfantasi

Joined Apr 11, 2010
9,237
I’m having trouble editing my post, but I wanted to ask if you did a search for “R2R ladder”?

If you had, I doubt you’d still be confused.
 

djsfantasi

Joined Apr 11, 2010
9,237
@Baart
I am sorry that you feel you have been lambasted; you were not. I had no belief that @djsfantasi was attacking anyone; it seemed to me that he was asking a question. Since it was unclear to me(!) whether his question was directed specifically to me, I began my reply with "if" and that was why my response was specifically directed to djsfantasi, not to you. Questions are not invariably attacks and (all, not just AAC) forums' questions tend to be terse and direct.

As for the need for input pins, I believe my previous posts above make it abundantly clear that I(!) am aware of that. As you are obviously not aware, different solutions to your problem may require different numbers of pins and pin types (In, Out, In/Out, DAC, ADC, etc). If you had only 2 input-only logic pins remaining and you had to read 5 (SPST) switches, then most of the suggested solutions above would not be feasible (but other solutions could be). Having all forum readers attempting a solution to an incompletely stated problem is unfair to those readers as they waste time and effort on solutions that cannot be implemented for your specific circuit. My statement about readers not even knowing how many pins & types were available for a solution was directed to that issue and was intended to remind all readers of the current topic & posts that we did not yet have all needed info.

As for the effect of your navigation keys configuration (SPST?, close/open on user actuation?, switches connected to pos supply, ground, or other potential?), that configuration limits what solutions will be available. All my questions and those of djsfantisi were relevant to offering solutions to your problem.

As for your chosen solution, the 'HC165, you cannot implement that solution without at least one output pin--to clock the data from the shift register into your microcontroller. You also would likely need an output pin to specify when data is to be latched into the shift register. You would also need an input pin to read in the data from the shift register. Are all those pins available? How are forum commenters to know that? Different device types most often require different control lines.

As it happens, I had been impressed by your earlier responses to forum commenters. You seemed knowledgeable--much more than many others--and clearly had a definite goal in mind. I hope that I was not mistaken.
I hope you didn’t think that I was lambasting you nor was I being condescending. I understood your original post to mean you wanted to minimize the number of pins to read 8 switches. And additionally, that you couldn’t see how to make a resistor network to encode the switches with an analog value.

I plan on addressing the latter point tomorrow. Not because it’s the only way or correct way to solve your problem. But because you were having trouble “wrapping your head” around the analog solution, I have decided to demonstrate how it may work.

You can discard a potential solution that minimized the pins required to read multiple switches because you don’t understand it. Or you can let us teach you a new method. Your choice!

I have already gone so far as providing code to extract the switch positions, based on an R2R ladder. Look for it tomorrow!
 

WBahn

Joined Mar 31, 2012
32,823
@djsfantasi , I really can’t get my head around your solution at all. I’ve got 1/rt = 1/r1 + 1/r2 .. and v=ir .... in my head and what value resistors do I use and what if there’s 1,2,3 or 4 resistors in series (due to the number of keys pressed), how do I calculate it all? It’s too mind boggling. Thats why I gave it up in the first place. Now, getting a serial number from a chip, doing bitwise comparisons to see what switches are presses, I can get my head around easy. I guess I’m a digital person rather than an analogue one! Having said that, I’m a vibration analyst. My project is capturing time based vibration, turning them into frequency based spectras and analysing them!

Anyhow, once again, I thank all who responded and I believe I have my (digital) solution. Steve.
What djsfantasi is talking about is a very specific configuration of resistors known as an R/2R ladder. It is a very old means of implementing D-to-A converters. Google it.

The problem you will have with a discrete R/2R network is accuracy. Since there are 256 possible values, each step is less than 0.4% of the range from it's neighbor and that means that the absolute accuracy of your network has to be better than about 0.1% in order to decode the correct values. Not only does that means that you have to have high precision resistors, but also a VERY stable voltage source for your switches (it must also be within 0.1% otherwise you are screwed right from the start) and it must be sufficiently low output impedance that it will stay within 0.1% whether none of the switches are pressed or all of them are.

The problem here is that, unlike most D/A applications where if you are off by a couple of steps nothing happens other than your measurement has a small amount of error, for this application you MUST decode it absolutely correctly every time and being off by just a single step means you will not determine the switch settings correctly and may well get most or all of them incorrect.
 
Top