Two switches per pin!!!


Closed Thread
Results 1 to 5 of 5
  1. #1

    Thumbs up Two switches per pin!!!

    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!
    Attached Files Attached Files
    Last edited by picnaut; - 16th June 2004 at 02:03.
    ---> picnaut

  2. #2


    Did you find this post helpful? Yes | No

    Question

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

  3. #3
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    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
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  4. #4


    Did you find this post helpful? Yes | No

    Smile

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

  5. #5
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    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
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

Similar Threads

  1. Is this a K Type sensor?
    By jessey in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 21st November 2009, 13:55
  2. DS1820 with 16f688
    By jessey in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 23rd May 2009, 05:07
  3. Advice-scrutiny for my serial controller
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 13th December 2008, 17:11
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. Another RTC, DS1287
    By DavidK in forum Code Examples
    Replies: 0
    Last Post: - 12th December 2006, 17:07

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts