20-Key Matrix Keypad Electronics Project

Build a keypad that uses only four microcontroller pins

By Philip Kane

Keypad Project Introduction

I've been considering a couple of possible future projects that would use DIY keypads with at least 20 keys. A 20-key matrix keypad connected directly to a microcontroller requires 9 digital I/O pins. That's a significant number of the available GPIO pins on the microcontroller board I was planning to use. I had two options - switch to a microcontroller board with a larger number of GPIO pins or look for a way to reduce the required number of pins. I chose the latter option and, after considering a number of solutions, I decided on one that would require only 4 microcontroller GPIO pins with a minimum number of additional components.

Keypad Project Solution

The circuit in figure 1 has 20 push button momentary contact switches, two inputs (Clock, Reset) and two outputs (C1, C2).

Parts List

Keypad CircuitFigure 1: Keypad Circuit


The switches are arranged in two columns of 10 rows. The odd numbered switches (S1, S3, S5,..., S19) are connected to column one and the even numbered switches (S2, S4, S6, ... , S20) column two.

The heart of the circuit is a CD4017 decade counter. As shown in figure 2 below, the inputs of switches in the same row are connected to the same 4017 output. The outputs of switches in column one are connected to keypad output C1, and those in column two are connected to output C2.

Switches in RowFigure 2: Switches in the Same Row


Figure 3 shows the same circuit with the switches arranged in a 5x4 matrix configuration. Each matrix row contains two key-pad circuit rows.

4x5 Matrix ArrangementFigure 3: 4x5 Matrix Arrangement


The CD4017 has a CLK and a RES input and 10 outputs (Q0-Q9). Applying a pulse to the CLK input causes the current output to return to a LOW state and sets the next successive output to HIGH. A pulse to the RES input sets the 4017 Q0 output to HIGH and all other outputs to LOW.

You scan the keypad for a key press by cycling through the rows, one at a time, checking the state of each column. Your code must keep track of the current row (that is, which 4017 output is currently active) in order to determine which key(s) are being checked. To determine if a key in the current row has been pressed, you check the state of keypad outputs C1 and C2. For example, assume that 4017 output pin Q3 (keypad row 4) is HIGH. If pin C1 goes from LOW to HIGH while Q3 is HIGH then that indicates switch S7 was pressed. Alternatively, if C2 is HIGH then switch S8 was pressed.

Closed SwitchFigure 4: Closed Switch


To ensure that each new cycle starts from a known state (4017 output Q0 = HIGH), a pulse is sent to the keypad circuit Reset input to begin the new cycle.

This circuit can be reduced to one column with up to 10 switches, requiring only one input pin for a total of just 3 microcontroller GPIO pins. You can also extend it by adding more columns. Each column requires an additional input pin. So, a 30 key-pad will require a total of 5 pins, a 40 key pad 6 pins and so on.

The Keypad Test Circuit

To test the circuit, I used an Arduino single board computer (SBC). An Arduino C application running on the SBC monitors the keypad circuit. When it detects a key press it prints the key label to the Arduino serial terminal.

As shown in figure 5, keypad circuit outputs C1 and C2 are connected to Arduino GPIO pins D2 and D3, which are configured as inputs. Arduino pins D4 and D5 are configured as outputs and connected to keypad circuit inputs Reset and Clock.

Arduino SBC Test CircuitFigure 5: Arduino SBC Test Circuit


The Test Application

The flowchart in figure 6 shows the general sequence of steps for testing the keypad circuit.
Application FlowchartFigure 6: Test Application Flowchart


The application scans the keypad circuit one row at a time. At each row it checks the column inputs, beginning with C1, until all of the columns have been checked or it detects a key press. If it detects a valid key press, it uses the row, column coordinates of the key to obtain and print the corresponding key label. After printing a key label or after checking all rows and not detecting a key press, it resets the keypad circuit to start a new scan cycle.

Keypad Circuit Test Application

Click here for the Arduino Code

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].