The most common method for interfacing multiple switches—multiplexing— allows for the connection of (N/2)2 switches with N microcontroller IO lines. The method described below, which has its roots in an LED interface technique commonly known as “Charlieplexing,” makes it possible to interface N*(N – 1) switches with N IO lines.

In conventional multiplexing of an N-by- N matrix with 2N IO lines, half of the lines are configured for input and half for output (at logic 0), permanently using a pin either for just the input or for just the output. The proposed method uses both states of an IO pin, increasing the number of switches that can be interfaced to it.

In the proposed method, every IO pin is connected to every other pin with two switch-and-diode series circuits. Figure 1 shows a two-pin example. In this configuration, switch S1 can be read by configuring Pin2 to input mode and Pin1 to output mode (with logic 0). Switch S2 can be read by configuring Pin1 to input mode and Pin2 to output mode. This technique allows designers to interface six switches with only three pins, and a whole numeric keypad of 12 switches with four pins.

This configuration easily scales to any number of IO pins, but between two IO pins, the circuit essentially remains the same. For a circuit with N IO lines, each IO pin has N – 1 diodes leading in, and N – 1 diodes leading out. At any time, one pin is switched to logic 0, while the other N – 1 pins are configured in input mode for reading N – 1 switches attached to this pin. Generally, the N – 1 diodes leading into this pin can be replaced by a single diode, with all of the N – 1 switches connected to its anode.

Figure 2 shows a circuit for interfacing 12 switches using only four microcontroller IO pins. To read switches S1-S3, Pin1 is set to logic 0, and Pin2, Pin3, and Pin4 are set to input mode. Similarly, to read switches S4-S6, S7-S9, and S10-S12, Pin2, Pin3, and Pin4 are, respectively, set to logic 0, while the rest are set to input mode.

The circuit in Figure 3 demonstrates how a full numeric keypad interface can be connected to only four pins of a microcontroller (an Atmel ATMEGA8 in this case). The switches are pushbutton types, and the diodes are generic rectifying diodes. The demonstration program (go to www.electronicdesign.com), written in C using WINAVR, successively sets one of the IO pins to 0, and the rest to input, and it sets the state of the switches, which was relayed serially in mode 9600-8-N-1.

References:
1. Anurag Chugh, Dhananjay V. Gadre, “Eight-Pin Microcontroller Handles Two-Digit Display With Multiple LEDs,” www.electronicdesign.com, ED Online 15512
2. “Charlieplexing” at http://en.wikipedia.org/wiki/Charlieplexing. Retrieved Sep. 3, 2007.