Multiplexer (of sorts) needed

djsfantasi

Joined Apr 11, 2010
9,237
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.
@TeeKay6 just pointed out to me that the actual number of switches is 5, giving only 32 possible values. So each step is 3% from its neighbor. Quite an improvement over 0.4% and possibly doable

The Arduino ADC has a 4mV resolution. The steps are 150mV wide. So it should definitely be doable.

Thanks for your advice on Googling R2R. I’ve mentioned that 2-3 times.
 
Last edited:

Thread Starter

Baart

Joined Jan 24, 2017
5
Hello, once again thanks for all your helpful comments. I realise that to every problem there are multiple solutions and using the resistor network is definitely one of them and if I sat down and thought about it, the maths would be straightforward especially as I’m using an ESP32 with 12 bit ADC resolution ie 0.8mV.

So, I have 4 spare IO pins, more than sufficient to use with the 74HC165. This chip also gives me the possibility of 8 inputs so I have spare capacity. It represents to me, the ideal solution as when I googled “74HC165 Arduino” it took me two seconds to know exactly how to implement it. Im a digital person who prefers digital solutions. I’m not against learning new things, in fact since I retired to Cyprus two years ago I reckon I have learned more than in the previous 57 years of life!

@TeeKay6, Maybe I need to learn not to be so sensitive but when anyone says the TS didn’t tell us this or the TS hasn’t told us that, it sounds like a criticism. Sorry. If you ever come to Geroskipou, there’s a cold Keo at the biker bar with your name on it! Cheers.
 

TeeKay6

Joined Apr 20, 2019
573
Hello, once again thanks for all your helpful comments. I realise that to every problem there are multiple solutions and using the resistor network is definitely one of them and if I sat down and thought about it, the maths would be straightforward especially as I’m using an ESP32 with 12 bit ADC resolution ie 0.8mV.

So, I have 4 spare IO pins, more than sufficient to use with the 74HC165. This chip also gives me the possibility of 8 inputs so I have spare capacity. It represents to me, the ideal solution as when I googled “74HC165 Arduino” it took me two seconds to know exactly how to implement it. Im a digital person who prefers digital solutions. I’m not against learning new things, in fact since I retired to Cyprus two years ago I reckon I have learned more than in the previous 57 years of life!

@TeeKay6, Maybe I need to learn not to be so sensitive but when anyone says the TS didn’t tell us this or the TS hasn’t told us that, it sounds like a criticism. Sorry. If you ever come to Geroskipou, there’s a cold Keo at the biker bar with your name on it! Cheers.
@Baart
I am an old timer (78yrs old) and I find much online conversation to be brutal; it has taken awhile to get used to it. Forum participation often requires that you have a "thick skin." If you find time, try reading a few dozen threads at this AAC forum and you will find out that what you deem as criticism is most often just a desire to quickly get the facts that are needed to solve a problem...and to prevent an enormous waste of mental energy by reviewers as they submit solution after solution only to have the thread starter make known a new solution requirement after each solution is published. It is a constant problem for commenters/reviewers that so many thread starters give so little info about what they want. Often it is necessary to ask outright and bluntly multiple times before getting even trivial info from the thread starter.

I rather doubt that I will visit Cyprus, but thanks for the offer. Good luck to you!
 

djsfantasi

Joined Apr 11, 2010
9,237
@Baart

I am glad you have enough pins for the digital solution. But I’ve spent time preparing to present R2R ladders, so please take a moment to check out my links.

The wiring is simple. I didn’t get on LTSpice, because the Wikipedia article is very clear. Here’s the circuit. Just replicate if for 5 switches.

57AF6B0A-33E1-4D8E-BFC7-4B9DC72F9DCE.png

So, while refreshing my knowledge of R2R ladders, I found something I suspected, but wanted to confirm.

It doesn’t matter what resistor value you use. No confusing calculations. If anything, the resistance R needs to limit input current to the pins requirements, such that 2*R limits the current to that needed

There are equations in the Wikipedia article that demonstrate how you can calculate the output voltage. The article also explicitly uses 5 switches for example! How lucky!

So for the cost of 10 1% resistors and a bit of coding, you’re done. You mentioned in your post you could add more switches because you had additional capacity. By adding two resistors, you can add another switch with the R2R ladder.

If you’re still listening, ask anything you need to understand. I’ve put as much information as I could to help you understand this technique. If you’re a digital guy (as I am), knowing this simple and inexpensive technique will certainly be used by you someday. And help you learn to use more accurate methods.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,237
One last thing. Since I got the number of switches wrong, I went back and edited my code snippet. I also tweaked the variables, to make them easier to understand. And I added a constant for the number of bits. If you want a different number of switches (up to 8), you only have to change the constant value.

Code:
const byte nbits = 5;

unsigned int throwAway;
unsigned int switchAnalog;
byte maxDigital= pow(2, nbits)-1;

byte SwitchStatus;

...

throwAway =analogRead(Apin);
switchAnalog=analogRead(Apin);
switchStatus=map(0,1023,0,maxDigital,switchAnalog);

for (byte mybit=o;mybit<nbits;mybit++){
    if(SwitchStatus && pow(2, mybit)){
        // Switch “mybit” is closed;
        // Do the needful !
        }
    }
I know this isn’t what you asked. But it won’t take much of your time to review and perhaps you’ll learn something new. As I said, I got my start in electronics with digital designs. Then since I was an IT professional, moved to microprocessors. This technique bridges the analog and digital worlds.
 

MisterBill2

Joined Jan 23, 2018
27,640
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.
A binary decoder IC is what you need, and several are available.
 

Thread Starter

Baart

Joined Jan 24, 2017
5
I know this isn’t what you asked. But it won’t take much of your time to review and perhaps you’ll learn something new. As I said, I got my start in electronics with digital designs. Then since I was an IT professional, moved to microprocessors. This technique bridges the analog and digital worlds.
Thank you for taking the time to explain this. I have indeed studied the article and see how it could work in my project. Just to prove that I did read it, may I point out that I will be using an ESP32 with 12 bits resolution so surely line 13 in your snippet will be

switchStatus=map(0,4095,0,maxDigital,switchAnalog);

Again, thanks to all posters. Your support is much appreciated.
 

djsfantasi

Joined Apr 11, 2010
9,237
A binary decoder IC is what you need, and several are available.
Another great solution. Only requires three pins on the μP. But in the direction his data is going, the TS needs a binary encoder ic. A decimal to bcd, a decimal to binary or an octal to binary encoder will work.
 

MisterBill2

Joined Jan 23, 2018
27,640
Another great solution. Only requires three pins on the μP. But in the direction his data is going, the TS needs a binary encoder ic. A decimal to bcd, a decimal to binary or an octal to binary encoder will work.
Use a CD4053 8 line to one analog switch to scan the inputs Or for only 4 lines use the CD4052 4 line switch, use the other line as the input. simple and easy and available from quite a few vendors.
 
Top