KEYPAD INTERFACING WITH PIC MICROCONTROLLER,In this tutorial, you will learn a very simple method of interfacing a keypad with PIC microcontroller. Before we begin with the lesson of keypad interfacing, it is assumed that you know how to interface an LCD with PIC16F877A microcontroller. If you Don’t know about how to interface LCD with PIC16F877A microcontroller. Check this article :”LCD interfacing with PIC16F877A microcontroller

Keypad interfacing with PIC16F877A microcontroller


To interface a keypad with PIC16F877A and display the pressed digits/characters on the LCD.


Keypad are available in various different sizes. The common sizes include 3×3, 3×4 and 4×4. In this project, we will interface a numeric keypad of 4×4 matrix with the PIC microcontroller 16F877A. The complete circuit diagram, designed on proteus, is given below:


Circuit diagram of LCD interfacing with pic microcontroller is given below:

Keypad Interfacing with pic16f877a microcontroller
Keypad Interfacing with pic16f877a microcontroller


Make all the connections as shown in the schematic diagram above. Keypad is connected to PORTD of the PIC microcontroller 16F877A. The four rows,namely A, B, C and D, are connected to the lower significant bits of the PORT (RD0-RD3) and the four columns,numbered as 1, 2, 3 and 4,are connected to the MSB of the PORT (RD4-RD7).The LCD module, crystal oscillator and remaining components will be connected to the controller in the similar fashion as described in the previous article.

 Video demonstration of keypad interfacing

Keypad Mikro c for pic library

Keypad_Init it initializes port for working with keypad. It returns noting. For example

char keypadPort at PORTB;


Keypad_Key_Press it reads the key from keypad when key is pressed. It returns the code of pressed key. If no key is pressed it returns 0. For this port needs to be initialized for working with the Keypad library. For example

            char key;

key = Keypad_Key_Press();

Keypad_Key_Click this function is blocking call which means the function waits until some key is pressed and released. When the key is released, the function returns key code from 1 to 16, depending on the key. If more than one key is pressed simultaneously the function will wait until all pressed keys are released. After that the function will return the code of the first pressed key. It returns the code of a clicked key. If no key is clicked it returns 0. For example

char key;

key = Keypad_Key_Click();


Just like for LCD, MikroC also provides built-in library functions for a 4×4 Keypad. These library routines, however, can be used with other sizes of keypad as well. Functions, such as keypad_init, keypad_key_clicketc, simply needs to be defined in the program, and rest of the inner operations are performed by the mikroC compiler.Write the following code in mikroC to interface keypad with PIC MCU:


int kpi;
// Keypad module connections
char keypadPort at PORTD;
// End Keypad module connections
// LCD Module connections
sbit LCD_RS at RB1_bit; // it can only access a pin not a port
//sbit LCD_RS at LATB1_bit; //same as "sbit LCD_RS at RB1_bit;"
//sbit LCD_RS at LATB.B1; //SAME AS "sbit LCD_RS at RB1_bit;"
//sbit LCD_RS at LATB;
//sbit LCD_RS at PORTB1_bit;
//sbit LCD_RS at PORTB.B1;
//sbit LCD_RS at PORTB;
sbit LCD_EN at RB0_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D4 at RB2_bit;
// End LCD module connections
// LCD Pin direction
sbit LCD_RS_Direction at TRISB1_bit;
sbit LCD_EN_Direction at TRISB0_bit;
sbit LCD_D7_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB2_bit;
// End of LCD Pin direction
void main() 
 ANSELH = 0X00;
 TRISB = 0X00;
Lcd_Init(); // Initializing LCD
Lcd_Cmd(_LCD_CLEAR); // Clear Display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor Off
Lcd_Out(1,1,"KEYPAD INTERFACE"); // Write "KEYPAD INTERFACE" in the first row
delay_ms(500); // 0.5s delay
Lcd_Cmd(_LCD_CLEAR); // Clear Display
delay_ms(500); // 0.5s delay
Keypad_Init(); // Initializing Keypad
Lcd_Out(1,1,"PRESS A KEY"); // Write "PRESS A KEY" in the first row
delay_ms(500); // 0.5s delay
Lcd_Cmd(_LCD_CLEAR); // Clear Display
 kpi = 0; // Reset key code variable
// Wait for key to be pressed and released
 kpi = Keypad_Key_Click(); // Store key code in kpi variable
 while (!kpi);
 switch (kpi)
 case 1: kpi = 49; break; // 7-Cmp kpi with equivalent ASCII code of 7, break if equal
 case 2: kpi = 50; break; // 4
 case 3: kpi = 51; break; // 1
 case 4: kpi = 65; break; // Space
 case 5: kpi = 52; break; // 8
 case 6: kpi = 53; break; // 5
 case 7: kpi = 54; break; // 2
 case 8: kpi = 66; break; // 0
 case 9: kpi = 55; break; // 9
 case 10: kpi = 56; break; // 6
 case 11: kpi = 57; break; // 3
 case 12: kpi = 67; break; // =
 case 13: kpi = 42; break; // /
 case 14: kpi = 48; break; // x
 case 15: kpi = 35; break; // -
 case 16: kpi = 68; break; // +
 Lcd_Chr(1, 2, kpi); // Print key ASCII value on Lcd
while (1);

A variable ‘kpi’ is declared in the beginning of the code, that takes value from the keypad and displays it on the LCD screen.The program first displays‘Keypad Interface’ and ‘Press a key’ on the LCD screen, separated by a 0.5s delay. The program then enters the ‘do-while’ loop and waits for a key to be pressed. As soon as any key is pressed, the inner ‘while’ loop is initiated and the ASCII code of that key is compared with the ASCII codes of all the characters present on the keypad. In case of a match, the program comes out of the while loop, displays the pressed key on the LCD screen and returns to the start of ‘do-while’ loop to wait for the next key press. The process is repeated, until program is aborted.


Keypads have been used extensively in automotive applications as well as food industries. Programmed Keypads can be used in automated attendance system at schools, offices etc, where you enter your ID, which is displayed and at the same time stored, to mark your presence. Automatic door locks are usually accessed with a keypad control system in which a particular code is dialed on the keypad to open the door.To downlaod circuit diagram and code of above project click on following link: Code and circuit diagramI hope after reading this article, you can use keypad in your project very easily. If you come around any issue after reading this article, your comments are welocme. Kindly share this article with your friends thanks 🙂


  1. Hello
    I tried your keypad code but it errors with Too many characters and Implicit conversion of Int to ptr at line 27.
    Is there a solution?

  2. Hi i really need help on how to make door lock using 4*3 keypad, 2*16 lcd, solenoid lock and simpp pic.this pic is made by own prof. And i dont know the connectio and code can u suggest or help me please i really need to pass this project.please100x. This is my gmail
    Thank you

  3. Hi Mr. Malik,
    I have a project for MicroControllers Course: converting users’s input decimal numbers to binary and as well, i must use Keypad and LCD and PIC16F877A . I am using MikroC Pro and your blog is very beneficial for me sure.Is it enough making this circuit for my project ?

  4. Hi Malik,
    I have a small project operating switch points at my model railroad using Arduino.
    I use a keypad to generate funktions to each of my 32 switch points via I2C bus.
    One, keypad(0X39), one 16x2LCD(0X3F), four PCF8575(0X38 – 0X35). Drivers for the point motors (solenoinds) is 8 of ULN2903. I have no problem communicating between the Keypad and LCD display.
    Problem is to write the code for the slaves. How do I make the right slave to get the message, displayed on the LCD? I have tried with “1-8 should write to 0X35″, 9-16 should write to 0X36” and so on. No success!

    There is two possibillities for the point motor, straight or curved.
    I would like to use number + * for straight
    I would like to use number + # for curve

    Each point motor will use two outputs from ULN2803 one for each solenoid spool.

    Please help ! How do I write it in the code?

  5. Hello bilal this sight is awesome for people like me, you have explored so many I am looking for a sight like this… in this programing when you entered any will displayed it on the LCD right?..what about the 2nd char…where it will didn’t mention for moving cursor right…? Or am I wrong

  6. Hi Bilal,
    Thanks for another master piece. I benefit a lot from your articles on your site. I am still learning the ropes. After going through your code, Sir, I noticed with all due respect that some information provided by you are confusing, This I noticed could be as a result of the way the keypad was connected to the MCU.
    I know you adjusted your code to get what you wanted and in that process, the desparity crept into the code.
    Specifically I respectfully refer to these lines of your code:
    Case 5 : kpI=53; //8
    And some other lines are affected as well.
    The ASCII for 5 is 53 as stated which is correct but your comment says it is key 8. I expected key 5. That left me confused.
    I simulated the code on Proteus , I discovered that the pin assignment for PortD to the key is the issue.
    The keypad Init() function of the keypad library does not specify how the pins are assigned to the pad and that could be he source of confusion.
    Sir, here is the connection I used in Proteus to get what I wanted.:
    D0. Row1 // input
    D1. Row2 // input
    D2. Row3 // input
    D3. Row4 // input. Ignore for 4×3 keypad
    D4 Col1 (A) // output
    D5. Col2 (B) // output
    D6. Col3 (C) // output
    D7. Col4 (4) // output
    Sir, with this configuration, surely, all information will conform.
    May I use this opportunity to thank you for the numerous helpful tutorials you post on this site.
    Sir, could you please post another article on keypad interfacing using this information to enable out learners like me learn the code without getting confused.


Leave a Comment