You are on page 1of 4


Explain the basic operation of a keypad. Describe the key press and detection mechanisms. Interface a keypad to the PIC18F4550 microcontroller.

Input and Output Interfacing: Keypad

Keypad Interfacing
Keypad or keyboards are the most widely used I/O devices. Most of microcontroller applications require a keypad for users to enter numbers and commands. A matrix keypad provides a simple data entry device for microcontroller systems.

Basic Operation
Organized in a matrix of rows and columns. Consists of an array of momentary pushbuttons switch or key. There are 4 column lines and 4 row lines. Each pushbutton has two terminals; one is connected to a column line and other to a row line.

Internal Connection of a 4 x 4 Keypad

Internal Structure of 4x4 Matrix Keypad.


2 . If one of the column bits has a zero. the microcontroller grounds all rows by providing 0 to the output latch.26-Jun-12 Basic Operation Initially. otherwise there is no connection between rows and columns. When a key is pressed. there are not direct connection between row and column. and the process continues until a key press has occurred. thus a row and column make a contact. the adjacent row and column are connected. when switch 3 is pressed. Row 3 and Column 2 will connect to each other. After a key press is detected. the Column 3 will become LOW Flowchart for Scanning Method To detect a pressed key. For example if key ‘8’ is pressed. the microcontroller will go through the process of identifying the key. this means that a key press has occurred. no key has been pressed. If the data read from the columns are equal to 1111. Key Press and Detection Mechanisms In programming there are 2 processes: Key press detection Interrupt method Scanning method – only this method will be covered Key identification Scanning Method for Key Press Detection As example: If Row 1 is set to LOW. then it reads the columns.

26-Jun-12 Flowchart for Scanning Method Starting with the top row. no key in that row is activated and the process is moved to the next row.High (RB7 . the next task is to find out which column the pressed key belongs to.Low(RB3 . Keypad Interfacing – PTK40A Schematic Keypad can directly being connected to MCU or used keypad decoder IC. then it reads the columns. an 8 x 8 matrix of keys can be connected to a microcontroller.RB4) Keyboard Debounce Noise Stable 3 . therefore with two 8-bit ports. MCU accesses both rows and columns through ports. If the data read is all 1s. This should be easy since the microcontroller knows at any time which row and column are being accessed. the microcontroller grounds it by providing a LOW to the first row only. Keypad Interfacing – PTK40A Schematic The keypad’s pins need to be pulled up or pulled down to avoid floating cases 4 x 4 matrix keypad connected to PORTB and PORTD Rows PORTB. After identification of the row in which the key has been pressed. reads the columns. It grounds the next row. and checks for any zero.RB0) Columns PORTB. This process continues until the row is identified.

if(KP_C3==0) return ‘3’. KP_R3 = 1. 4 . __delay_us(30). return ‘2’ If Column 3 = 0. Row3. KP_R2 = 1. return ‘3’ If Column 4 = 0. return ‘A’ Keypad Programming (continued) unsigned char Read_Keypad() { KP_R1 = 0. } //indicates nothing being pressed //return pressed key value //make Row1=0 //the rest =1 The process continue by making Row2. KP_R4 = 1. if(KP_C2==0) return ‘2’.26-Jun-12 Keypad Programming Steps (based on connection in Slide 3): Make Row 1 = 0 Delay Check every column If Column 1 = 0. return 0xFF. if(KP_C4==0) return ‘A’. if(KP_C1==0) return ‘1’. and Row 4 low one by one and then check the column. return ‘1’ If Column 2 = 0.