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


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



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:


  1. int kpi;
  2. // Keypad module connections
  3. char keypadPort at PORTD;
  4. // End Keypad module connections
  5. // LCD Module connections
  6. sbit LCD_RS at RB2_bit;
  7. sbit LCD_EN at RB3_bit;
  8. sbit LCD_D7 at RB7_bit;
  9. sbit LCD_D6 at RB6_bit;
  10. sbit LCD_D5 at RB5_bit;
  11. sbit LCD_D4 at RB4_bit;
  12. // End LCD module connections
  13. // LCD Pin direction
  14. sbit LCD_RS_Direction at TRISB2_bit;
  15. sbit LCD_EN_Direction at TRISB3_bit;
  16. sbit LCD_D7_Direction at TRISB7_bit;
  17. sbit LCD_D6_Direction at TRISB6_bit;
  18. sbit LCD_D5_Direction at TRISB5_bit;
  19. sbit LCD_D4_Direction at TRISB4_bit;
  20. // End of LCD Pin direction
  21. void main() {
  22. Lcd_Init(); // Initializing LCD
  23. Lcd_Cmd(_LCD_CLEAR); // Clear Display
  24. Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor Off
  25. Lcd_Out(1,1,"KEYPAD INTERFACE");  // Write "KEYPAD INTERFACE" in the first row
  26. delay_ms(500); // 0.5s delay
  27. Lcd_Cmd(_LCD_CLEAR);                   // Clear Display
  28. delay_ms(500); // 0.5s delay
  29. Keypad_Init(); // Initializing Keypad
  30. Lcd_Out(1,1,"PRESS A KEY"); // Write "PRESS A KEY" in the first row
  31. delay_ms(500); // 0.5s delay
  32. Lcd_Cmd(_LCD_CLEAR); // Clear Display
  33. do
  34. {
  35. kpi = 0; // Reset key code variable
  36. // Wait for key to be pressed and released
  37. do
  38. kpi = Keypad_Key_Click(); // Store key code in kpi variable
  39. while (!kpi);
  40. switch (kpi)
  41. {
  42. case 1: kpi = 55; break; // 7-Cmp kpi with equivalent ASCII code of 7, break if equal 
  43. case 2: kpi = 52; break; // 4
  44. case 3: kpi = 49; break; // 1
  45. case 4: kpi = 32; break; // Space
  46. case 5: kpi = 56; break; // 8
  47. case 6: kpi = 53; break; // 5
  48. case 7: kpi = 50; break; // 2
  49. case 8: kpi = 48; break; // 0
  50. case 9: kpi = 57; break; // 9
  51. case 10: kpi = 54; break; // 6
  52. case 11: kpi = 51; break; // 3
  53. case 12: kpi = 61; break; // =
  54. case 13: kpi = 47; break; // /
  55. case 14: kpi = 42; break; // x
  56. case 15: kpi = 45; break;  // -
  57. case 16: kpi = 43; break; // +
  58. }
  59. Lcd_Chr(1, 2, kpi); // Print key ASCII value on Lcd
  60. } while (1);
  61. }

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 diagram

I 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. Johm January 3, 2016
    • BILAL Malik January 3, 2016
  2. jude August 3, 2016
  3. Jean October 19, 2016
  4. Omer Akatekin December 16, 2016
  5. Max Lindström February 13, 2017

Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 899 other subscribers