Apologies if this has been asked before - I tried browsing around and searching for something similar, but I was having trouble even figuring out what to search on to find this particular topic.
I am attempting to use multiple simple external switches to control the logic on my Arduino Uno R3. It's very important that I use as few pins as possible, as I ultimately hope to expand this program to deal with a large number of devices at once.
I was hoping to run 5V from the 5Vout pin through three mutually exclusive switches, through nonequal resistors, and back to one of the analog pins, and have the Arduino be able to determine which of the three switches was closed based on the voltage of the input (see the diagram below). Upon a little bit of reading, I found that this is not an acceptable way to set things up, since the internal resistance between the analog pins and GND is very large and ill-defined (so the voltage drop across my R1, R2, and R3 will be small and hard to predict).
Can someone please help me come up with an alternative way to have multiple switches control Arduino logic using only one analog pin? Again, it's only possible for one switch to be closed at a time, which simplifies things a bit.
Thanks in advance for the help! Also, please don't respond with variants of "you don't want to do that" without providing an alternative. I know the posted circuit won't do what I want it to do, that's why I'm asking for alternatives
A "more professional" way of doing this is with a [u]multiplexer[/u]. ...I kind-of hate using analog when you really want digital.
But it can be done. You just need to make a [u]voltage divider[/u]. Two equal-value resistors gives connected between 5'v and ground gives you a choice of 0V (ground), 2.5V, or 5V.
And the "trick" is to give yourself some leeway so you're not looking for an exact voltage.
You can actually string several resistors in series to make a ladder divider. For example, if you make a string of equal-value resistors between ground and 5V, each resistor has an equal voltage "dropped" across it so there's 5V at the top, 0V at the bottom, and 2.5V in the center, etc.
Note the need to use good quality switches to execute this hack.
It is routinely used for the buttons in MP3 players (prior to touch screens) and video monitors - and my car speedometer - and the lubricant used in the "tact" buttons attracts moisture and degrades over time leading to spurious operation and eventual failure.
If you use a ladder containing resistors in a binary sequence, switches that bridge individual resistors can be distinguished when more than one is pressed and you have your analog input connected to the last point on the ladder without a switch so it is never undefined.
For regular quality or higher voltage rated switches / push-buttons they usually have a minimum current requirement. From IEC 60947-5-4 (2002) where they've tested at 5VDC and 1/5/10/100mA they've found 10mA is preferred. So using 470 ohm for resistors would probably be a better choice to ensure 5-10mA switching current and higher reliability/long life.