Shop OBEX P1 Docs P2 Docs Learn Events
Multiple buttons to one IO pin? — Parallax Forums

Multiple buttons to one IO pin?

smbakersmbaker Posts: 164
edited 2009-02-10 05:21 in Propeller 1
Does anyone have a clever solution to attaching more than one button to a single IO pin? One of the standard tricks with other microcontrollers is to use resistors to turn a collection of buttons into voltage dividers and then sample them with built in A/D. We don't have built in A/D so that is not an option. I wonder if maybe there's a way to throw a capacitor onto a pin and turn it into a cheap A/D converter (set the·dir to output to charge the cap, set the dir back to input, and measure how long it takes the cap to discharge through a resistor that's attached to the button).

I can think of numerous ways to do it with two pins using a parallel-in, serial-out·shift register (one pin for data, one for clock). I·could probably get the shift register idea down to a single pin by throwing an independent clock at it.

I'm looking to support 4-6 buttons.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-08 20:13
    You can make a cheap A/D converter with two Propeller I/O pins. See the application note (AN001) for the cog counters for a discussion, sample circuitry, and sample code. You can also do what you suggest about charging a capacitor and measuring the discharge time. That takes only one I/O pin. There are some 1-wire I/O expanders from Dallas/Maxim that provide one or two inputs or outputs each device, can be driven with a single I/O pin, and you can have a whole bunch (certainly 4-6) on one I/O pin. There's sample 1-wire driver code available in the Object Exchange.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-02-08 20:32
    You can use a single pin to charge a cap, change from output to input, and time the discharge, the time being dependent on which button (with series resistor to ground) was pressed. This is how the RCTIME command is used in the BASIC Stamps.

    -Phil
  • rjo_rjo_ Posts: 1,825
    edited 2009-02-08 22:19
    My favorite solution... add another Prop... and use frequency synthesis or pwm to transmit the button or button combinations...you are eventually going to add another Prop anyway... just break down and do it now[noparse]:)[/noparse]
  • DroneDrone Posts: 433
    edited 2009-02-08 22:30
    @smbaker, Welcome to the Propeller Forum!

    I'll briefly address the analog-to-digital "switch-plex" approach:

    Have a look at this page:
    www.edn.com/article/CA6360317.html
    Download the .pdf here:
    www.edn.com/contents/images/6360317.pdf

    And the article at the top of this page:
    www.edn.com/article/CA56657.html
    Download the .pdf here:
    www.edn.com/contents/images/2499di.pdf

    Keep in-mind, with this analog/switch approach, simultaneous closures (key presses) may result in undesired results; or in-fact desired results!...

    For example; if you want to ignore multiple simultaneous switch closures, your code should ignore resulting invalid analog voltages that appear. On the other-hand, you might allow your code to accept some voltages for certain multiple switch closures to add more functions; e.g. pressing two keys simultaneously puts you back one step in a user-interface. Programming to use or ignore multiple switch closures obviously makes things more complex. But this is always the case with any user button/keypad driver.

    Also, even with the delays introduced by analog to digital conversion, your code may require delays to de-bounce switch closures.

    Lastly, pay attention to the fact that the simple delta-sigma ADC solution outlined in the Propeller AN001 really requires that the passive components be connected VERY close to the physical pins on the Propeller chip. This is stated somewhere in AN001 if memory serves. I've had mixed (mostly poor) results with this approach when trying it with a 40pin propeller on a plug prototyping board. YMMV...

    Please do reply at some point and share with us what your application is and what solution worked for you. In the mean-time, there's usually someone here willing to help in near real-time wherever you are.

    Best Regards, David (GMT+7)
  • smbakersmbaker Posts: 164
    edited 2009-02-09 09:07
    Thanks for all the advice. I think what I'll end up doing is to go with a PCF8574N I2C I/O Expander. If I understand correctly, I can wire this into the SDA and SCL signals on the 24LC256 eeprom, and effectively get myself 8 additional I/O lines without using up any additional prop pins. I should be able to throw together a little piggyback board and be able to retrofit this to the eeprom socket on my existing boards without actually having to modify them, which would be another win.
  • mctriviamctrivia Posts: 3,772
    edited 2009-02-09 09:09
    8 extra io lines meens 16 buttons if you use a scanning grid.
  • virtuPICvirtuPIC Posts: 193
    edited 2009-02-09 09:13
    Spending 3 pins you could even scan 6 capacitive touch buttons... But for a single pin I support the idea of resistors and ADC.

    Use the schematic below. Use different values for the resistors to the switches - like 10k, 22,k, 33k, 47k - and a much smaller value for the resistor to the pin - like 1k. Your button driver checks the pin. If it is logical high it switch to output low to ground for a certain time to discharge the cap, then switches back to input mode and measures the time to logical high input. Time depends on RC-product.

    If you use approximately exponentially resistor values you can even distinguish simultaneous presses of several buttons! Build your own polyphonic piano keyboard.

             ------         VCC      VCC      VCC      VCC
                   |         +        +        +        +
                   |         |        |        |        |
                   |         o        o        o        o
                   |         '\       '\       '\       '\
                   |           \        \        \        \
                   |         o  \     o  \     o  \     o  \
                   |         |        |        |        |
                   |        .-.      .-.      .-.      .-.
                   |        | |      | |      | |      | |
             prop  |        | |      | |      | |      | |
                   |        '-'      '-'      '|'      '|'
                   |   ___   |        |        |        |
              pin  o -|___|--o--------o--------o--------'
                   |         |
                   |         |
                   |        ---
                   |        ---
                   |         |
                   |         |
             ------         ===
                            GND
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Airspace V - international hangar flying!
    www.airspace-v.com/ggadgets for tools & toys
  • dMajodMajo Posts: 855
    edited 2009-02-09 10:02
    smbaker,

    this is not a direct answer to your question, but maybe you can have an idea out from this:

    http://www.maxim-ic.com/quick_view2.cfm/qv_pk/4944
  • T ChapT Chap Posts: 4,223
    edited 2009-02-09 18:46
    smbaker said...
    Thanks for all the advice. I think what I'll end up doing is to go with a PCF8574N I2C I/O Expander. If I understand correctly, I can wire this into the SDA and SCL signals on the 24LC256 eeprom, and effectively get myself 8 additional I/O lines without using up any additional prop pins. I should be able to throw together a little piggyback board and be able to retrofit this to the eeprom socket on my existing boards without actually having to modify them, which would be another win.

    I use the PCF8575 and 8574 a lot, they are great for expansion i/o. Just be sure to read the data sheet about writing all "1's" to the outputs prior to reading.
  • AleAle Posts: 2,363
    edited 2009-02-09 19:39
    I had to add some buttons to an already 2 propeller app, so I just used an atmega48 and used a synchronous transfer protocol, just 1 pin. A less expensive uC like a PIC or an attiny may be better suited
  • smbakersmbaker Posts: 164
    edited 2009-02-09 21:21
    TChapman said...


    I use the PCF8575 and 8574 a lot, they are great for expansion i/o. Just be sure to read the data sheet about writing all "1's" to the outputs prior to reading.
    One thing that wasn't quite clear to me from the datasheet is whether or not i need to put pullups on the inputs (generally I stick a 10K pullup resistor to +3.3V on my button inputs to the propeller). I was getting the impression from the datasheet that this might not be necessary with the 8574 but I don't think it had a clear cut application note for a simple button connected to an input. I'm assuming that regardless of the pullup, the buttons will be wired to pull the input to ground when depressed. Can anyone clarify on the need for pullups on the 8574?

    Thanks,
    Scott
  • T ChapT Chap Posts: 4,223
    edited 2009-02-10 05:13
    I use pullups or pulldowns depending, I think I recall that they are needed for a floating state like a NO or NC button.
  • mctriviamctrivia Posts: 3,772
    edited 2009-02-10 05:21
    switched contacts should never be left hanging. therefore if your switch goes from io pin to high you should use a pull down resister to make it a 0 if the switch is open. if the switch goes to low you should use a pull up resister to make it a 1 if open. If the switch is connected to an output(like done in matrix switching) then you should use pull down resisters if high is the active state and pull up if low is the active state.
Sign In or Register to comment.