Arduino Forum

Using Arduino => Project Guidance => Topic started by: arduino_314 on Dec 06, 2016, 09:25 am

Title: Modified R-2R ladder for input buttons
Post by: arduino_314 on Dec 06, 2016, 09:25 am
I have a need to connect 8 buttons to the ATmega328p (aka Arduino Uno bare clone), however only 1 pin is available.

Using of some dedicated I/O extender is out of question, as it is expensive and as well require more than 1 available pin to connect.

Thinking about simpler solution, I know exists some Arduino LCD shield with about the same buttons which use some resistance ladder, however with that is really difficult to detect multiple button connection...

Then I started to think about R-2R ladder. It seems to me ideal solution, except floating bits must be solved as button must be between VCC and input pin. In that case, when all except one button is pressed there will be voltage level which is not expected...

The only obvious way to solve that is to have additional pull-down resistor, but then again voltage level slightly drifts from expected values...

Long story short, I have a feeling that I'm "reinventing wheel". Is there already some mathematical model have in consideration additional pull-down resistors or I would have to make my own? I have not found any relevant study about on internet.



Title: Re: Modified R-2R ladder for input buttons
Post by: DrDiettrich on Dec 06, 2016, 08:55 pm
I don't think that you'll find a mathematical solution, that can be implemented with simple electronic circuits.

If you want to monitor all inputs at the same time, you may use SPDT switches with a R-2R ladder, which allow to toggle between Vcc and Gnd. Alternatively each SPST switch can toggle a logic gate (inverter...), to provide the right signal levels to the ladder.

For a single input you can use a simple R ladder, with the switches connecting to Vcc. Then the output voltage of that voltage divider depends solely on the closed switch closest to the bottom resistor (to Gnd).

Another approach were multiple (switchable) current sources, with the currents summed up on a resistor to form a voltage. The current sources can be approximated by resistors of values 2^x, e.g. 1k, 2k, 4k...256k. An opamp may be required for the conversion from current into a voltage, with a zero input impedance.
Title: Re: Modified R-2R ladder for input buttons
Post by: danielmario on Dec 06, 2016, 10:53 pm
The R-2R ladder should work. Put a buffer between the switches (pulled to Vcc) and ladder inputs, then analogRead the ladder output. The 74hc244 logic IC has exactly 8 buffers and costs around $0.50.
Title: Re: Modified R-2R ladder for input buttons
Post by: ballscrewbob on Dec 06, 2016, 11:43 pm
By careful selection of the resistors its should be more than possible using your own suggestion to use a section of an LCD shield code.

Included a link to such a sketch so you can see how its done.
This already reads 5 buttons so another 3 would be quite simple.

LINK HERE (https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)#Example_use_of_LiquidCrystal_library).

It also includes a schematic from which to deduce your own resistance ladder.

I have used a similar approach in an industrial application and the only thing I changed slightly was to add a better debounce function with no loss of responsiveness.

If you strip out the unwanted LCD sections you are left with a quite nice sketch to add in your own uses.

A simple search of the forum itself also turns up a plethora of similar sketches and answers one such HERE (http://forum.arduino.cc/index.php?topic=8558.0)

Or a slightly better method HERE (http://playground.arduino.cc/Code/OneWireKeyPad) with room to spare for additional buttons.


Title: Re: Modified R-2R ladder for input buttons
Post by: Wawa on Dec 07, 2016, 01:03 am
There are 256 possible combinations with 8 buttons if you want multiple button detection.
A practical problem with Arduino's 10-bit A/D, because you likely would need perfect switches that are always near zero ohms when closed, and 0.1% resistors for the ladder.
What else is using up all the pins of the 328. Maybe you could change something there.
Leo..
Title: Re: Modified R-2R ladder for input buttons
Post by: dlloyd on Dec 07, 2016, 01:43 am
If you only need to operate one button at a time ...

(http://i.imgur.com/BP20wpg.png)
Button Volts
B1     1.0
B2     1.5
B3     2.0
B4     2.5
B5     3.0
B6     3.5
B7     4.0
B8     4.5
Title: Re: Modified R-2R ladder for input buttons
Post by: arduino_314 on Dec 07, 2016, 08:39 am
Thanks to all.

The R-2R ladder should work. Put a buffer between the switches (pulled to Vcc) and ladder inputs, then analogRead the ladder output. The 74hc244 logic IC has exactly 8 buffers and costs around $0.50.
The idea with non inverting buffer/line driver sounds perfect. This should handle floating input pins and plain R-2R ladder can be used allowing predictable voltage level as output with multiple push buttons support.

Thank you very much, Daniel.
Title: Re: Modified R-2R ladder for input buttons
Post by: dlloyd on Dec 07, 2016, 08:42 am
Quote
Using of some dedicated I/O extender is out of question, as it is expensive and as well require more than 1 available pin to connect.
$1.20, need 2 available pins, gain 14 I/O. (http://www.digikey.com/product-detail/en/microchip-technology/MCP23017-E-SP/MCP23017-E-SP-ND/894272)  datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/20001952C.pdf)

Check the forum here for examples and library or check adafruit.

Haven't seen any success using 8 buttons with R2R ladder approach (4 or 5 seem to be the max).

EDIT:

With 8 buttons (8-bit R2R ladder) you'll need to distinguish what button(s) are pressed to within 19.5mV steps or just 4 ADC code (good luck with that).

Consider the 5V regulator accuracy/stability/regulation for AREF, consider leakage through buttons, wiring, solder connections, crosstalk, noise pickup, resistor matching tolerance, temperature stability, accuracy drift, aging effects, etc.
Title: Re: Modified R-2R ladder for input buttons
Post by: danielmario on Dec 08, 2016, 12:17 am
Be welcome, and remember to use better precision resistors for the MSbits.
Title: Re: Modified R-2R ladder for input buttons
Post by: DrDiettrich on Dec 08, 2016, 04:03 am
When AREF equals Vcc, no errors can result from varying supply voltage in the R-2R ladder.

Also the choice of the resistors is not critical. It's normally sufficient to pick resistors from the same charge, then also 10% resistors have almost the same parameters. Recently I built a R-2R ladder from 5% 270 Ohm SMD resistors, and it works perfectly in my DAC.

Really critical is the button connection. I'd suggest to place the drivers and ladder near the ADC, to keep the output voltage free of external noise.
Title: Re: Modified R-2R ladder for input buttons
Post by: dlloyd on Dec 08, 2016, 04:43 am
For each more significant bit, the resistor needs to be proportionally more accurate. A DAC application could be more tolerant in reading jumps in the LSB, however for the OPs application, this would cause false readings of which button(s) were pressed. Many combinations could never be read correctly.

"Consider an 8-bit ladder specified so that there is solely a ±1.0% absolute tolerance on the resistors. The full resolution of the ladder may not be realized. Ratios within the network could be as poor as almost ±2.0% yielding a ladder, which, although manufactured with 8 bits, may only provide accurate outputs to the 6th or 7th bit."  - app note, pg5 (http://www.ttelectronicsresistors.com/pdf/application_notes/LADDERNETWORKS.pdf)
Title: Re: Modified R-2R ladder for input buttons
Post by: arduino_314 on Dec 08, 2016, 08:15 am
Well, I do not need the most of these additional info, but for other may be of interest.

When I practice SMD soldering by plain iron station, first board I have made is 16-bit DAC (overkill for this purpose, but anyway I have planned it for experiments with playing WAV). I have read all related info before soldering about R-2R ladder, bought more than needed 1% 0805 resistors and before soldering tested each resistor value and pickup the  closest ones - final results are in the specified range.

Thus, there is no question about precision of R-2R ladder. And there will be no need to cover all possible combinations (2^8), max 2 buttons push should be supported at the time.

I do not think it would be any problem with. Resolution of 10-bit ADC with 5V ref. is 5 / (2^10-1) = 4,887mV, which is 4 times more than need and certainly that is not 1%, but 400% total allowed error. With read voltage, it is trivial to determinate precisely which button combination was pressed.

Thanks to Daniel, this is now trivial.
Title: Re: Modified R-2R ladder for input buttons
Post by: dlloyd on Dec 08, 2016, 03:06 pm
Need accuracy of 8-bit ladder to 1/2 LSB (9.75mV). 10-bit ADC precision will not help if the accuracy of the 8-bit ladder is beyond 0.5 LSB as there'll be missing 8-bit codes.

The MSB resistor effects the output voltage by 5/2 = 2.500V. For ±1% tolerance, the effect is ±25mV = 2.5 LSB

Need to pick MSB resistor to achieve ±0.25LSB (0.5 LSB range) which is ±4.88mV. Therefore, the MSB resistor tolerance needs to be better than ±0.2%

I suggest testing and recording all 8-bit readings. Then calculate step change (in LSB) and error (%) for each reading.
Title: Re: Modified R-2R ladder for input buttons
Post by: arduino_314 on Dec 08, 2016, 06:24 pm
If we have all 1% resistors and declared value of 1K (using 2 in series to make 2R) and measuring each one choose these are between 990 and 992 ohm, what is then total expected error of the 8-bit ladder? With my calculation and real test - very close to 0.

For a mostly "perfect" R-2R ladder, it is the most important to have equal value resistance, not in which sale category exists. Of course, material from which resistor is made should be quality, not to fail due soldering and durability.

Thus, for less money and a bit preparation before soldering, equally good DAC can be made comparing to extremely expensive 0% resistors.

That is my whole story when I built my own 16-bit DAC.
Title: Re: Modified R-2R ladder for input buttons
Post by: dlloyd on Dec 08, 2016, 06:42 pm
Yes, very close to 0 error (±0.1%) ... no missing 8-bit codes. Didn't realize you were picking values to within 0.2% spread. Just need to sort in batches 990Ω-992Ω, 993Ω-994Ω, etc for each DAC.
Title: Re: Modified R-2R ladder for input buttons
Post by: DrDiettrich on Dec 08, 2016, 06:48 pm
Now I've tested my R-2R ladder with 8 bit and ADC, and I admit that the resistors should have much less tolerance.

Thanks for encouraging me to adjust my mind by a real-life test :-)
Title: Re: Modified R-2R ladder for input buttons
Post by: arduino_314 on Dec 08, 2016, 07:59 pm
Well, I have deliberately chose three values in the row, it is much better economy - not to waste money if build one or two DAC.

Simply:

R = 991, 2R = (R-1) + (R+1) = 990 + 992

Error is then in theory 0 and negligible in practice.
Title: Re: Modified R-2R ladder for input buttons
Post by: Wawa on Dec 08, 2016, 10:18 pm
There is also a "binary weighted resistor ladder".
Only one resistor per switch (plus one pull up resistor).

I think all of this is a silly exercise.
As said in post#4, you can't use cheap switches either.
Bad switch contact will make it unreliable over time.
Had to replace lots of tact switches in consumer electronics that used this principle in the eighties.

As asked before, can't you use I2C port expanders for this.
And what else is using all the pins of this Arduino.
Leo..
Title: Re: Modified R-2R ladder for input buttons
Post by: arduino_314 on Dec 09, 2016, 12:40 am
I think all of this is a silly exercise.
I think you missing several important facts:

1. This is an exercise.
2. I'm a hobbyist. I do not make and sell medical equipment, but make devices for my own personal enjoyment.
3. I'm as well retro nostalgic, I would rather make retro computer than buy and use modern tablet (which I refuse to buy, anyway).
4. I do not use switches, but plain push buttons.
5. As wrote in the first post, using the dedicated I/O expander is out of question. It cost for me almost as new ATmega328p - I would rather buy another ATmega328p.

Title: Re: Modified R-2R ladder for input buttons
Post by: Wawa on Dec 09, 2016, 02:14 am
4. I do not use switches, but plain push buttons.

5. As wrote in the first post, using the dedicated I/O expander is out of question. It cost for me almost as new ATmega328p - I would rather buy another ATmega328p.
Plain push buttons ARE (momentary) switches.

A 16-channel MCP23017 expander is ~US$1.20 including free shipping on ebay.

So what else have you connected that use all the pins of the 328.
Many devices/sensors/LCD displays also come in I2C, and can all be connected to the same two I2C pins.
Leo..
Title: Re: Modified R-2R ladder for input buttons
Post by: Wawa on Dec 09, 2016, 02:40 am
Things get a lot easier if you only push one button at the time.

http://www.technoblogy.com/show?NGM

Leo..
Title: Re: Modified R-2R ladder for input buttons
Post by: arduino_314 on Dec 09, 2016, 08:40 am
@Wawa

Why you impose your opinion? All that is already suggested.

About prices: You can buy it for $1.2, for me min price is almost 3 Euro.  The ATmega328p costs 3.3 Euro.

As well, we also have sparkfun distributers here.

For instance, you can buy this for $8.96:
https://www.sparkfun.com/products/10168
For me the price is 13.5 Euro

https://www.sparkfun.com/products/12085
For you that cost $59.36, for me around 70 Euro.

Etc, import taxes, VAT, distributors income, imported quantities... Some prices goes insane.

To order by mail, minimum fixes shipping, custom taxes and VAT exceed your price 50 times + filling paper for custom... Much more expensive and too many  complications...

Again, why you impose your opinion if  you lack many important facts I'm facing with?

I'm quite satisfied with Daniel's suggestion and that is enough for me.

Thank you for your understanding.
Title: Re: Modified R-2R ladder for input buttons
Post by: Wawa on Dec 09, 2016, 09:57 am
Sorry for doing that. I just wanted you to think more outside the box.
There might have been other solutions if you had told us why you only had one pin left.

I still doubt that eight buttons with more than one button at the same time can be done that way.
Please post a diagram of the R2R ladder with resistor values, and your experiences.
Leo..
Title: Re: Modified R-2R ladder for input buttons
Post by: dlloyd on Dec 09, 2016, 04:12 pm
I also have doubts that the pushbutton 8-bit R2R approach can be decoded reliably, for reasons previously stated and for the fact that there's no examples for this being done on the web.

One last suggestion: Use a 74HC165 (http://www.onsemi.com/pub_link/Collateral/MC74HC165A-D.PDF) input shift register to get 8 additional inputs . Daisy-chain for additional inputs.

$0.37 each qty 1
$0.20 each qty 100 (http://www.digikey.com/product-detail/en/on-semiconductor/MC74HC165ADG/MC74HC165ADGOS-ND/1479887)

Consider your costs for your time, cost and assembly of multiple surface mount resistors, extended BOM list, PCB footprint size, etc. Drastically reduce the components to place and get high reliability results.  (don't forget using a decoupling capacitor per IC).

Here's an example and code from a very reliable source: Using a 74HC165 input shift register (http://www.gammon.com.au/forum/?id=11979)

You could mark this thread (SOLVED) if your not interested in further input, but it would be beneficial for everyone if you could provide followup to your project.
Title: Re: Modified R-2R ladder for input buttons
Post by: arduino_314 on Dec 10, 2016, 03:00 am
Sorry for doing that. I just wanted you to think more outside the box.
What are you talking about? You constantly repeat the same over and over again I said I would not use and  additionally insulting!?

If that is nice practice and manners here, thank you but I do not need any help from here.

If moderators do not react or behaves the same, that is more reason to delete account and forgot about all thing.
Title: Re: Modified R-2R ladder for input buttons
Post by: sforsandeep on Sep 28, 2017, 03:27 pm
If you only need to operate one button at a time ...

(http://i.imgur.com/BP20wpg.png)
Button Volts
B1     1.0
B2     1.5
B3     2.0
B4     2.5
B5     3.0
B6     3.5
B7     4.0
B8     4.5

Sorry Just a question there... am a very beginner.. So why did you put that capacitor over there? Is that mandatory?
Title: Re: Modified R-2R ladder for input buttons
Post by: DrDiettrich on Sep 28, 2017, 04:58 pm
The capacitor helps to debounce the switches. It quickly loads from the low resistors (on top), and discharges slowly through the high (100k) resistor. It also reduces stray pick-up on long lines to the keyboard.
Title: Re: Modified R-2R ladder for input buttons
Post by: lastchancename on Sep 28, 2017, 11:54 pm
Now we're heading in the right direction.
OP Asks for advice, gets several meaningful and optional ideas to make a solution.
Move forward.