Teach and Learn: Working with Matrix Keypads

Providing Input to a Wide Variety of Devices

By Philip Kane

Matrix keypads are used to provide input to wide variety of devices and appliances such as cellphones, microwave ovens, and calculators.

Structure and Operation of Matrix Keypads

A matrix keypad consists of push button switches that are located at the column and row intersections of a grid or matrix. Switches in the same row are connected together as are switches in the same column.

12 switches arranged in matrix

Figure 1: 12 switches arranged in a 3x4 matrix

As you can see in the following figure, when you press a switch it connects a row to a column.

Key press connects

Figure 2: Key press connects a row to a column

The circuit in figure 3, below, illustrates one way to detect a key press. Pull-up resistors are connected to the rows to ensure that they will be in a HIGH (+V) state when no key is pressed. If you ground one of the columns, pressing a key in that column will connect the associated row to ground (LOW). You can determine if one or more keys in that column have been pressed by checking each row for a LOW.

Detect a Key Press

Figure 3: One way to detect a key press

In the example in figure 3 column C2 is connected to ground and a LOW is detected at row R3. This indicates that the key in row 3, column 2, (R3C2) was pressed.

Interfacing Microcontrollers and Matrix Keypads

Matrix Keypad Hardware

To directly interface a RxC matrix keypad to microcontroller you will need to allocate R + C digital I/O pins. For example, the key press detection scheme illustrated in figure 3, requires a total of 7 I/O pins - four input pins to connect to the rows and three output pins to connect to the columns.

Matrix Keypad Software

Your application must include software that monitors the keypad for key presses and, when a key press is detected, returns the key coordinates or the key value to the main application.

Matrix Keypad Example

The example in this section illustrates the basics of interfacing a matrix keypad to a microcontroller. As shown in figure 4, the example hardware consists of an Arduino Uno development board and a 4x4 Matrix Membrane Keypad.

Arduino Uno Matrix Keypad

  Figure 4: Arduino Uno and 4x4 matrix keypad

The Arduino application monitors the keypad for key presses. When a key press is detected, it prints it to the Arduino IDE serial terminal. This isn't intended to be a full featured keypad driver. If you're going to use the Arduino platform in future projects, check out the keypad library created by Mark Stanley and Alexander Brevig.

Wiring Up the Hardware

Eight Arduino I/O pins are used in this example – four input pins for the keypad rows and four output pins for the columns. The circuit uses the Arduino's internal pull-up resistors so external pull-up resistors on the row inputs are not required.

Figure 5 shows the pinout diagram for the keypad used in this example. It associates the pins on the keypad connector with the keypad rows and columns. Refer to the data sheet for your keypad to get the pinout for your keypad.

Pinout Diagram

Figure 5: Matrix keypad pinout diagram

The following table lists the wiring connections between the Arduino board and the keypad used in this example. To wire up the application, refer to this table and figure 6 below.

Table 1

Applied Parts Requirements:

Keypad Row/Col
Keypad Pin #
Arduino Digital Pin #
Col 1
Col 2
Col 3
Col 4
Row 1
Row 2
Row 3
Col 4

Keypad Connection

Figure 6: Arduino Uno – 4x4 matrix keypad connection diagram

Application Software

The code in Listing 1 is an Arduino C application that implements a simple matrix keypad driver. It repeatedly scans the keypad, one column at a time. It sets the column to LOW. It then checks each row in the column for a LOW which indicates a key press. When it detects a key press, it uses the row, column coordinates of the key to map into an array that contains the corresponding key label and prints it to the serial terminal.

The application consists of two major functions, getKey and processKey and arrays, rowPins, colPins and keyChars.

The arrays, rowPins and colPins, contain the numbers of the Arduino pins assigned to the rows and columns respectively. The row pins are inputs. The column pins are outputs. However, when not in use the column pins are left floating by configuring them as inputs.

Function getKey scans the matrix keypad once per call. It is called from the main loop and thus the application checks for a key press every iteration of the main loop.

For each column pin in the colPins array getKey changes the pin configuration from input to output and sets it to LOW.

It then checks each row pin in that column for a LOW condition, which indicates that a key has been pressed.

When a key is pressed or released there are momentary voltage fluctuations before the key settles into its final state. Therefore, if getKey detects a possible key press it waits for a short time interval and then checks the row pin again. If it's a valid key press, getKey sets the keyDown flag to “true” and then calls function processKey and passes to it the coordinates of the key that was pressed.

Function processKey uses the coordinates passed to it by the caller (getKey) to retrieve the associated key label from the keyChars array. The array contains the key labels, arranged in the same order as the corresponding keys on the keypad.

In a real application might pass the character representing the key label back to the caller or place it in an input buffer for access by another routine that handles user input. In this example processKey simply prints the character to the serial terminal and returns.

After processKey returns, getKey waits for the user to release the key and then returns to the caller.

Listing 1

Philip Kane, a technical writer for over 20 years, has had a life-long interest in science, technology and space exploration. He enjoys designing and building electronic gadgets.

If you have an electronics story or project you'd like to share, please email [email protected].