How to access 5 buttons through 1 Arduino input

 by riaancornelius
Button Project on breadboard.jpg
Using this method, I'll show you how you can access 5 (or even more) inputs through 1 Arduino pin. These buttons will only be read correctly if only one is pushed at any time though.

As we go through it I'll explain whatever background info you need to know, so as long as you can blink a button, read a switch and read an analog input, you'll be fine. If you can't do any of these, I'll point you in the right direction in the relevant steps as well.
 
Remove these adsRemove these ads by Signing Up
UselessBagOfMostlyWater says: Apr 22, 2013. 4:06 AM
Thanks for posting this. I had the same idea of using resistors in series before getting here and was glad to see your diagram. I'm wiring an old-school (think industrial arcade game) joystick. In its guts, when you push the lever one way, it causes physical closure of a switch (push up joins two metal contacts on the right; push down and it closes contacts on the left). So really it's just four switches, and physically it's capable of having one or two switches closed at once, but no more.

I'm currently supplying the Arduino's 5V to it, and taking meter readings (disconnected from Arduino) and actual analogRead values (connected to Arduino) to correlate and get an idea of what I'll see. So far it looks logarithmic, suggesting a good spread of resistor values would be 1k, 10k, 37k, and 100k.

At the same time, I have to look up the combined parallel resistances to see what kind of spread they give... Rtotal = (R1*R2)/(R1+R2).

The logarithmic scale, if borne out, suggests higher resistances will give me less variance in analogRead values. Example: choosing R1=220 ohms, and R2=470 ohms, my parallel resistance is 149.86 ohms. But, at that point in the log scale, I expect my analogRead values to be around 220ohms=1002, 470ohms=978, 150ohms=1009. So that split between 1002 and 1009 isn't comfortable enough to account for resistor quality differences.

Instead, if I choose larger values, I hope to get a wider spread of analogRead values to work with.
Rob311 says: Apr 8, 2013. 8:47 AM
The all time best use of this trick is on RC controllers (Car, plane, boat). They take one channel and on the TX side, they disconnect it from the stick (which is a potentiometer). They then add 5 or 6 buttons with varying resistors like you did. On the RX side, they plug it into a micro controller that can read PWM (i've done it with a Basic Stamp 2, but an Arduino would work). Now they have taken that 1 channel on the transmitter, and made it into a bunch of buttons that can control anything you want with an arduino.

Sometimes its nice to have the responsiveness of direct PWM on some channels, and 1 or 2 channels of other "actions" at the receiver.
Bam Boy says: Mar 9, 2013. 4:28 AM
Hi,

I was just trying to implement your instructable in my project. I connenced the buttons according to your sketch. I used 22K, 33K and 47K resistors and 100K as a pull down resistor.

Now the buttons are working and like whn I press them, I can see the values change in the serial monitor. However, when it comes to calculating the range, the value doesnt fluctuate. Its the same value repeating. Like for button 1, the range is 1023. for button 2 the range is, 838-839, for button 3 it is, 658-659 and for button 4 it is, 503-504.

So, I am confused now. Like should I put down these ranges in my sketch or what?

Can you please help me out :/
Dyte says: Feb 4, 2012. 11:23 AM
Thanks a lot for this, helped me a great deal.
OCPik4chu says: Apr 14, 2011. 1:12 PM
Wouldnt it be possible to do something like this with a shift register or similar and eliminate the possible conflicts of the resistor values? (not to mention give you more buttons i think)
LordMekk says: Oct 28, 2009. 1:11 PM
 If you kept the resistor values increase by the power of two you could do binary addition and subtraction to get what buttons are pressed.  This is done in computer programing all the time
riaancornelius (author) in reply to LordMekkOct 28, 2009. 3:14 PM
I thought so too, and initially chose my resistor values that way, but the fact that the resistors are in parallel means that the binary doesn't hold when you connect multiples. For example, if I had 10K, 20K, 40K and 80K resistors on buttons 1, 2, 3 and 4 (To make the maths simpler), I get the following resistances when pressing multiple buttons:
1+2: 6.66K
1+3: 8K
1+4: 8.88K
1+2+3: 5.7K

As you can see, the multiple button presses result in resistances that are all very close together (and close to the button 1 resistance).

I'm wondering if there wouldn't be some way of rewiring the circuit to get the resistors in series when multiple buttons are pressed... Off the top of my head, I don't see an easy way, but it is after midnight here.
bugQ in reply to riaancorneliusAug 12, 2010. 7:25 AM
Your buttons are double-throw switches, right? You should be able to put them in series just fine. All you have to do is short the "off" contact of one switch to the common of the next one, so that there's no resistance when it's not being pressed. Not sure how best to debounce it, though. Anyway, sorry for replying to such an old comment. I have a similar project where this could be verrry useful, though...
bugQ in reply to bugQAug 12, 2010. 9:44 AM
It looks something like this...maybe the resistor values have to be tweaked to make it easier to distinguish the voltage readings, but you get the idea.
4finger.jpg
kikiclint in reply to riaancorneliusOct 30, 2009. 10:10 AM
now if you calculated everything with conductances (conductance=1/resistance) they add in parallel.  That would help to do some simple addition calculations.
riaancornelius (author) in reply to kikiclintOct 30, 2009. 12:57 PM
It would make the maths simpler, but I don't see how that helps practically? 

I have to admit, I haven't worked with any electronics for a while, so maybe I'm missing something obvious, but I don't see an easy way to convert conductance into a voltage that the Arduino would be able to measure.
jonnybeats says: Jul 19, 2010. 1:11 PM
Nice tutorial and good method! I'm a bit new to resistance and the like, i need to run eight buttons, does anyone have any idea what resistors to use and what the output values would be. Cheers!
aolinger says: Oct 29, 2009. 6:08 AM
Yes, please post the rest of your project plans! Multiplexing LEDS isn't that easy and you demonstrated teaching skills with this project :-).

Thanx a lot,

Alex
riaancornelius (author) in reply to aolingerOct 29, 2009. 10:44 AM
Thanks Alex. I'm working on the charlieplexed LED's now. Bit of a headache so far... Finally have it wired up properly, now I just need to figure out the code...
inventorjack says: Oct 28, 2009. 1:39 PM
I don't think this would work so well in industry, due to the fact that resistor values can change over long periods of time, but this is an excellent and cheap technique for personal projects. Thanks for sharing how you simplified the process. I know math can be really tedious for some, and your process cuts all that out. Good instructable!
riaancornelius (author) in reply to inventorjackOct 28, 2009. 3:21 PM
Thanks :) 

I guess you could deal with that in code. If you had button 1 tied to the supply voltage with no resistor, you know its value would be constant. So use button one to start a calibration process where you press each button in turn and the software records the new values for each button.

Of course then you need some form of memory to store the new values and it all starts getting more complicated again. Still, you could make it work if you were desperate enough :)

Of course, for a commercial venture, I would just use a micro-controller with more IO lines.
Bongmaster says: Oct 28, 2009. 12:01 PM
pretty clever that :)
riaancornelius (author) in reply to BongmasterOct 28, 2009. 12:10 PM
Thanks. You know what they say about necessity :)
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!