Interfacing Keypad with 8051 Microcontroller using Keil C

Interfacing Keypad with 8051 Microcontroller using Keil C

Matrix Keypad

Matrix Keypads are commonly used in calculators, telephones etc where a number of input switches are required. We know that matrix keypad is made by arranging push button switches in row and columns. In the straight forward way to connect a 4×4 keypad (16 switches) to a microcontroller we need 16 inputs pins. But by connecting switches in the following way we can read the status of each switch using 8 pins of the microcontroller.



The status of each keys can be determined by a process called Scanning. For the sake of explanation lets assume that all the column pins (Col1 – Col4) are connected to the inputs pins and all the row pins are connected to the output pins of the microcontroller. In the normal case all the column pins are pulled up (HIGH state) by internal or external pull up resistors. Now we can read the status of each switch through scanning.

  1. A logic LOW is given to Row1 and others (Row2 – Row-4) HIGH
  2. Now each Column is scanned. If any switch belongs to 1st row is pressed corresponding column will pulled down (logic LOW) and we can detect the pressed key.
  3. This process is repeated for all rows.

If you need to save more pins of your microcontroller then you can interface keypad using the ADC module of your microcontroller.

Interfacing with 8051 Microcontroller

Circuit diagram

Interfacing Keypad with 8051 Microcontroller using Keil C

Interfacing Keypad with 8051 Microcontroller using Keil C

10KΩ resistor and 10μF will provide the required Power On Reset (POR) signal to the 8051 microcontroller. 12MHz crystal is used to provide required clock for the microcontroller and 22pF capacitors will stabilize the oscillations of the crystal. AT89C51 can works upto 24MHz. We can choose the required frequency by changing the crystal and clock frequency in the project settings of Keil C. Keypad is connected to the Port P1 and column inputs pins are pulled up internally. 16×2 LCD is connected to Port P2 and P0. P0.0 and P0.1 pins are pulled up externally using 10KΩ resistors since Port P0 has no internal pull up.

Keil C Code

#include<reg52.h> //including sfr registers for ports of the controller

//LCD Module Connections
sbit RS = P0^0;
sbit EN = P0^1;
sbit D0 = P2^0;
sbit D1 = P2^1;
sbit D2 = P2^2;
sbit D3 = P2^3;
sbit D4 = P2^4;
sbit D5 = P2^5;
sbit D6 = P2^6;
sbit D7 = P2^7;
//End LCD Module Connections

//Keypad Connections
sbit R1 = P1^0;
sbit R2 = P1^1;
sbit R3 = P1^2;
sbit R4 = P1^3;
sbit C1 = P1^4;
sbit C2 = P1^5;
sbit C3 = P1^6;
sbit C4 = P1^7;
//End Keypad Connections

void Delay(int a)
  int j;
  int i;

char Read_Keypad()
  if(C1==0){Delay(100);while(C1==0);return '7';}
  if(C2==0){Delay(100);while(C2==0);return '8';}
  if(C3==0){Delay(100);while(C3==0);return '9';}
  if(C4==0){Delay(100);while(C4==0);return '/';}
  if(C1==0){Delay(100);while(C1==0);return '4';}
  if(C2==0){Delay(100);while(C2==0);return '5';}
  if(C3==0){Delay(100);while(C3==0);return '6';}
  if(C4==0){Delay(100);while(C4==0);return 'X';}
  if(C1==0){Delay(100);while(C1==0);return '1';}
  if(C2==0){Delay(100);while(C2==0);return '2';}
  if(C3==0){Delay(100);while(C3==0);return '3';}
  if(C4==0){Delay(100);while(C4==0);return '-';}
  if(C1==0){Delay(100);while(C1==0);return 'C';}
  if(C2==0){Delay(100);while(C2==0);return '0';}
  if(C3==0){Delay(100);while(C3==0);return '=';}
  if(C4==0){Delay(100);while(C4==0);return '+';}
  return 0;

void main()
  int i=0;
  char c,p;
    Lcd8_Write_String("Keys Pressed:");
    while(!(c = Read_Keypad()));
      while(!(c = Read_Keypad()));

The code consists of two user defined functions. The Delay() is used to make delay in the program execution. The Read_Keypad() reads the keypad. If any key is pressed it waits until the key is released and returns the corresponding character. If no key is being pressed it returns zero. As told before one of the pins of the row is kept at a logic 0 and the columns are checked for a logic 0. If a logic 0 is found the function returns a value according to the key pressed. This process is repeated for each row until a pressed key is found. If not found it will return zero.

This example project will show the character of key pressed and the number of times that key is pressed on the LCD screen.

while(!(c = Read_Keypad())) is used in the program to read keypad. It is a locking call as it will wait until a key is pressed and released.

Note : The lcd is interfaced using the ‘lcd.h’ header file which is to be included in the project folder. For more details about LCD interfacing please read our article Interfacing LCD with 8051 Microcontroller.

You can download Keil C files and Proteus files here…
Interfacing Keypad with 8051 using Keil C

Share this post

Comments (34)

  • kk

    nice article

    August 24, 2013 at 2:04 am
  • Jebin George

    please give me the lcd function to display a int variable

    February 23, 2014 at 10:34 pm
  • HQ Bui

    Thank You,

    March 11, 2014 at 7:50 am
  • bdkbaik

    Its only producing until “Times :”

    Besides that, it doesn’t scan my keypad input

    July 15, 2014 at 2:17 pm
  • Ligo George

    Just convert the integer to string and display..

    July 16, 2014 at 7:23 am
  • Jay

    what changes do i have to make if i interface keypad on port 0 and the RS and E pins of LCD on port 1..?
    will it work..?

    October 25, 2014 at 6:22 pm
  • Ligo George

    Try this :
    //LCD Module Connections
    sbit RS = P1^0;
    sbit EN = P1^1;
    sbit D0 = P2^0;
    sbit D1 = P2^1;
    sbit D2 = P2^2;
    sbit D3 = P2^3;
    sbit D4 = P2^4;
    sbit D5 = P2^5;
    sbit D6 = P2^6;
    sbit D7 = P2^7;
    //End LCD Module Connections

    //Keypad Connections
    sbit R1 = P0^0;
    sbit R2 = P0^1;
    sbit R3 = P0^2;
    sbit R4 = P0^3;
    sbit C1 = P0^4;
    sbit C2 = P0^5;
    sbit C3 = P0^6;
    sbit C4 = P0^7;
    //End Keypad Connections
    Don’t forget to connect PULL UP resistors to PORT 0

    October 26, 2014 at 10:04 am
  • lui

    i could not get it to compile in KEIL it said::: keil keypad n LCD.c(88): warning C206: ‘Lcd8_Set_Cursor’: missing function-prototype

    November 3, 2014 at 9:24 am
  • Ligo George

    Please refer the following link for lcd library..

    November 4, 2014 at 9:23 pm
  • 107

    how can we reduce number of pins using ADC as you said above

    January 1, 2015 at 1:45 am
  • Ligo George

    Sorry, I haven’t yet posted a tutorial about it.
    You can search google ADC Keypad Microcontroller etc.

    January 3, 2015 at 8:43 am
  • Vinod Rajbhar

    mujhe induction motor ko control karna hai aur slowaly slowaly start aur slowaly slowaly stop

    February 6, 2015 at 2:00 am
  • Prakash

    how can display different values with single key on LCD

    March 31, 2015 at 10:04 am
  • Ligo George

    I don’t understand you.

    April 5, 2015 at 1:04 pm
  • Akshay Nehete

    what should i change in above program,
    if i want to enter only 4 digit code using keypad and this four digits should be displayed on lcd???

    April 23, 2015 at 10:32 pm
  • suresh don

    same function of keypad i.e.,Read_Keypad() is not compaitable with PIC16f877a controller?….I saw your post regarding that…but it seems to be some what easy to me to understand

    May 13, 2015 at 5:03 pm
  • suresh don

    what are the modifications to be done in this code for interfacing keypad with pic16f877a

    May 13, 2015 at 5:08 pm
  • suresh kumar

    sir same keypad function is not suitable for PIC16f877a microcontroller……i am trying it in MPLAB…it shows build successful but it is not working in proteus simulation…But this function is easy to me than the post of u about keypad interfacing with pic microcontroller..

    May 14, 2015 at 7:31 pm
  • Sunday kc chima

    pls can you tech me how to do programming

    May 26, 2015 at 10:22 am
  • nagaraj

    sir above program could not get compile in keil c Build target ‘Target 1’
    compiling key pad.c…
    key pad.c(2): warning C318: can’t open file ‘lcd.h’
    key pad.c(84): error C202: ‘lcd’: undefined identifier
    Target not created

    July 22, 2015 at 5:34 pm
  • nagaraj

    sir what can i do modification for this program

    July 22, 2015 at 5:36 pm
  • Ligo George

    Add the file lcd.h to your project.

    August 14, 2015 at 9:16 pm
  • Ligo George

    Of course you can do it.

    August 14, 2015 at 9:16 pm
  • Jyoti

    Hi. How to get continuous data if key is keep pressed and the single data for single press. But the rate of getting data for keep pressing shuld be faster than single press.

    November 20, 2015 at 11:39 pm
  • Jyoti

    So, Mr Ligo, Can you please tell me this one please.

    November 21, 2015 at 10:22 am
  • Ligo George

    You need to modify the program for that. Just try replacing while(); in the checking with if()

    November 21, 2015 at 2:36 pm
  • pulluri naga vikas

    if two switches are pressed at a time what wil happen…?
    i need switch matrix in that 4 or 5 switches pressed at a time…

    January 3, 2016 at 9:15 am
  • Ligo George

    As per the program, it will detect only the first checking. For detecting multiple keys you can try like this article :

    January 24, 2016 at 11:32 am
  • farhan

    sir i have to control speaker system with micro controller with keypad,, so, can you help me plz, how i connect speakers with micro controller and controlled with keypad

    October 29, 2016 at 9:16 pm
  • Ligo George

    Kindly use our forums ( ) for asking dobuts autside the scope of above article.

    November 27, 2016 at 2:26 pm
  • hari

    bro i want to take string i/p from keypad help me

    February 13, 2017 at 12:30 pm
  • soorya

    can anyone give me the advantages of this system over sensors..if any..also the applications

    March 16, 2017 at 12:04 pm
  • Tejashree Shelar

    How to add two digit number and display it on LCD?

    April 14, 2017 at 10:06 am
  • Styllish Harsha

    lcd.c(89): error C267: ‘Lcd8_Set_Cursor’: requires ANSI-style prototype
    it was showing this error msg

    November 9, 2017 at 10:56 am

Leave a Reply

Your email address will not be published. Required fields are marked *