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

Required Reading Material

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 × 1
CharacterLcdDisplay s Character LCD module × 1
10 KΩ Potentiometer × 1
Breadboard × 1
Breadboard Power Adapter × 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


  1. Create a new folder under the EE3450 folder and name it Lab05_Keypad. Then double-click the folder you just created to jump into it.
  2. Launch the Keil μVisio, and create a new project, save the project as Lab05_Keypad.
  3. Add MyDefines.h to the Source Group, and then add the Common folder to the include paths under the "Options for Target" setting.
  4. Add ezTiva LIB into your project, increase the stack and heap size under the "startup_TM4cXXX.s (Startup)" setting, and then add the ezTivaLIB folder to the include paths under the "Options for Target" setting. (Lesson 09: Add ezTiva Library into Your Project)