Instructables

Connect several digital inputs to one analog input!

Picture of Connect several digital inputs to one analog input!
I often run out of digital pins on my Arduino. Anything as complicated as, say, a video game controller, was near impossible with the amount of pins I had available. Multiplexing buttons works, but it requires lots of connections and soldering. So, I put together a solution!

Step 1: Resistor Dividers in Five

Picture of Resistor Dividers in Five
In a circuit consisting of one resistor and a 9V battery, the resistor drops 9V across itself. So, the resistor's forward voltage is 9V. Two resistors of equal value would each drop half the voltage, 4.5V, each being half the total resistance in the circuit. If one resistor is 100 ohms, and a second resistor is 300 ohms, each will have a proportional forward voltage. In a 12V circuit, the 100 ohm resistor would drop 3V and the 300 ohm resistor would drop 9V. There's more to resistor dividers (such as potentiometers), but that is beyond the scope of this instructable. I suggest reading this for more info: https://www.sparkfun.com/tutorials/207
 
Remove these adsRemove these ads by Signing Up
Great explanation. I'm a little confused by the last sentence. Can "switch case" only be used with digital inputs? I know the cases can look for a range of values. I don't think I've used it with analogue input, but I always thought you could. Maybe I missed something.
gizzmotronics (author)  justbennett1 year ago
Switch case just compares integers. It's possible, I'll post some code.
gizzmotronics (author)  gizzmotronics1 year ago
Done. The formula to convert a voltage into analog reading is (V/Vcc) * 1023, but it's a better idea to actually check the values you get using the serial monitor because of resistor tolerances and other imperfections.
Ok, I see. I was still a little confused so I checked http://arduino.cc/en/Reference/AnalogRead. So analogRead() calls this function ((V/Vcc) * 1023) and assigns the reading an integer value from 0 to 1023.

Along the way I learned, as russ_hensel has pointed out that the Arduino can only read analog to digital about 10,000 times a second. Pshh.
You should probably put a bit more emphasis on the fact that you are not multiplexing digital signals directly but converting the digital input to an analog one. Analog input is a bit more complicated and much slower than digital. This is indeed a useful technique in some cases. Why not post a code snippet to go with it.