Go Down

Topic: How to read 5 buttons with only 4 I/O lines (Read 3386 times) previous topic - next topic

jacksons

Here is an interesting challenge for you:

I have only 4 digital I/O lines left on my Arduino; the others were taken up by other things.  I was wondering if there is a way, using minimal external circuitry, to read 5 buttons with these 4 I/O lines, assuming I have power and ground as well?

cdacunha71

A shift register : http://arduino.cc/en/tutorial/ShiftOut or a demux http://tronixstuff.com/2013/08/05/part-review-74hc4067-16-channel-analog-multiplexerdemultiplexer/

fungus


A shift register : http://arduino.cc/en/tutorial/ShiftOut


Maybe you mean http://www.arduino.cc/en/Tutorial/ShiftIn
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

jacksons

I know about shift registers, but i want to do it without much external circuitry.

dlloyd

#4
Jun 07, 2014, 05:47 pm Last Edit: Jun 07, 2014, 06:07 pm by dlloyd Reason: 1
If you have one spare analog in line...








   
All resistors (6) = 1K

No buttons pressed: 4V
Button 4 pressed:   3V
Button 3 pressed:   2V
Button 2 pressed:   1V
Button 1 pressed:   0V



EDIT: Missed the 5th button (would need an extra resistor).
This would provide 0.83V/button (Button 1-5 giving 0, 0.83, 1.67, 2.50, 3.33 volts, 4.17V for none pressed).
Of course, would only work if one of the spare pins has an analog input configuration available.




pico

#5
Jun 07, 2014, 06:02 pm Last Edit: Jun 07, 2014, 06:12 pm by pico Reason: 1

I know about shift registers, but i want to do it without much external circuitry.


You can read up to 7 switches with three digital pins, and up to 15 with four. This is how:

Call the pins 0 1 2

configure 0 1 2 as INPUT_PULLUP using pinMode()

call the switches A B C D E

Wire A so that it grounds 0 when pressed
Wire B so that it grounds 1
Wire C so that it grounds 0 and 1
Wire D so that it grounds 2
Wire E so that it ground 0 and 2

When you want to know what's pressed, you read all three pins, then decode the pattern (reading a pin as  HIGH = 1, as LOW = 0):

1 1 1 - no buttons pressed
0 1 1 - button A pressed
1 0 1 - button B pressed
0 0 1 - button C pressed
1 1 0 - button D pressed
0 1 0 - button E pressed
WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

jacksons

Sorreh, i have no analogs.

What if...

Code: [Select]

  dio1     dio2     dio3     dio4
   |--btn1--|---btn2--|--btn3--|
   |-------btn4-------|        |
   |-----------------btn5------|


dlloyd

#8
Jun 07, 2014, 06:30 pm Last Edit: Jun 07, 2014, 06:33 pm by dlloyd Reason: 1
As per pico's suggestion with diodes and external pull-ups installed:

Ground is common line below pushbuttons.

pico


As per pico's suggestion with diodes and external pull-ups installed:


Since he said he didn't want any extra external components, I was thinking he'd probably just want to use the internal pullups. Then, no other external components except wire.

If he doesn't want to use even wire, I give up.  :smiley-mr-green:
WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

dlloyd

Quote
If he doesn't want to use even wire, I give up.
LOL

krupski


LOL


I can read the state of up to 7 switches with 4 digital lines. No extra components needed.

Only caveat: they need to be pressed individually. Pressing more than one at a time would produce an invalid result and that data would need to be discarded.

Want to know how, or are you just testing us?
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

fungus


Here is an interesting challenge for you:


Not really.


I have only 4 digital I/O lines left on my Arduino; the others were taken up by other things.  I was wondering if there is a way, using minimal external circuitry, to read 5 buttons with these 4 I/O lines, assuming I have power and ground as well?


Do they have to be read individually (only one button pressed at a time) or can you press combinations of buttons?

One button at a time: Some sort of multiplexing.

eg. There's 15 binary combinations of four bits.

You can therefore have fifteen switches, each switch connects a combination of pins to ground. You read the combination as a binary number.

If you need to read multiple button presses in any combination then you need a shift register. There's no way around it.

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

dlloyd

#13
Jun 08, 2014, 03:48 am Last Edit: Jun 08, 2014, 04:02 am by dlloyd Reason: 1
Hello Krupski,
I hope your SPI interface project worked out ok.

Quote
I can read the state of up to 7 switches with 4 digital lines. No extra components needed.

You sure can ... 3 SPDT switches, 3 DPDT switches and 1 3PDT switch.

Quote

Only caveat: they need to be pressed individually. Pressing more than one at a time would produce an invalid result and that data would need to be discarded.

Absolutely

Quote
Want to know how, or are you just testing us?

Confused. I still find pico's comment rather humorous (and so did he with "smiley-mr-green").

As for my previous circuit suggestion, I've assumed single pole normally open push buttons. Then added external 10K pullups to provide greater signal strength and noise tolerance. The diodes are to isolate the switching of multiple lines.

krupski


Hello Krupski,
I hope your SPI interface project worked out ok.

Quote
I can read the state of up to 7 switches with 4 digital lines. No extra components needed.

You sure can ... 3 SPDT switches, 3 DPDT switches and 1 3PDT switch.

Quote

Only caveat: they need to be pressed individually. Pressing more than one at a time would produce an invalid result and that data would need to be discarded.

Absolutely

Quote
Want to know how, or are you just testing us?

Confused. I still find pico's comment rather humorous (and so did he with "smiley-mr-green").

As for my previous circuit suggestion, I've assumed single pole normally open push buttons. Then added external 10K pullups to provide greater signal strength and noise tolerance. The diodes are to isolate the switching of multiple lines.


Actually, with my design, each switch only needs to be SPST (and the Arduino running code to drive outputs and read inputs (set as INPUT_PULLUP) sequentially to determine which one was pressed.).

Oh, my SPI stuff? Got it all figured out!  :)
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Go Up