I am thinking about building a circuit that is literally going to take 11 of the 14 digital pins of an UNO
to communicate with an SD card and an external EEPROM chip (EEPROM programmer).
Using 4 shift registers and the SD card, I will have 1 digital pin left. Well, I'm also reserving pins 0/1 for serial communication.
Anyway, I need to read some external buttons and I have the 6 analog pins that I haven't touched.
I will only need about 4-5 push buttons in my design so they should fit nicely on A0+.
So, my question is, will I run into any issues doing this? Or, are the analog inputs better left for true analog reading?
Thanks.
-
1Possible duplicate: arduino.stackexchange.com/questions/4011/…Anonymous Penguin– Anonymous Penguin01/28/2015 02:57:58Commented Jan 28, 2015 at 2:57
2 Answers 2
The Analog pins are essentially identical in functionality to digital pins when used as digital I/O.
If desired you could "be clever" and use an analog input in analog mode to read multiple keys using one pin. eg using 10k, 22k, 39k, 82k, 150k in series with 5 buttons from V+ to pin and a say 4k7 to ground would result in 5 voltages which were easily distinguished using an analog read. This is a very very old technique indeed and allows far more than 5 keys on one input pin. THere are limitations which can be discussed if required.
R1 ... R5 correspond to buttons 1...5
Vout = V+ x Rb / (Rb + Rbutton)
Example only
Rb = 39k
R1 = 10k
R2 = 22k
R3 = 39k
R4 = 82k
R5 = 150k
Gives
The % of V+ in column 2 when a button is pressed.
The voltage in column 3 with V+ = 5V
Delta-V is the difference between that key and the one below - very ample clearance.
enter image description here
enter image description here
-
Awesome answer. Thanks! BTW, I didn't even think about using the analog pins to read multiple buttons that way. Thanks for the tip too.cbmeeks– cbmeeks01/28/2015 13:15:01Commented Jan 28, 2015 at 13:15
-
Can this method be extended to detect multiple buttons pressed at the same time?Paolo Zanchi– Paolo Zanchi01/28/2015 13:51:15Commented Jan 28, 2015 at 13:51
-
2@LoganBlades Yes - within limits. If you think of button resistors as ~= injecting current into the lower resistor you'll see I have chosen values that are in an approximate binary ratio subject to std E12 values. ie I aimed at 10 20 40 80 160 and then took closest E12 value. So if you take the inverses it shows ABOUT how much they will pull a low value resistor up by - I say low value as that better approximates a current source. So contributions are in inverse R order of about 16 8 4 2 1 current units. So if you press B1 + B3 you get 20 current units, B3 + B5 = 3 etc. ...Russell McMahon– Russell McMahon01/28/2015 18:22:34Commented Jan 28, 2015 at 18:22
-
2... The 3CU of B3 + B5 lies between the 4 CU of B3 and the 2 CU of B4 so you need enough resolution to distinguish between them. If you used the resistors to control a "proper" current source and binary weight the resistors you get 2N^N levels for N buttons keys. So 8 buttons gives 256 levels AND with a 10 bit ADC vyou notionally have 2 bits between each level. That would almost certainly be tighter than you could easily manage in practice - you'd need 0.1% resistors and a 0.1% accurate current source. 6 buttons is probably doable. Easily experimented with using eg an Excel spreadsheet.Russell McMahon– Russell McMahon01/28/2015 18:27:28Commented Jan 28, 2015 at 18:27
If you have no use for analog pins there is no problem. A5/A6 are used for I2C (on the Uno).