Path: AVR_EN => Applications => Keys and switches on an ADC
Key matrix Applications of
AVR Single chip controllers AT90S, ATtiny, ATmega and ATxmega
Keys, switches and resistors on an ADC input

All those pages and subpages are contained in this PDF document (35 pages, 1.42 MB).

Keys and switches via an AD converter channel

This is the page to save portpins for far more relevant tasks than just sensing some keys. The trick used is to connect keys, switches and keypads to an AD converter input and to leave the task to identify the ones pressed to a short piece of software.

If you need more explanations on the resistor matrix's function and calculation, consult the numerous subpages.

Attaching and reading of several keys and switches via an ADC input pin can be done in different ways. Three options are described here, depending from your hardware:

Resistors on a keypad Whatever hardware you have, this fits it all Link
n keys If you have up to eight single keys of which only one of it can be pressed at a time Link
Mouse piano If you have up to four switches, each of which can be on and off simultaneously (such as a DIP switch or a mouse piano) Link,
Keypad If you have a key matrix with either 12 keys in a 3-by-4 or 16 keys in a 4-by-4 matrix, where only one of the keys can be pressed at a time, and if you want to learn how to interface that with an AVR Link
... and you prefer using a command line tool to iterate resistor values Link
... and you prefer using a graphical application tool to iterate resistor values Link

Software for all purposes

As calculating resistors for keys, switches and keypads is pretty much the same for all cases, this (for Win64) software in Lazarus Pascal has been written to calculate all variants.

It can be used as follows.
  1. From the drop-down Type select either keys, switches or keypads,
    Type menu Key type Switch type Type menu

  2. depending from that selection, the Size drop-down offers specific sizes,
    Size menu keys Size menu switches Size menue for keypads

  3. choose resistor and ADC properties, if desired,
    Resistor tolerance Resistor rows ADC resolution
After having selected all properties you can press Iterate1 for single iterations or Iterate100 for 100 iterations.

Iteration window The read-only edit field Difference shows the average difference of the resulting voltages from the target voltages. The edit field Overlaps provides information on the number of overlapping areas between the different keys and states of the input keys and switches. Steps counts the total number of iteration steps, Success the number of successful iterations. If you'd like to restart iteration choose Restart.

From the group Show

Show windows

you can open different windows that display the current iteration status and results:

  1. Resistors opens a window displaying the current resistor values and their lower and upper values, depending from the current tolerance setting,

    Resistor window

    If you klick on a value in that window the content is saved as a text file, see the file as an example.
  2. Schematic opens a graphic window displaying the electrical schematic

    Schematic window

    If you click on that picture the image is saved as a Portable Network Graphic (.PNG) file or a Bitmap Graphics (.BMP) file.
  3. Results displays a window holding all results for all different states (keys, switch combinations), with the target value, the nominal value, the lower and upper bound (all in mV/V) as well as the nominal, lower and upper ADC values, depending from the selected ADC resolution,

    Result window

    If you click on one of the values displayed the results are saved either as Tab Separated Values (.TSV) or as Comma Separated Values (.CSV), ready to be imported to a spreadsheet (such as Calc in the Open-Office package, just select Open with in the context menue of the TSV file and Calc, or, in Excel, open an empty spreadsheet and drop the file onto that). Note that in case of the TSV file the decimal separator character is taken from the operating system properties so that comma-separated float values are correctly recognized.
  4. Asm source provides an assembler table which can be imported into assembler source code,

    Assembler window

    If you click on that window its content is saved as an Assembler Include file (.INC), ready to be included with .include "" into your source code.
  5. All resp. None selects resp. unselects all the windows.
The filename is in all cases "[type]_[size]_[resistor row]_[resistor tolerance%]_[ADC resolution]". The location/path can be selected in the dialog.

©2018 by