At times I feel short of digital pins on the Arduino to handle multiple button inputs. Here is an easy way to use 1 analog pin to handle many input buttons. The way it works is very straightforward: use a resistor network as voltage dividers, and then let each button feed a different voltage to the analog pin. Thus by detecting the voltage we can tell which button has been pressed.
Schematic:
Download the corresponding Arduino sketch code.
As a downside, it cannot handle simultaneous button presses. To do that, one could potentially use resistors at doubly increasing resistance (1K, 2K, 4K, 8K...). Hence by checking the detected voltage, we should be able to tell which buttons are pressed simultaneously.
could this be used for 8 buttons??
ReplyDeletehow will the program behave if more than one button is pressed at the same time?
it looks like it will take the first one it sees
is this correct?
very good idea not only to save pins but to save on hardware as you can effectively daisy chain each button. i.e. for a game show buzzer system
can you include a picture of the wiring?
ReplyDeleteYes, I believe you can extend the circuit to support 8 buttons.
ReplyDeleteWhen you press two buttons together, say B1 and B2 together, it basically shorts resistor R2, so you should get a different voltage reading on the analog pin. In fact, it may happen to work out that every pair of buttons correspond to a different voltage reading. If that's the case, then you can simply set different threshold in your code to detect multiple button inputs. Need to check if this is true or not.
I'm having a hard time understanding this can you please post a image showing the wiring? Thanks
ReplyDelete