Need to connect 4 switches to only 1 port

Hello there ! I'm currently having a small issue : i made a program, wich must be able to know the state of 4 switches ( like if they are on 0010 state, or 1110 state, etc...) The problem is, i only have 2 ports free, the others being occupied by other things in my project. Is there a way to have the arduino knowing wich state the switches are ?

I was thinking of using a current divider, but i don't really know how to use them, or how to make it in that specific case.

Thank you in advance for reading and answering :)

( Apologies for bad english, fellow frenchman's speaking :D )

Do you have any analog pins available? You can use an analog input with different resistors on each switch to identify the switches with different voltages.

Of course an analog input can always be used as a digital input.

pinMode(A0, INPUT_PULLUP);
digitalRead(A0);

So, if i do that, will the program be able to make the difference betwen all the 16 possibles differents states ? The currents from each switch will just add up ?

You will have to measure resistance values for all 16 combinations. It's as if you are splitting up a potentiometer into 16 discrete steps.

If you have 2 analog inputs, I'd say use them both, 2 buttons each, it's less hardware, more reliable, and a little easier to code.

Technically a port is a group of 8, 16, or 32 pins sharing an address in the memory-map of the
microcontroller. You are talking about individual signal pins.

MarkT: You are talking about individual signal pins.

Yeah i meant pins, sorry :)

Thanks a lot for your answers, i will do that as soon as i can !

Hi,
What model Arduino are you using?

Interfacing 4 switches to an analog input is basically a Digital to Analog Converter.

google r-2r ladder dac

Tom… :slight_smile:

Thanks TomGeorge, it seems to be what i'm trying to do ! But i'm habing trouble making it... What resistors should i pick and how should i wire the whole thing ?

Hi,
Something like this;

Vref being your 5V supply, Vout to your analog input.

Use 2 x 1k in series to get 2K

Tom… :slight_smile:

Thanks a lot ! I hope this time I'll be able to do something :D

It works ! Thanks a lot, guys ! :D

Hi, Good to hear. I have given you a karma, for prompt and consise replys.

Pleasure helping mate.

Tom.. :)

Update : I just realized my switches are not what i expected : They have 3 pins, but it's a open/closed only circuit, so it can't switch between being connected to the ground and being connected to Vref... Can you help me again with this ? D:

( thanks for the karma tho )

That does make it a bit harder but not impossible.

First option, but the one with a lot of components.... Do all values x10 in the current design (10k and 20k) or even x100 (100k and 200k) but then you need to add a capacitor between the analog in and GND. And instead of connecting the switch to GND parallel the switch with a pull down to GND of 1k. Because it's an order of magnitude different than the normal resistors it will act as a hard connection to GND but will not cause a problem when the switch forces that point HIGH.

Option two, drop the nice R2R. Just use a single resistor to GND and just some different values in series with the switches to Vcc (or the other way around if you want). That way every switch will give you a different voltage but they will not be spaced as nice across 0-Vcc as with the R2R. And every combination of two switches will give you a different voltage as well. So easier on the hardware, harder on the software.

Hi, Not sure if this will help;

http://www.edn.com/design/analog/4439796/Read-multiple-switches-using-ADC

google r-2r ladder spst switch

Tom... :)

septillion: Option two, drop the nice R2R. Just use a single resistor to GND and just some different values in series with the switches to Vcc (or the other way around if you want). That way every switch will give you a different voltage but they will not be spaced as nice across 0-Vcc as with the R2R. And every combination of two switches will give you a different voltage as well. So easier on the hardware, harder on the software.

Do you think you can make a schematic of this please ? I don't really understand how to wire what you are explaining, but it sound like a good solution :)

See link of TomGeorge ;)

Yeah i looked at that link, but i didn't understand that much

Then i made what i found on this picture ( i searched "r-2r ladder spst switch" ) :

https://i.stack.imgur.com/ZgUkn.png

It seems to be working, but i have a strange set of values :

Number in decimal value returned by the arduino 0 : 0 1 : 340 2 : 511 3 : 651 4 : 614 5 : 732 6 : 768 7 : 833 8 : 682 9 : 783 10 : 819 11 : 872 12 : 829 13 : 883 14 : 896 15 : 928

Is this viable, you think ?

Your numbers do not appear correct.

.

Jaeth: Yeah i looked at that link, but i didn't understand that much

I would suggest to read it again. It goes a bit in depth but to does give a very extensive description.