You have no items in your shopping cart.

Subtotal: 0.00

How to Interface Keypad with 8051 Advanced Development Board

8051 Advanced Development Board

The 8051 Advanced Development 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 serial port.

NXP’s 8051 (AT89V51RD2), 8051 Advanced Development Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of 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 8051

We now want to scan a keypad in 8051 Advanced Development 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 J5, all row lines(P2.4 – P2.7) and column lines(P2.0 to P2.3) connected directly by the port pins.

Pin Assignment with 8051


4x4 Matrix Lines

8051 Lines

4x4 Matrix Keypad




















Circuit Diagram to Interface keypad with 8051

Source Code

The Interfacing keypad with 8051 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 Keil software.

C Program to 4 X 4 matrix keypad using 8051


Title : Program to keypad interfacing


#include <stdio.h> //Define I/O Functions #include <reg51.h> //Define 8051 Registers #define DATA P1 //Define DATA to Port1 void lcd_init(void); //LCD Initialization void lcd_cmd(unsigned char); //LCD Command Function void lcd_display(unsigned char); //LCD Display Function void Key_Scan(void); //KeyScan Function void DelayMs(int); //DelayMs Function sbit RS = P3^5; //Register Select sbit RW = P3^6; //LCD Read/Write sbit lcd_e = P3^7; //LCD Enable unsigned char R,C,ch,d=0x81; unsigned int i=0; unsigned char Key[4][4] = {'C','D','E','F', //Matrix Keypad Character '8','9','A','B', //Initialization '4','5','6','7', '0','1','2','3',}; code unsigned char msg[] = ("KEYPAD TEST PGM "); //Display the Message code unsigned char msg1[] = ("PRES ANY KEY... "); //in LCD //------------------------------- // Main Program //------------------------------- void main() { lcd_init(); DelayMs(20); lcd_cmd(0x01); lcd_e=0; lcd_cmd(0x80); while(1) { Key_Scan(); lcd_e=1; DelayMs(5); ch = Key[R][C]; //Assign Key value to ch; DelayMs(5); //and Row Value of Keypad lcd_display(ch); lcd_cmd(d++); DelayMs(10); P2=0xFF; } } //------------------------------- // Key Scan Function //------------------------------- void Key_Scan(void) { unsigned int i = 0; //Scanning for Row Value P2 = 0x0F; //Initialize Port2 to 0Fh while(P2 == 0x0F); if(P2 == 0x0E) //Checking for Row0 R = 0; else if(P2 == 0x0D) //Checking for Row1 R = 1; else if(P2 == 0x0B) //Checking for Row2 R = 2; else if(P2 == 0x07) //Checking for Row3 R = 3; //Scanning for Column Value P2 = 0xF0; //Initialize Port2 to F0h while(P2 == 0xF0); if(P2 == 0xE0) //Checking for Column0 C = 0; else if(P2 == 0xD0) //Checking for Column1 C = 1; else if(P2 == 0xB0) //Checking for Column2 C = 2; else if(P2 == 0x70) //Checking for Column3 C = 3; DelayMs(20); } //------------------------------- // LCD command Function //------------------------------- void lcd_cmd(unsigned char cmnd) { DATA = cmnd; RS = 0; //RS:Register Select RW = 0; //RW:Read/Write lcd_e = 1; //LCD Enable DelayMs(15); lcd_e = 0; } //------------------------------- // LCD Data Function //------------------------------- void lcd_display(unsigned char dat) { DATA = dat; RS = 1; //RS:Register Select RW = 0; //RW:Read/Write lcd_e = 1; DelayMs(15); lcd_e = 0; } //------------------------------- // LCD Initialization //------------------------------- void lcd_init(void) { unsigned char i; lcd_cmd(0x38); //2x16 Character 5x7 dot DelayMs(15); //matrix LCD,8-bit format lcd_cmd(0x0c); //Display On, cursor off DelayMs(15); lcd_cmd(0x06); //Shift Cursor to right DelayMs(15); lcd_cmd(0x01); //Clear display screen DelayMs(15); //------------------------------------ // First Line Message Display //------------------------------------ lcd_cmd(0x80); //First Line Initialization DelayMs(15); i=0; while(msg[i]!='\0') { lcd_display(msg[i]); i++; } DelayMs(20); //------------------------------------ // Second Line Message Display //------------------------------------ lcd_cmd(0xc0); //Second Line Initialization DelayMs(15); i=0; while(msg1[i]!='\0') { lcd_display(msg1[i]); i++; } DelayMs(30); } //-------------------------- // DelayMs Function //-------------------------- void DelayMs(int k) { unsigned int a,b; for(a=0;a<=k;a++) for(b=0;b<1275;b++); }