How to Interface Keypad with PIC16F877A

keypad Interfacing With PIC16F877A Slicker

Tags: Interfacing keypad with PIC16F877A, Circuit Diagram to Interface keypad with PIC16F877A,C Program to 4 X 4 matrix keypad using PIC16F877A, source code to interface keypad with pic microcontrollers,
Overall rating
PIC16F/18F Slicker Board

The PIC16F/18F Slicker board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports in system programming (ISP) which is done through USB port.

Microchip’s PIC (PIC16F877A), PIC16F/18F Slicker Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 8-bit Microcontrollers.


A keypad is a set of buttons arranged in a block or "pad" which usually bear digits, symbols and usually a complete set of alphabetical letters. If it mostly contains numbers then it can also be called a numeric keypad. Here we are using 4 X 4 matrix keypad.

Interfacing keypad

Fig. 1 shows how to interface the 4 X 4 matrix keypad to two ports in microcontroller. The rows are connected to an output port and the columns are connected to an input port.

To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, and then it reads the columns. If the data read from the columns is D3-D0=1111, no key has been pressed and the process continues until a key press is detected. However, if one of the column bits has a zero, this means that a key press has occurred. For example, if D3-D0=1101, this means that a key in the D1 column has been pressed.

After a key press is detected, the microcontroller will go through the process of identifying the key. Starting with the top row, the microcontroller grounds it by providing a low to row D0 only; then it reads the columns.

If the data read is all 1s, no key in that row is activated and the process is moved to the next row. It grounds the next row, reads the columns, and checks for any zero. This process continues until the row is identified. After identification of the row in which the key has been pressed, the next task is to find out which column the pressed key belongs to.


Fig. 1 Interfacing keypad to Microcontroller

Interfacing keypad with PIC16F877A

We now want to scan a keypad in PIC16F/18F Slicker Board. In case of 4X4 matrix Keypad both the ends of switches are connected to the port pin i.e. four rows and four columns. So in all sixteen switches have been interfaced using just eight lines.

1Keypads arranged by matrix format, each row and column section pulled by high or low by selection J15, all row lines(PORTB.0 – PORTB.3) and column lines(PORTB.4 to PORTB.7) connected directly by the port pins.

Pin Assignment with PIC16F877A



4x4 Matrix Lines

PIC16F Lines

4x4 Matrix Keypad




*Connect PORTB with JP15 (Keypad) via FRC Cable


*Turn ON TXD and RXD Pins of CONFIG switch SW1.


*Connect Serial cable between USART Section in the

Board and PC and open HyperTerminal


*Press RESET once.















Press a Key and the number will be displayed in Hyper-





Circuit Diagram to Interface keypad with PIC16F877A


Source Code

The Interfacing keypad with PIC16F877A program is very simple and straight forward, that scan a keypad rows and columns. When the rows and columns are detected then it will display in PC through UART0. The C programs are developed in Mplab software with Hi-Tech Compiler.

C Program to 4 X 4 matrix keypad using PIC16F877A


Title : Program to keypad interfacing




#include <pic.h>     // Define PIC Registers

#include <stdio.h>   // Define I/O functions



// Select HS oscillator, Enable (PWRTE,BOREN),

// Disable (CPD,CP,WDTEN,In-circuit Debugger)


#define FOSC       10000

#define BAUD_RATE  9.6   //9600 Baudrate

#define BAUD_VAL   (char)(FOSC/ (16 * BAUD_RATE )) - 1;      //Calculation For 9600 Baudrate @10Mhz


void SerialInit(void); //Serial port Initialization Function

void ScanCol(void);    //Column Scan Function

void ScanRow(void);    //Row Scan Function

void DelayMs(unsigned int);


unsigned char KeyArray[4][4]= {   '1','2','3','4',     




//Keypad value Initialization Function


unsigned char Count[4][4]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

int Col=0,Row=0,count=0,i,j;


void main()




   nRBPU=0;    //Enable PORTB Pullup values




      TRISB=0x0f; // Enable the 4 LSB as I/P & 4 MSB as O/P


      while(PORTB==0x0f);   // Get the ROW value



      TRISB=0xf0; // Enable the 4 LSB as O/P & 4 MSB as I/P


      while(PORTB==0xf0);   // Get the Column value




      Count[Row][Col]++;    // Count the Pressed key


      printf("[%c] Pressed:[%d] Times\n\r",






void ScanRow()       // Row Scan Function





      case 0x07:

         Row=3;      // 4th Row


      case 0x0b:

         Row=2;      // 3rd Row


      case 0x0d:

         Row=1;      // 2nd Row


      case 0x0e:

         Row=0;      // 1st Row





void ScanCol()    // Column Scan Function





      case 0x70:

         Col=3;   // 4th Column


      case 0xb0:

         Col=2;   // 3rd Column


      case 0xd0:

         Col=1;   // 2nd Column


      case 0xe0:

         Col=0;   // 1st Column





void SerialInit()


   TRISC=0xc0;       // RC7,RC6 set to usart mode(INPUT)  


//Enable Serial Transmission, Asynchronous mode, High Speed mode


   SPBRG=BAUD_VAL;   // 9600 Baud rate selection

   RCSTA=0x90;     // Enable Serial Port & Continuous Receive


   printf("Press Anyone Key:\n\r");



void putch(unsigned char character)


   while(!TXIF);   // Wait for the TXREG register to be empty

   TXREG=character;  // Display the Character




void DelayMs(unsigned int Ms)


   int delay_cnst;




      for(delay_cnst = 0;delay_cnst <220;delay_cnst++);






To compile the above C code you need the Mplab software & Hi-Tech Compiler. They must be properly set up and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.

In Mplab, you want to develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without PIC16F/18F Slicker Board.

The PICKIT2 software is used to download the hex file into your microcontroller IC PIC16F877A through USB port.

Testing the Keypad with PIC16F877A

Give +12V power supply to PIC16F/18F Slicker Board; the serial cable is connected between the PIC16F/18F Slicker Board and PC.

Open the Hyper Terminal screen, select which port you are using and set the default settings. Now the screen should show some text messages & it display which key is pressed in keypad.

If you want to see more details about debugging just see the videos in below link.

General Information

  • For proper working use the components of exact values as shown in Circuit file. Wherever possible use new components.
  • Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints.
  • Use the exact value crystal shown in schematic.
  • More instructions are available in following articles,
       - User Manual of PIC16F/18F Slicker Board.
       - Create & Debug a project in Mplab using PIC16F877A.
       - Interfacing Switches with PIC16F877A.

Join the World's Largest Technical Community

we respect your privacy.