ADC for 8 Buttons

Thread Starter

harveyf2801

Joined Nov 12, 2021
5
Hi folks, for a uni project I’m creating a synth using an Arduino. I want to have at a minimum 8 buttons for an input but I’m restricted to using only 1 or 2 input pins. Therefore I wanted to use an ADC to convert the input to binary values.

I wanted to use no IC’s if possible so tried using an R2R DAC in reverse as an ADC but the issue I had was the buttons were floating and not grounded when open. I’ve tried using 10ohm resisters on each button which works but increases the load. However using too high resister values ruins the structure of the R2R

If anyone could help me with a schematic/solution it would be much appreciated.
 

sagor

Joined Mar 10, 2019
674
A typical solution is to put 9 equal resistors between the +5V supply and GND (or 3.3V supply). At each junction of the resistors, you put in a switch wired to the ADC pin. A high value resistor to +5v to the ADC pin holds it high, typically 100k. the 9 equal resistors are lower values, like 1k (9 in series - 9k total). When a button is pressed, the ADC reading will change to one of the 8 step values between 5V and GND. Your software looks for a change from the +5V pullup value, and when it sees a different voltage, it calculates which of the 8 buttons was pressed based on the voltage reading.
 

KeithWalker

Joined Jul 10, 2017
2,299
If you only have two input pins available, you are limited to four binary numbers:
00
10
01
11
You will need a minimum of 3 input pins to get eight binary numbers:
000
100
010
110
001
101
110
111
It will only work if you only push one button at a time.
 

ronsimpson

Joined Oct 7, 2019
2,005
At first I thought to take out the top resistor like this. Then I realized that some one would push the top and bottom button at the same time and short out the power supply. So leave in the top resistor.
1636768584839.png
 

Thread Starter

harveyf2801

Joined Nov 12, 2021
5
I would ideally like to be able to press more than one buttons at the same time, therefore I'm pretty sure a potential divider won’t work
 

Thread Starter

harveyf2801

Joined Nov 12, 2021
5
Just to give an example, if all buttons are pressed the binary value I would like would be 11111111 and each bit represents a button in the keyboard… so if the first and last button is pressed then 10000001 would be the output.
 

ericgibbs

Joined Jan 29, 2010
15,347
hi Harvey,
Using the pins as Analog will not be suitable for that 0 to 255 possible coding.

Program the 6 Analog as digital inputs, also 2 other pins as digital inputs.
Gives 0 to 255 codes.
E
 

Thread Starter

harveyf2801

Joined Nov 12, 2021
5
1636799218806.png
So I was planning on using switches in a similar way to this to convert switch values to binary values i.e if the first and last switches are on, the output would be 1001 in this example. The issue I have is that I’m using switches with SPST rather than SPDT, so I can’t switch between the refference and ground, and the value floats when the switch is open. How can I modify the above to work with a SPST switch which doesn’t toggle between ground and the reference.
 
Last edited:

ericgibbs

Joined Jan 29, 2010
15,347
hi H,
Think about the 256 discrete levels for a possible 1024 ADC counts
1024/256 = 4 ADC counts per combination that the program has to detect.

ie: approx 19mV voltage difference per combination.
 

Ian0

Joined Aug 7, 2020
4,856
You can read 9 states with two pins, provided that:
1) You have double pole switches
2) Your inputs have configurable pull-ups and pull-downs.

Pole 1 of switches 1, 4 and 7 connect to V-
Pole 1 of switches 3, 6 and 9 connect to V+
Pole 2 of switches 1, 2 and 3 connect to V-
Pole 2 of switches 6, 7 and 9 connect to V+
Set both pull-ups ON (both pull-downs OFF) and read the two inputs.
Then set both pull-downs ON (both pull-ups OFF) and read the two inputs.

If you get zero in both cases on pole 1, it is switch 1, 4 or 7
If you get ones in both cases on pole 1, it is switch 3, 6 or 9
If you get a one and a zero on pole 1, it is switch 2, 5 or 8.

So you can work out which switch is pressed.

[Edit] you'll need some current limiting resistors in case two switches are pressed at the same time.
 
Last edited:

Ian0

Joined Aug 7, 2020
4,856
If you want to detect more than one button pressed at a time, this will work:
Untitled 1.png
The PNP transistor and R6 just make a constant current source so that I.(R4+R5) = half the supply voltage, so if you don't happen to have convenient 5V and 3.3V supplies, you can derive a constant current source some other way.
In theory, you can interface as many resistors as the number of bits of your A/D. In practice you can't, because of resistor and constant-current source tolerances. 4 bits is a good place to stop. You can therefore interface 8 buttons on 2 inputs.

If all your switches have a common terminals, then this will work instead:Untitled 2.png
[Edit] wrong value for R6 - should be 11k
 
Last edited:
Top