PDA

View Full Version : Two switches per pin!!!



picnaut
- 16th June 2004, 02: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!

picnaut
- 16th June 2004, 05:15
Hello,

As someone has pointed out to me, you can also use the POT command to tie multiple switches to one pin. You also don't need to tie up an analog pin because you can use a digital I/O pin.

However, for time critical applications, that's probably not the best option. Using the analog pin, you can start the ADC reading and then poll for it's completion. That allows you to have time critical stuff (like interrupts) going on in the background.

Since the POT command relies on timing to determine the resistance in series with a capacitor, I think that it will fall apart if an interrupt happens in the middle of it.

So, for non-timing critical applications, use either method (I'd actually probably lean toward the POT command).
However, for timing critical applications, you're probably better off using the ADC inputs and polling.

Dwayne
- 18th June 2004, 17:31
Hello Picnaut!

Picnaut>>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.<<


If you string a number of resisters in series between neg and Pos.... and put a switch inbetween each of the resistors, you can use your AD to read the value between 0 and 1000 (or whatever you have set up..

10 resisters will give you AD values of 102, 204, 306...1024

Thus a 10 switcherooo on one pin.

I do like your Resistor trick though!

Dwayne

picnaut
- 18th June 2004, 19:36
Hi Dwayne,

That's a cool method too!

If I understand you correctly, each switch brings the point where it's connected to ground. That means that there is no voltage drop across the rest of the resistors between that point and ground, even if the switches at those resistors are pressed.

The only problem with this method is that it can't handle simultaneous switch presses.

For the application that I was working on (when I developed my method) I had to be able to handle more than one switch at a time being pressed.

Your method will always give precedence to the switch that is connected to the furthest resistor from ground.

It's still a cool way to do it though.
It's also not timing dependant like the POT command, which is really good.

I will probably use it on my next project that uses a rotary switch.
It would be good for multiposition slide and toggle switches too.
There is no way that you can hit more than one switch at a time with those.

Cheers.

Dwayne
- 18th June 2004, 20:02
Hello Picnaut,,

Picnaut >>If I understand you correctly, each switch brings the point where it's connected to ground. That means that there is no voltage drop across the rest of the resistors between that point and ground, even if the switches at those resistors are pressed.

The only problem with this method is that it can't handle simultaneous switch presses.<<

Yes, you are correct It cannot handle two switches at teh same time.

Picnaut >>
I will probably use it on my next project that uses a rotary switch.
It would be good for multiposition slide and toggle switches too.
There is no way that you can hit more than one switch at a time with those.
<<

I Understand... I enjoyed the accuracy of the PIC AD converter... It was always within +/- 1.... thus, if the number should have been 202,,, it was either 201 or 203...and mostly 202... And it was like this right on up each of the resistors. Granted we used 1 percent tolerance resistors, but At least we can read a value and see if the tolerance is within 5% and know what was pressed....with confidence.

We are looking (right now) for cheap keypads. maybe in a matrix situation.

Dwayne