
Interfacing 4×4 Matrix Keypad with 8051 (AT89C52)
Interfacing Keypad matrix with 8051
Would you like to join our comprehensive practical video course on the AT89C52 Microcontroller?
Complete Video Course on AT89C52 Microcontroller

Introduction
Interfacing Keypad matrix with 8051: A matrix keypad is a simple and efficient human–machine interface commonly used in embedded systems for data entry, password input, and menu navigation. A 4×4 matrix keypad consists of 16 keys arranged in 4 rows and 4 columns. The AT89C52 microcontroller can scan this keypad using digital I/O pins and detect which key is pressed based on the intersection of rows and columns. In this tutorial, you’ll learn how to interface a 4×4 matrix keypad with the AT89C52 microcontroller, understand its working, and write an Embedded C program to display the pressed key on a 16×2 LCD using Keil µVision and Proteus simulation.
Keypad Structure and Working Principle
The 4×4 keypad is arranged as a grid of rows (R0–R3) and columns (C0–C3). Each key is placed at the intersection of one row and one column.
When a key is pressed, it connects a specific row line to a column line. The microcontroller scans the matrix by setting one line LOW at a time and checking which column goes LOW, thus identifying the pressed key.
Pin Layout:
| Key | Row | Column |
|---|---|---|
| 1 | R0 | C0 |
| 2 | R0 | C1 |
| 3 | R0 | C2 |
| A | R0 | C3 |
| 4 | R1 | C0 |
| 5 | R1 | C1 |
| 6 | R1 | C2 |
| B | R1 | C3 |
| 7 | R2 | C0 |
| 8 | R2 | C1 |
| 9 | R2 | C2 |
| C | R2 | C3 |
| * | R3 | C0 |
| 0 | R3 | C1 |
| # | R3 | C2 |
| D | R3 | C3 |
Circuit Description
- Connect Rows (R0–R3) of the keypad to Port 2 (P2.0–P2.3).
- Connect Columns (C0–C3) to Port 2 (P2.4–P2.7).
- A 16×2 LCD is connected to display the pressed key:
- Data pins D0–D7 → Port 0 (P0.0–P0.7)
- Control pins RS → P3.0, EN → P3.1, RW → GND.
- A 10kΩ potentiometer is connected to VEE of LCD for contrast control.
Algorithm for Key Detection
- Set all column pins to HIGH and row pins as output.
- Pull one row LOW at a time.
- Read column pins — if any column reads LOW, the key at that row-column intersection is pressed.
- Identify the key using a lookup table.
- Debounce delay to avoid false readings.
- Display the pressed key on LCD.
Example Code: Keypad Interfacing with AT89C52
#include <reg51.h>
#define lcd P0
sbit rs = P3^0;
sbit en = P3^1;
void delay(unsigned int t)
{
unsigned int i, j;
for(i=0;i<t;i++)
for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char c)
{
lcd = c;
rs = 0; en = 1; delay(2); en = 0;
}
void lcd_data(unsigned char d)
{
lcd = d;
rs = 1; en = 1; delay(2); en = 0;
}
void lcd_init()
{
lcd_cmd(0x38);
lcd_cmd(0x0C);
lcd_cmd(0x06);
lcd_cmd(0x01);
lcd_cmd(0x80);
}
void lcd_string(char *s)
{
while(*s)
lcd_data(*s++);
}
char keypad_scan()
{
unsigned char row, col;
P2 = 0xF0; // upper nibble inputs, lower nibble outputs
while(1)
{
for(row=0; row<4; row++)
{
P2 = ~(1 << row); // Set one row low at a time
col = P2 & 0xF0; // Read column lines
if(col != 0xF0)
{
delay(10); // Debounce delay
switch(col)
{
case 0xE0: return (row*4 + 0); // C0 low
case 0xD0: return (row*4 + 1); // C1 low
case 0xB0: return (row*4 + 2); // C2 low
case 0x70: return (row*4 + 3); // C3 low
}
}
}
}
}
void main()
{
unsigned char key;
char keys[16] = {'1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'};
lcd_init();
lcd_string("Keypad Ready");
lcd_cmd(0xC0);
lcd_string("Press a Key:");
while(1)
{
key = keypad_scan();
lcd_cmd(0xC0);
lcd_data(keys[key]);
delay(500);
}
}CInterfacing Keypad matrix with 8051
Working Explanation
- The microcontroller sets each row LOW sequentially while monitoring the column inputs.
- When a key is pressed, it connects one row and one column, making that column LOW.
- The program identifies the pressed key using the row–column combination.
- The pressed key character is displayed on the LCD in real time.
Circuit Design in Proteus
In Proteus, place:
- AT89C52 microcontroller
- 4×4 Matrix Keypad
- 16×2 LCD Display
- 10kΩ potentiometer (for LCD contrast)
Connections: - Keypad rows → P2.0–P2.3
- Keypad columns → P2.4–P2.7
- LCD data → Port 0, RS → P3.0, EN → P3.1, RW → GND
Run the simulation and press different keys on the virtual keypad — the corresponding character appears on the LCD.
Applications
- Digital password locks
- Menu selection systems
- Home automation control interfaces
- Security and keypad-based entry systems
Summary
The 4×4 matrix keypad provides a simple way to input numeric or alphanumeric data into the AT89C52 microcontroller. By scanning rows and columns efficiently, you can detect keypresses accurately and use them for controlling applications like password entry or menu navigation.
Learn More
Do you want to go beyond just configuration and build real embedded projects?
🎓 Join our Complete Practical Video Course to Master the AT89C52 Microcontroller.
Get step-by-step guidance on Timers, Interrupts, UART, I/O Interfacing, ADC (using ADC0804), and I²C/SPI (Software Implementation) — with real circuits, simulations, and hardware demonstrations!
Complete Video Course on AT89C52 Microcontroller

Downloading and Installing Keil µVision IDE
- Visit the official Keil website at https://www.keil.com or KEIL IDE
- Navigate to the Downloads → C51 Compiler section.
- Register (if required) and download the latest version of Keil µVision for 8051 (C51).
- Run the installer and follow the on-screen instructions.
- Once installed, launch the Keil µVision IDE from the Start Menu.


