Login

Lost your password?
Don't have an account? Sign Up

Interfacing 4×4 Matrix Keypad with 8051 (AT89C52)

Would you like to join our comprehensive practical video course on the 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:

KeyRowColumn
1R0C0
2R0C1
3R0C2
AR0C3
4R1C0
5R1C1
6R1C2
BR1C3
7R2C0
8R2C1
9R2C2
CR2C3
*R3C0
0R3C1
#R3C2
DR3C3

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

  1. Set all column pins to HIGH and row pins as output.
  2. Pull one row LOW at a time.
  3. Read column pins — if any column reads LOW, the key at that row-column intersection is pressed.
  4. Identify the key using a lookup table.
  5. Debounce delay to avoid false readings.
  6. 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);
    }
}
C

Interfacing Keypad matrix with 8051

Working Explanation

  1. The microcontroller sets each row LOW sequentially while monitoring the column inputs.
  2. When a key is pressed, it connects one row and one column, making that column LOW.
  3. The program identifies the pressed key using the row–column combination.
  4. 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.

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!

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.

Home » Recent posts » Learn › 8051>Interfacing Keypad matrix with 8051

https://bitziga.com

Leave a Comment

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

*
*