picnaut
- 16th June 2004, 01:58
Hello,
I'm sure that many people here have already used this method but, if not, here is a way to detect two switches on one pin.
Why would you want to do this?
If you've ever had to upgrade to a larger, more expensive chip because you needed a couple more input pins, you'll see the benefit.
Assuming you're already using one resisitor per switch to bias them to VCC or GND (you should be), this method only adds one extra resistor for every two switches. Fractions of a penny.
The only requirement is that you must be able to configure your input pin as analog. This will not work on "digital only" I/O pins.
It works by using a voltage divider. When neither switch is pressed, you get VCC. When switch 1 is pressed you get a unique voltage. When switch 2 is pressed you get another unique voltage. If you press switch 1 and switch 2 at the same time you will get a 3rd unique voltage.
It starts to get kind of sketchy when you try and hook up more than two switches to one input pin. So, unless there is no way that the user can press more than one switch at a time, I wouldn't recommend it.
I've attached a small application (zipped up) that includes a simple schematic and allows you to fool around with different resistor values.
You want to choose resistor values that keep each of the 4 different voltages as far apart (spread out) in value as you can, so that you don't misread which switch has been pressed.
Enjoy!
I'm sure that many people here have already used this method but, if not, here is a way to detect two switches on one pin.
Why would you want to do this?
If you've ever had to upgrade to a larger, more expensive chip because you needed a couple more input pins, you'll see the benefit.
Assuming you're already using one resisitor per switch to bias them to VCC or GND (you should be), this method only adds one extra resistor for every two switches. Fractions of a penny.
The only requirement is that you must be able to configure your input pin as analog. This will not work on "digital only" I/O pins.
It works by using a voltage divider. When neither switch is pressed, you get VCC. When switch 1 is pressed you get a unique voltage. When switch 2 is pressed you get another unique voltage. If you press switch 1 and switch 2 at the same time you will get a 3rd unique voltage.
It starts to get kind of sketchy when you try and hook up more than two switches to one input pin. So, unless there is no way that the user can press more than one switch at a time, I wouldn't recommend it.
I've attached a small application (zipped up) that includes a simple schematic and allows you to fool around with different resistor values.
You want to choose resistor values that keep each of the 4 different voltages as far apart (spread out) in value as you can, so that you don't misread which switch has been pressed.
Enjoy!