"Tricks" to connect multiple push switches with minimum i/o usage. - Page 1
Forum section: electronics / general

author: firewalker
author_rank: Super Contributor
timestamp: March 04, 2011, 10:18:45 am
content: What "tricks" do you use to connect multiple push switches with minimum i/o usage?Lets say you have to connect 10 push switches but you don't to spare to many i/o ports. I have seen some people implementing an r-2r ladder and pushing the output to the mcu's analog input. After that it's all software.

author: Jimmy
author_rank: Regular Contributor
timestamp: March 04, 2011, 10:49:54 am
content: You can wire the switches in a matrix to save inputsHow many switchs and how many inputs

author: firewalker
author_rank: Super Contributor
timestamp: March 04, 2011, 10:54:25 am
content: Not a particular project. I just saw the r-2r method and opened this discussion.

author: Jimmy
author_rank: Regular Contributor
timestamp: March 04, 2011, 11:13:46 am
content: CoolTheir are many different options depending on the operation of deviceTo extent io you could useRS232 1 pin simplexI2C device 2 pinDallas 1 Wire 1 pinJohnson counter 2 pinSPI 4 pin

author: apex
author_rank: Regular Contributor
timestamp: March 04, 2011, 02:47:39 pm
content: If you add a diode to each switch, you could charlieplex them.Or add a 555 with a different frequency to each one and then measure the frequency as seen on hackadayapex

author: Jon Chandler
author_rank: Frequent Contributor
timestamp: March 04, 2011, 02:48:16 pm
content: I've used the R2R ladder approach to read 10 port pins with good results. One analog input reads up to 10 switches with only a small investment in hardware. Here's an article on how I do it.An interesting approach from EDN is shown in:Read multiple switches and a potentiometer setting with one microcontroller input pinNote that in the illustration here, the wire from the pot wiper is connected to the wire segment between the switches where it crosses.A limitation of many methods of reading multiple switches is the case where more than 1 switch is pressed simultaneously. You may get an erroneous reading or only read one of the switches.Jon

author: Zero999
author_rank: Super Contributor
timestamp: March 04, 2011, 06:11:26 pm
content: http://electronicdesign.com/article/analog-and-mixed-signal/novel-switch-interface-scheme-reduces-microprocess.aspxhttp://en.wikipedia.org/wiki/Charlieplexing#Input_data_multiplexing

author: Mechatrommer
author_rank: Super Contributor
timestamp: March 04, 2011, 08:05:10 pm
content: excellent Hero! i got a good friend though, he suggested to use abundant of pin count of 32bit micro, no need charlie. ymmv.

author: Zero999
author_rank: Super Contributor
timestamp: March 04, 2011, 08:48:11 pm
content: You're right. . .How much more would the extra pins cost?What's the extra cost of a larger MCU with more pins?

author: Psi
author_rank: Super Contributor
timestamp: March 04, 2011, 09:59:22 pm
content: Quite often when you decide against the ideal solution it comes back to bite you in the ass later on.

author: dimlow
author_rank: Frequent Contributor
timestamp: March 04, 2011, 10:00:22 pm
content: I have used the ADC method is works well, i put four buttons on one pin. just a few resistors needed.

author: RayJones
author_rank: Frequent Contributor
timestamp: March 05, 2011, 06:43:50 am
content: The ADC method is also popular for alarm sensor inputs.You can detect 4 states with appropriate bias resistors:Open Circuit (tampered wiring)Normal open (sensor is active)Normal closed (sensor is not active)Short Circuit (tampered wiring)

author: Mechatrommer
author_rank: Super Contributor
timestamp: March 05, 2011, 11:33:51 am
content: Quote from: Hero999 on March 04, 2011, 08:48:11 pmwell my good friend said, it only cost a few cents more. but thats just my friend's saying, not me. i prefer charlie. it keeps my brain working

author: Bored@Work
author_rank: Super Contributor
timestamp: March 05, 2011, 11:59:29 am
content: Quote from: Psi on March 04, 2011, 09:59:22 pmNo, when you grow up you figure out there is usually no ideal solution and you always have to compromises.

author: Zero999
author_rank: Super Contributor
timestamp: March 06, 2011, 10:07:54 am
content: Quote from: Mechatrommer on March 05, 2011, 11:33:51 amIt could also be useful if the switches are on another PCB and connected via a cable. There again it might also work out more economical to put a cheap PIC, e.g. PIC16F54/57 with loads of pins on the PCB containing the switches and transmit the data serially.

author: Jon Chandler
author_rank: Frequent Contributor
timestamp: March 06, 2011, 01:28:33 pm
content: Quote from: Hero999 on March 06, 2011, 10:07:54 amIt seems like there's a lot of resistance to using the analog R2R method - no pun intendedIt works well and it's ideal for a remote keypad application as only 3 wires (maybe only 2) are required for the interface. Testing for which switch is pressed is pretty simple, with a small range of voltage representing each switch. For a reasonable number of keys, if 1% resistors are used, no calibration is required, and if all the resistors are on one circuit board exposed to the same environment (i.e., the same temperature) there won't be any issues with drift. If the ladder is powered from Vdd and the ADC reference is Vdd, there's not even a problem if supply voltage varies.A quick search on Digikey shows that 1% resistors are about 4 cents a piece, so a ten-key array would cost about 50 cents over the cost of the switches, be interfaced with 3 wires over a distance, use 1 port pin and be simple to program. No complexity of using a I2C port expander nor the wiring complexity of Charlieplexing. This really is a simple, effective solution to reading multiple keys as long as only reading one key at a time is needed.

author: Mechatrommer
author_rank: Super Contributor
timestamp: March 06, 2011, 03:48:19 pm
content: what i learn so far:r2r ladder:pro: only one adc needed for 4 switches (maybe up to 6, but i dont think stability is guaranteed for 8 switches)con: need an adc capable mcu, and adc reading is not as fast as digital input, (1), cannot (2), a bit (3)charlieplexing:pro: n*(n-1) switches for n pins (beneficial for larger n>2), (2)cons: need diodes and (3)high pin count mcu:pro: no need (1), just direct register read, hence fast. (2), less (3), more room for processing power/features for higher end mcu.con: more expensive mcu (by just a few cents or dollars).(1) extra programming to decode(2) read simultaneously pressed switches correctly(3) complexity in circuitrymy 2cnts learning.

author: Zero999
author_rank: Super Contributor
timestamp: March 06, 2011, 04:12:43 pm
content: Here's how to use 4017 to enable 100 switches to interface with an MCU using just three pins.

author: Mechatrommer
author_rank: Super Contributor
timestamp: March 06, 2011, 06:21:26 pm
content: ok let me add...4017s:pro: unlimited switches, 3 pin mcu only.con: you need 4017s

author: Zero999
author_rank: Super Contributor
timestamp: March 06, 2011, 07:30:05 pm
content: Buy they're so cheap as are the jelly bean transistors - I couldn't find a cheaper MCU with enough pins.

author: abbtech
author_rank: Contributor
timestamp: March 08, 2011, 09:35:11 am
content: You could use some 74HS165 chips, costs you a few inputs but you then have unlimited button count possibilities.

author: smackaay
author_rank: Regular Contributor
timestamp: March 13, 2011, 10:46:26 pm
content: My favorite solution has always been to use a separate MCU with no frills and gimmicks like a pic16f505. You can use a matrix for the keys and use internal values to produce a debouncing algorithm that acts to produce a threshold for the switches. you can then use a serial out interface and tell the master MCU which keys have been pressed and un-pressed.

author: bilko
author_rank: Frequent Contributor
timestamp: March 31, 2011, 11:34:45 pm
content: Use a $3 pc keyboard and decode the serial stream. Strip the keys and use the matrix

author: ivan747
author_rank: Super Contributor
timestamp: April 11, 2011, 07:42:15 pm
content: Check this switch matrix out:http://ww1.microchip.com/downloads/en/AppNotes/01081a.pdfOf course you can use it directly with a PIC.