Connecting push buttons to a microcontroller to trigger specific actions sounds straightforward: use a pin and read its state. But what happens when you need more buttons than the chip has available pins? Here are several ways to get more from limited hardware—sometimes with just one or two pins handling more than five or six buttons. Things are not always as simple, or as difficult, as they first appear.

When we start programming microcontrollers, examples seem to cover most common needs. Yet they often assume that readers already know certain basics, and skip details that may be unfamiliar. Connecting buttons to microcontrollers to control events is one such detail. Nobody is born knowing how to add more and more buttons to a design; it takes a little explanation.

8D79.jpg
One way to provide input to a microcontroller is with push buttons.

How do electronics designers read dozens of buttons through a single pin? A little research turns up several useful approaches. Whether you use buttons, switches or another kind of switch, it helps to know the usual ways to connect them to a microcontroller.

One button at a time

The most basic approach is to use available microcontroller I/O pins configured as inputs in the program. The TRIS instruction sets a pin's direction. This lets us configure a pin as an input or an output whenever needed. For example, TRISB.5 = 1 sets the specified port-B pin as an input; setting it to zero configures it as an output.

8D7A.jpg
Examples of connecting a push button to a PIC

The diagram shows two ways to connect a button to a microcontroller input. One notable detail is the use of pin PORTA.4, whose input includes a Schmitt Trigger. This feature is commonly used to count external pulses with Timer 0 (T0CKI, or Timer 0 Clock Input). At the right point in the program, simply check the pin's state. Here is a Proton BASIC example:

8D7B.jpg
Example code for using a single button

Scanning a keypad matrix

A matrix keypad arranges buttons in rows and columns so that many keys can share a smaller number of microcontroller pins. This familiar layout appears in phones and calculators.

8D7C.jpg
A nine-key matrix

The example uses nine keys with just six microcontroller pins: three rows and three columns. With a PIC such as the 16F628A, port B's features mean no external bias resistors are needed: the chip has them built in. Enable them through bit 7 of the OPTION_REG register, found in all PICs in the 16F family. If you use port B's internal resistors for this or another application, configure the register correctly.

You can add a row and a column to make a 16-key keypad, or remove a row or column to use only six buttons. Below six keys, this method still takes four pins—no better than assigning one pin to each button—so it is most useful for more than six and up to 16 buttons when only one port is available. With two free ports on another microcontroller, the matrix can support as many as 64 keys: eight rows and eight columns.

A good keypad-reading routine should be orderly, fast and accurate. It needs to spend as little time as possible checking whether a key is pressed, and must identify the correct key rather than confusing one button with another.

8D7D.jpg
Program header for scanning a matrix keypad

A common method sets the columns as inputs and the rows as outputs, or vice versa. The circuit shown uses TRISB = %00000111: the three least significant bits are set to 1 for inputs, with the remaining pins set as outputs. The program activates each row in turn and reads the columns. By scanning quickly, it can identify which key was pressed.

The example's program header defines variables and names the port pins according to their functions, then configures TRIS and OPTION_REG. The main program runs its chosen tasks and calls the PULSAR subroutine to read the keypad whenever necessary.

8D7E.jpg
Routine that reads the matrix

The scan activates each row in sequence and checks for a pressed key. When it finds one, the program returns to its main loop with the BOTON variable set to the number of the key pressed.

Let Proton do the work

Proton BASIC includes the INKEY instruction, which can spare you a long, confusing routine for scanning rows and columns. First, declare the port used for the keypad: DECLARE KEYPAD_PORT PORTB. Then BOTON = INKEY makes the program look for a pressed key and return its value in a single line.

8D7F.jpg
The INKEY instruction saves many lines of code

As the example shows, the complete program can be reduced to just a few lines. That makes it much easier to follow. Proton's HELP contains more information on extending this useful instruction's capabilities.

When 16 keys are not enough

Another way to handle scarce hardware resources is to follow a technique used in many consumer electronics devices: connect a series of buttons to one or more PIC ADC inputs, using a chain of resistors.

8D80.jpg
A classic TV keypad

In this TV example, a single pin reads five buttons. The 5 V supply at the bottom feeds the series of resistors R1901–R1907, which lead to pin 12 of the processor (KEY–IN). Resistor R819 connects that input to ground, creating a variable voltage divider whose output depends on the button pressed. Because each button taps a different point in the chain, each produces a different voltage. The ADC measures the voltage level and thereby identifies the key.

8D81.jpg
A matrix keypad combined with a resistor-based keypad in one device

Some devices combine a matrix with a series resistor circuit like the one above. This hybrid arrangement was common in large music systems: the matrix handled primary functions, while the resistor circuit served secondary controls. One reason was the reliability of the soft-touch buttons used in these products. Poor-quality buttons in a resistor-chain setup could make the processor misread a key and perform the wrong action. Pressing PLAY to start a CD, for instance, might switch the device to RADIO or change the equalizer settings.

A resistor divider connected to a microcontroller's ADC can accommodate a great many buttons. A BYTE variable, for example, can hold up to 256 distinct values, enough to represent the same number of buttons. But button quality matters: a faulty switch can make the design fail unexpectedly. And, as Murphy's law has it, it will fail at the worst possible moment and cause the greatest imaginable damage. Choose high-quality buttons to avoid trouble later.