Large number of buttons to one analog input - a possibly new design - Page 1
Forum section: electronics / general

author: lapi
author_rank: Contributor
timestamp: May 29, 2021, 07:48:26 pm
content: While working on a project of connecting 28 buttons to one analog pin of Cypress PSOC4 I chanced at a design that easily and with small number of components produces evenly distributed voltages from a matrix keypad. The design is described in a hackaday project (https://hackaday.io/project/180024-two-wire-matrix-keypad-to-analog-input) with links to Arduino code and schematic in GitHub. The basic idea is to use matrix keypad to encode evenly spaced resistances throuh the matrix and then using a simple current mirror to convert the resistances to voltage levels for the ADC pin.Has this design been used anywhere before, in a product or in a hobby project? I have not been able to find any reference in the internet to similar design.

author: mariush
author_rank: Super Contributor
timestamp: May 29, 2021, 08:14:28 pm
content: The technique is explained in Microchip's Tips and Tricks pdf , see tips 5 and 6 (pages 8-9):https://ww1.microchip.com/downloads/en/devicedoc/01146b.pdfThe downside is you have difficulty detecting multiple buttons pressed at same time, and that there's some delay in the ADC which could cause missed pressed (or difficulty doing button debouncing)Considering the number of buttons, it probably would make more sense to just use a port extender or a separate microcontroller so that you can use interrupt on change ( when one button is pressed, trigger interrupt and dump 32 bits to the main microcontroller - or as many bits as you have buttons)

author: lapi
author_rank: Contributor
timestamp: May 29, 2021, 08:36:27 pm
content: Thanks @mariush! These mature Microchip tips indeed are doing a resistance to voltage conversion. They were intended for microcontrollers without AD-inputs and hence use the RC-time constant method. Without a constant current source, though, handling more buttons has the disadvantages of a resistive divider, namely complex set of resistors to get more buttons separated, added with the exponential charging of the capacitor.For human pressing the buttons (at best around 10 presses a second), the ADC-delay isn't an issue, also the debouncing can be done robustly as shown in the Arduino code example.In this project on the PSOC4, adding more chips was not an option, though in many cases and especially when multiple buttons being pressed need to be detected simultaneously, that would be a better approach.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 29, 2021, 08:51:50 pm
content: I've found this method in a lot of electronics, but with not such high number of buttons tied to a single ADC channel.A lot of buttons will need high ADC resolution and complex resistor values to avoid overlapping.- Consider splitting the buttons in multiple ADC channels to simplify the design while still using few pins.- Another option would be to use a cheap mcu that does the scanning (5x6 matrix) and sends IRQ/scancodes to the PSOC. I think a 16 pin mcu would be enough. You don't need a lot of power.- Using 11 IOs from the PSOC for the scanning might be an option.- Some i2c/spi extenders that send an IRQ on input change?- There're specialized key scanning ICs like LC75700T, TCA8418, ADP5586....

author: PCB.Wiz
author_rank: Super Contributor
timestamp: May 29, 2021, 11:17:50 pm
content: Quote from: lapi on May 29, 2021, 07:48:26 pmThat's the sort of design that looks fine at first glance on paper, but is not up to commercial production.The current mirror has wide initial gain variations, which means the ADC will certainly need calibration, and then the current mirror is going to vary with temperature.I find this in a Nexperia data sheet, for a nominally matched pair in the same package. VBEsat decreases by about 1.7 mV/K with increasing temperature. VBE decreases by about 2 mV/K with increasing temperature.So that's a skew of 0.3mV/K between the two 'matched' transistors, and they commit to only a 1.02 - 1.22 band over temperature.You could pay quite a bit more money, and buy something like thesehttps://www.analog.com/en/parametricsearch/10766#/and that specs 3% over temperatureMaybe a DS3920 Current Mirror is good enough ? Note that these parts all cost more than another small MCU...It's probably easier to just use a lower load resistor, with no mirror, and accept the transfer is non linear, but use a better ADC to compensate.

author: radar_macgyver
author_rank: Frequent Contributor
timestamp: May 29, 2021, 11:23:14 pm
content: It's used in my 2008 Subaru Impreza to communicate steering wheel button presses to the stereo head unit. When I installed an aftermarket unit, I had to buy a converter module which measured the voltage and generated a digital stream suitable for the aftermarket head unit.On cold days (well below freezing), the whole thing falls apart, every button is "shifted down" by one place, must be the voltage reference for the ADC inside the converter module.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 29, 2021, 11:27:48 pm
content: My car also uses this for steering buttons. But uses 2 analog lines, and only 3 buttons for each, no issues.I guess your problem is the converter itself, related to quality/design.

author: nctnico
author_rank: Super Contributor
timestamp: May 29, 2021, 11:50:33 pm
content: Quote from: lapi on May 29, 2021, 08:36:27 pmThe problem with not being able to detect which button is pressed exactly is that you might get unexpected results. Another source of problems are wear, moisture and dirt. So basically: don't use these kind of constructions on keypads. For reading dip-switches at startup it is somewhat OK but not for buttons that can be pushed in random combinations, are subject to wear & tear and weather influences. Better add a shift register or I2C I/O expander to scan the keyboard at regular intervals.

author: lapi
author_rank: Contributor
timestamp: May 30, 2021, 06:09:38 am
content: Quote from: radar_macgyver on May 29, 2021, 11:23:14 pmThat is an interesting case, thanks @radar_macgyver! I ran temperature simulation. There is 450 mV variation from -55C to 125C, which will limit the accuracy. In range -35C to 75C it is 280 mV. Both with the BCV61C curent mirror that is used in the demo. That, however, can be compensated with temperature measurement using the internal temperature sensor of the microcontroller.Edit: I took a look at the wiring manual of the 2008 Subaru Impreza and the circuit there looks more like part of the resistive divider circuits used also in many home appliances and consumer electronics devices.

author: lapi
author_rank: Contributor
timestamp: May 30, 2021, 07:15:59 am
content: Quote from: DavidAlfa on May 29, 2021, 08:51:50 pmThe key point of the design is that getting the voltage levels evenly spaced does not need complex combination of resistor values.Of course the tolerances of the resistors have to meet the requirements set by the number of buttons. Adding more expensive silicon will increase the BOMmuch more than adding more accurate resistors, in particular when small number of different values (only 3) are needed.I would like it very much to get a reference to a prior design which uses this few-resisitors-and-a-current-mirror approach. So far I haven't found any, not even in patent and literature searches.

author: msliva
author_rank: Newbie
timestamp: May 30, 2021, 08:12:41 am
content: What about I2C Controlled Keypad Scan IC? TCA8418 or the other type.https://www.ti.com/product/TCA8418TI prices: 1-99 $1.042,100-249 $0.913Disadvantage - Temporarily Out of Stock.

author: capt bullshot
author_rank: Super Contributor
timestamp: May 30, 2021, 08:31:19 am
content: Quote from: lapi on May 29, 2021, 07:48:26 pmI've often seen a resistive divider to read about 3 to 10 keys with an A/D input, it's quite common.Using identical resistor values by series connecting them is a good improvement over individually calculated resistors, I've done that before.The matrix configuration, using two resistor values is a clever improvement, I haven't seen or done that before (but I don't look everywhere).Next step, the current mirror eases the software, but I'd improve the current mirror by degrading through emitter resistors so the current is less transistor matching and temperature dependent. Any kind of constant current source / sink in theory would do the job. The current mirror makes the current proportional to the supply voltage, if one uses the same voltage as the A/D reference, you get a ratiometric measurement which is good. The only issue left is the temperature drift of the semiconductors.I can imagine applications where this technique would have been used in the past, but I've never seen it's been used to read a keypad. There's quite a few ICs out there that use a single (or a couple of) resistors connected to a pin to statically set parameters. Internally, they use a current source (which usually is a current mirror) and some comparators to determine which setpoint is wanted (I cannot find an example right now ...)In general, I'd prefer a circuit that is made of resistors only, for the price of somewhat more complex software. The reason is, resistors are more stable over temperature and better matched than cheap semiconductor circuits. Having a ratiometric measurement (supply of the divider is the same as the A/D reference voltage) is very important here, because it rules out temperature drifts of the supply voltages.

author: lapi
author_rank: Contributor
timestamp: May 30, 2021, 09:17:29 am
content: Quote from: capt bullshot on May 30, 2021, 08:31:19 amThanks for the suggestion! I studied the effect of the emitter resistors, and indeed, the temperature effect can be reduced significantly by adding a compensating emitter resistor to the output leg of the current mirror.

author: nctnico
author_rank: Super Contributor
timestamp: May 30, 2021, 09:32:24 am
content: For argument's sake: you can have a resistor across the current mirror and read that when the keypad is idle. In that case the current mirror doesn't need to be accurate at all.

author: lapi
author_rank: Contributor
timestamp: May 30, 2021, 09:56:48 am
content: Quote from: nctnico on May 30, 2021, 09:32:24 amI have thought about such calibration method, but have not yet come up with a way to do it without degrading the linearity or using more pins. Any suggestions of such topology?

author: snarkysparky
author_rank: Frequent Contributor
timestamp: May 30, 2021, 11:57:19 am
content: How about the full wilson current mirror. It seems more accurate with higher impedance and less dependent on transistor matching.https://circuitdigest.com/tutorial/current-mirror-circuit-wilson-and-widlar-current-mirroring-techniques

author: lapi
author_rank: Contributor
timestamp: May 30, 2021, 12:21:44 pm
content: Quote from: snarkysparky on May 30, 2021, 11:57:19 amWhile (very) slightly more linear the Wilson current mirror and the three-transistor topologies reduce the output voltage swing(by about one Vbe) and the Vbe temperature coefficient doubles (to ~ 4 mV/K). Furthermore, it seems it can't be compensatedwith one resistor like for the two-transistor one.

author: radar_macgyver
author_rank: Frequent Contributor
timestamp: May 30, 2021, 01:20:10 pm
content: Quote from: lapi on May 30, 2021, 06:09:38 amSorry about that, I didn't fully read your post and parse that there's a key matrix involved, rather than just a divider.

author: nctnico
author_rank: Super Contributor
timestamp: May 30, 2021, 04:18:01 pm
content: Quote from: lapi on May 30, 2021, 09:56:48 amIMHO the discussion is rather moot because -as I wrote before- you'll get large variations in resistance due to wear & tear and moisture. One way I might consider is using dual pole switches (with make & break contact) with the switches in series but I'd do some serious math on the reliability of such a solution and not use it for more than (ballpark) 4 buttons. For real life circuits it is already not trivial to design a circuit which detects closing a single contact reliably under adverse conditions.

author: lapi
author_rank: Contributor
timestamp: May 30, 2021, 05:26:58 pm
content: Quote from: nctnico on May 30, 2021, 04:18:01 pmThat is true. Over the life, especially rubber dome key mats will experience degradation of the contacts. Typical resistances of new key mats are order of 10Ω.Manufacturing promise is sometimes only 100Ω. At the end of life time it may grow to 1 kΩ. When it gets larger one can consider the keys failed.For tactile and other metal contact switches, depending on the contact materials these resistances are two or more orders of magnitude lower.What comes to degradation of the switch performance, the more serious problem than the resistance change is mechanical wear which show in largerand longer lasting contact bounces and inability to make contact. In particular, in certain types of atmosphere, the silver contact switches tend tosulfidize and oxidize which prevents contact making. In all these cases, when the contact resistance becomes excessive, one can consider theswitch failed and it would not work reliably in digital detection either.What comes to the shown design, I ran a simulation which shows that the voltage levels shift uniformly by 40 mV when the contact resistance grows from 0 to 1 kΩ.When the level spacing is over 400 mV, that change won't affect the operation.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 30, 2021, 06:01:01 pm
content: What about a 5x6 matrix, and a 74HC595 + 74H165/166?To scan every row, send the 8 bit data to the 595. Ex. 1,2,4,8,16 to enable every row.Wait few uS to settle, send Load pulse to the 165, and read 8 bits back. You know what bits represent each key.And you have some free bits on the 595 that you can use for driving leds, buzzers... Also on the 165 that you can use to read something.The pins are still kept down, while you can escalate the design without needing more lines.- Clock- Serial in- Serial Out- Load/LatchIt's a cheap, simple solution. The 74xx parts cost pennies.But this method is way more simple that breaking your head with the analog voltages, tolerances and such.

author: nctnico
author_rank: Super Contributor
timestamp: May 30, 2021, 07:39:48 pm
content: Quote from: lapi on May 30, 2021, 05:26:58 pmBut is that also true in case there is a random resistance from 5k Ohm to 100k Ohm in parallel with the buttons?

author: PCB.Wiz
author_rank: Super Contributor
timestamp: May 30, 2021, 07:50:37 pm
content: Quote from: lapi on May 30, 2021, 09:56:48 amIt depends on what and when you calibrate.A power up calibrate will only be at start-up temperature, and you can inject some small current of 5-10% of full scale, and measure that.This assumes no buttons are pressed at that time.That's only a single point calibrate, as you still do not know the gain errors, and the matched transistor pair parts I quoted above, give about 10% error bands.Gain slope errors can be flash calibrated, but you are still left with temperature variations.

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 30, 2021, 08:08:52 pm
content: Quote from: radar_macgyver on May 29, 2021, 11:23:14 pmYes, it's commonly seen on steering wheels. Note that the number of buttons is limited. In this use case, it's not done to save a few pins on some MCU, but to save wires with a significant length from the wheel to some ECU, so that makes sense. Likewise, it could make sense if you're connecting some keyboard that is kind of far away from the controller board.Of course, these days, it's easier and almost as cheap to use a very small MCU inside the keyboard and communicate to an external unit through I2S, LVDS, CAN, whatever depending on the distance.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 30, 2021, 10:07:13 pm
content: This works nice for me. In the end I could do it with only 4 pins. And you could read up to 64 buttons by using all the 8 bits in the 595 and 165.After sending the bits to the 595, you need two Load pulses, the first one loads the data onto the 595 output, the second loads the output from the keypad into the 165.Another 8 clock pulses and you got the 8 bit data.Repeat, enabling A0, A1, A2 and A3 each time.The diodes prevent the current going back into the other buttons, so they don't affect the readings.Using the analog method, a single defective button going into short will completely disable the keypad. This way each button is completely isolated.I've repaired a lot of these push buttons. When moisture comes in, they can go down to 20ohms in off state.

author: lapi
author_rank: Contributor
timestamp: May 31, 2021, 05:51:58 am
content: These designs based on SIPO and PISO logic chips are common in devices that require large number or simultaneous detects.Take a look, for example, into various music instruments, like Novation Launchpad, Behringer RD-8, and Native Instruments Machine Mikro MK2.And in general, IO-expanders like MCP23S17 will give practically unlimited amount of pins. Also, in devices thar require multiple analoginputs both SPI or I2C connected multi-channel ADC's and cheap slave microcontrollers are used.Pardon me, but those are nothing new and the internet is full of such designs.Now, if we talk about mass production of dirt cheap devices, but which still have to work, adding those extra chips is a BOM issue.In volumes 74HC165 is only about 30% more expensive than BCV61B, both are below 10 cents, so that would probably notbe an issue either. However, SIPO requires 2 or 3 pins, analog input one. Then comes the PCB area.With these dirt cheap designs, like 40€ microwave oven, which is operated at fairly constant room temperature, a very commonapproach is to use these resistive dividers, and, as seen above, also in some automotive applications. I have seen them inTV sets, set-top boxes, VCR's, computer monitors, etc.By the nature of the non-uniform distribution of the voltages when less resistors are used, some use two or more analoginputs and even those the voltage separations for some buttons are smaller than if one could have achieved a more uniformdistribution of the voltage levels. Other solutions resort to complex arrangements of resistors of many sizes. A crown jewelof such is the 27-resistor behemoth referred to in the description of the hackaday.io project describing this current mirror approach.I know that many digital-oriented designers abhor the analog parts in the domain that naturally appears to fit the bill betterthan analog, but some times it saves a lot to be aware of analog alternatives and be able to evaluate the alternatives.You can read about these issues in many articles and books written by Bonnie Baker who has had a long career with,among others, Microchip and TI.So to recap: there is a design (presented) that circumvents the problem of connecting larger number of buttons (than about 5)to a single analog pin. The reason why there has been such limit, has been the non-linear (1/(1+r) or r/(1+r)) behavior of theresistive voltage divider design used so far. The use of constant current source (like the current mirror shown), makesit possible to use larger number of buttons with one pin. The practical limits, from simulations and experiments, appear tobe 12 buttons with 5% components and 16-20 buttons with 2% component, but as a demo, a reliable operation has beenachieved in one design having 28 buttons. This is not a suggestion that all multi-button inputs should (or worse yet, must)use this design. What is suggested, that what has been know about the use of analog inputs to read multiple buttonsis not the whole truth and there is a rather simple solution to that.I originally asked if this design has been seen before. From this small poll, it appears, perhaps not. Be it way or other, now ithas been, and it is free for everyone to use. Also this public exposure of the design, hopefully, will prevent anyone from patenting it.I want to thank all who have been participating into this interesting discussion, in particular for some good suggestionsabout the possible weak points.

author: lapi
author_rank: Contributor
timestamp: May 31, 2021, 08:02:38 am
content: Quote from: DavidAlfa on May 30, 2021, 10:07:13 pmWhy not use ADP5589 and be done with it? Those and other similar chips used to be used in smart phones back when touch screen was not yet the only king of the hill. Cheap, less PCB, and faster to manufacture than arrays of diodes and pull up/down resistors, multiple chips etc. Only 2 pins needed.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 31, 2021, 08:50:51 am
content: For sure, there are a lot of specific options. I was bored and had to do something.

author: lapi
author_rank: Contributor
timestamp: May 31, 2021, 01:37:13 pm
content: As good reason as any!That's the way to go, my friend!

author: daqq
author_rank: Super Contributor
timestamp: May 31, 2021, 02:17:09 pm
content: See:https://www.daqq.eu/?p=1333for another way to approach the problem. You could in theory combine yours and this one to achieve a LOT of button readouts with just a few pins.

author: nctnico
author_rank: Super Contributor
timestamp: May 31, 2021, 03:17:46 pm
content: Quote from: lapi on May 31, 2021, 08:02:38 amActually... I like Daqq's idea of using a matrix of resistors and your touchscreen remark made me think of another option: why not treat the keypad as points of a touch-screen and connect them this way? You apply an X voltage and Y voltage and from there you calculate which button is pressed. The advantage is that it can deal with a varying contact resistance. Resistive touchscreen chip are likely to be found cheap and several of them offer interrupt capabilities as well.

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 31, 2021, 03:34:55 pm
content: But as mentioned earlier, what is the point really except in very niche applications these days?Locally (next to the "keys") using a cheap MCU with enough IOs will almost always be cheaper in the end, and most likely more robust (because it'll be more immune to noise, temperature, and have fewer parts, which usually means lower probability of failure...)

author: lapi
author_rank: Contributor
timestamp: May 31, 2021, 03:44:42 pm
content: Quote from: nctnico on May 31, 2021, 03:17:46 pmThe simple answer is: because of that one-pin budget.But I like this experiment by daqq. Reminds me of the random resistor network simulations I did for my master's thesis (https://doi.org/10.1016/0925-4005(93)85203-M) as a part of ceramic sensor conductivity studies.

author: daqq
author_rank: Super Contributor
timestamp: June 02, 2021, 06:58:32 am
content: Quote from: lapi on May 31, 2021, 03:44:42 pmThe problem for low cost devices would be the math needed to actually solve the measurement - it's fairly memory and computational speed hungry. I'm sure that middle and higher ARMs could do quickly, not sure about lesser devices.