Using Just 1 Pin To Handle Many Buttons

DTB Radio

Member
This is copied from a reply I made in another thread, but I thought some might find it useful enough to warrant its own thread.

You can theoretically use one ADC channel and a multiple-resistor voltage divider to read up to 254 buttons. I've used that trick myself for up to 8 buttons (for a who-buzzed-in-first quiz game control box). My approach to resistor values is I assume I want a total divider resistance of close to 1k, and divide 1k by the number of buttons I need. I then pick a common resistor value that's as close as possible to the result. One end of each button is tied to a divider junction (including either the hot or ground side of the divider, whichever end you do NOT tie to the ADC channel as specified shortly) , and the other end of the button is tied to the ADC channel I want to use. I also tie that ADC channel to either hot or ground through a 20k resistor, to make sure it doesn't float. After that, you just use code to determine if a button was pressed, and which one. If you need to know the exact ADC values generated by each button, you can find out by using the debug command after executing a readadc command.

Here is some rough example code, assuming three buttons and three resistors, with the ADC channel tied through a 20k resistor to ground:
Code:
main:
      readadc 0,b0 'check for button press
      if b0 > 30 then goto button_pressed 'button was pressed
      goto main 'no button pressed

button_pressed:
      if b0 > 55 and b0 < 115 then
          'button 1 pressed, do something
      end if
      if b0 > 140 and b0 < 200 then
            'button 2 pressed, do something else
      end if
      if b0 > 225 then
            'button 3 pressed, sterilize a goat using LED's
      end if

goto main
Quite easy, and it allows you to handle a LOT of button inputs with just one ADC line. Now, some might say that the PICAXE's slow speed means that multiple buttons could end up being pressed and read at the same time, but in practice, with the 8-button quiz set I made, it has NEVER happened even when trying to make it happen. And that's running at the low end of only 4mhz.
 

BeanieBots

Moderator
The "finished projects" sections are worth having a good browse through.
There is some very good content from some very experienced people.
Even if the project itself is not of direct interest, the posted code and/or circuits may well include relavent sections. Menu systems, LCD driving and I/O techniques being good examples.
 
Top