Tiva Lab 05: Scan a Key from a Matrix Keypad



  • Learn how to read inputs from a matrix keypad and display the corresponding key that is pressed on a character LCD module

Background Information

You will need to write a C program that will determine which key has been pressed on the keypad. The program will then display the corresponding character on the character LCD module. A schematic diagram is provided below to show the connections needed to implement this lab. To do this, we will briefly introduce the fundamentals of a keypad.

Matrix Keypad

Keypads are used in all types of devices, including cell phones, fax machines, microwaves, ovens, door locks, etc. There are practically everywhere. Tone of electronic devices use them for user input.

In this section we will discuss logic and interface of a matrix keypad with microcontroller to reduce the number of port pins required to read a certain number of digital input. The same logic applies to any matric keypad.

MatrixKeypad 4x4 s

Why Matrix Keypad?

Typically, one digital input is connected to one port pin. When there are a lot of digital inputs that have to be read by microcontroller, it requires same number of port pins to read each input signal separately. It would not be feasible to allocate one pin for each of them, because these will occupy a lot of I/O pins. the main reason is that microcontrollers grow with the number of pins, and growth means more power, capabilities and most of all higher price. So in the end you can either get a cheap chip with little capabilities (that is what you need) but with few I/O pins, or a more powerful chip, which is mush more than you need.

Therefore, a new interface technique will be needed to reduce number of required pins in this kind of situation. A easiest way to do that is to arrange inputs in matrix form, which divides I/O pins into two sections: the rows and the columns. For example, a 64-key keyboard would require 64 digital input port pins. With a matrix circuit, 16 I/O pins arranged in 8 rows and 8 columns can connect 64 keys — 8 output pins to drive rows and 8 input pins to read columns.

What are the Key Matrices?

Matrix Keypad is made by arranging push button switches in rows and columns. If you want to interface a 4 by 4 (16 keys) matrix keypad with a microcontroller. In the straight forward way, you will need 16 I/O pins of a microcontroller for that, but by using a simple technique we can reduce it to 8 I/O pins. In the matrix keypad, switches are connected in a special manner a shown in the figure below.


The blue lines are the columns and the red lines the rows. There are 16 knots that the rows and columns intersect, and each knot connects one switch button. There will be no connections between columns and rows. When any of  the switches are pressed, the corresponding columns and rows are connected (short circuited), which can be detected by microcontroller to identify which keys have been pressed.

How does Key Matrix Works?

We make the columns as input pins and we drive the rows making them output pins. In order for the microcontroller to determine which button is pressed, it first needs to pull each of the four rows (R1 ~ R4) either logic low or high one at a time, and then poll the states of the four columns (C1 ~ C4). Depending on the states of the columns, the microcontroller can detect which button is pressed.


Lets assume that a logic high signal is given to Row 2 (R2). If any of the key belongs to Row 2 is pressed, the high signal from Row 2 will pass to the corresponding column as high. Watch the above animation, the button '5' is pressed, then the column 2 will also have high as long as the button '5' is pressed. What this means it that. if we know which row has currently logic high signal, and we watch the columns, then we can understand which button was pressed, if we detect power on a column. For example, our program pulls all four rows low and then pulls the second row (R2) high. It then reads the input states of each column, and reads column 2 high. This means that a connection has been made between column 2 and row 2, so button '5' has been pressed.

4x4 Matrix Keypad Pinout

Matrix keypads use a combination of four rows and four columns to provide button states to the host device, typically a microcontroller. Underneath each key is a pushbutton, with one end connected to one row, and the other end connected to one column. These connections are shown in below Figure

 MatrixKeypad 4x4Buttons 2 s

Connection Diagram

ConnectionDiagram2 s
Keypad Connection for Open Drain Output Pins

ConnectionDiagram s
Keypad Connection with Resistors for Regular Output Pins


 Required Components List

MatrixKeypad 4x4 64 4x4 Matrix Pad x 1
CharacterLcdDisplay s Character LCD module x 1

Circuit / Schematic Diagram

Connect the character LCD and matrix keypad to the Tiva LaunchPad board as shown below. The pin directional need to be configured as follows:

  • All the pins connected to the character LCD module are output direction.
  • The pins connected to the matrix keypad are divided into two parts: rows and columns. The pins connected to row wires are output direction, and connected to column wires are input direction.

LCD 123G 1294 s

MatrixKeyPadConn s

Required Reading Material




Add ezTiva LIB

Follow "Lesson 09: Add ezTiva Library into Your Project" to add ezTiva LIB into the project.


Sample Firmware Code

The code are not complete, you have to finish the GPIO Configurations and ReadKeyPad() function.

Inside the ReadKeypad() function, if no keys are pressed, it will return 0. If a key is pressed, it will return the ASCII value from the Keypad[] array that corresponds to that key. For example, if you press a key on the 3rd row, second column, the ReadKeypad() function will return the character '8' ( or ASCII code for '8').

Implement the following algorithm to "scan" the keypad in the ReadKeypad() function.

Scan Algorithm

  1. Drive row wire R low, and other rows high
  2. Read and test the states of the column wires
    • If column C is low, it is shorted to row R
    • if column C is high, C is not shorted to any row wire & remains pulled high
  3. Repeat steps 1 & 2, but with different row wire driven low (and others high)
    • A keypress is detected if a column line is detected in the low state
    • The key position is the intersection of that column and the row being driven low
    • Lookup the ASCII table to find out the ASCII code of the pressed key
  4. If no key is pressed then the function should return null (zero value)

Timing Issue

There is a short time delay from the time a pattern is written to an output port to the appearance of that pattern on the external pins. After writing a pattern to an output port (to drive row lines), insert a short program delay before reading the input port and testing the keypad column lines.


write to output port; // Row lines
read input port; // Column lines

Lab Questions

  1. Can you briefly describe your GPIO configuration for the matrix keypad? Explain what happens if the row pins are configured as general output pins without open-drain function?
  2. What is the status of the column input if no key is pressed?
  3. In a 4 x 4 matrix keypad used in this lab, can we press two keys at the same time? Explain.
  4. If you hold down a key, what happens to the system?


 Submit your completed project report including your working code.