KEYPAD INTERFACING WITH 8051(AT89S52) WITH SIMULATION IN PROTEUS

Photo of author

By Jackson Taylor

Home » Electronics Topics » 8051 Microcontroller » KEYPAD INTERFACING WITH 8051(AT89S52) WITH SIMULATION IN PROTEUS

As an Amazon Associate we earn from qualifying purchases.

A keypad is one of the simplest ways to get input from a user in a microcontroller project. Door locks, calculators, and menu-driven devices all use one. This tutorial shows how to connect a 12-key (4×3) keypad to an AT89S52 (8051 family) microcontroller, read the pressed key, and show it on an LCD. Everything is simulated in Proteus, so you can test it before building the hardware.

What you need

  • COMPILER – Mikro C PRO
  • SIMULATION – Proteus 8.1
  • MICROCONTROLLER – AT89S52
  • CRYSTAL – 11.0592MHz (use this crystal to get a baud rate of 9600)

The 11.0592 MHz crystal matters mainly for serial communication. If you want to use the serial port later, see our guide on UART communication on the 8051. The keypad itself works with other crystal values too. Just make sure the clock frequency in your mikroC project matches the one set in Proteus, because the delay functions depend on it.

How a 4×3 keypad works

A phone-style keypad has 12 buttons but only 7 pins. The buttons are wired as a matrix of 4 rows and 3 columns. Pressing a key connects one row line to one column line. This saves a lot of pins: 12 separate switches would need 12 inputs, while the matrix needs only 4 + 3 = 7.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

The microcontroller finds the pressed key by scanning:

  1. Set all column pins high.
  2. Pull one column low.
  3. Read the four row pins. A row that reads 0 means a key in that row and the active column is pressed.
  4. Repeat for the next column.

Port 1 on the 8051 has internal pull-ups, so the row pins read high when nothing is pressed. If you only need a single button, switch interfacing with 8051 covers the basics.

Hardware

Circuit and pin connections

The keypad rows and columns connect to the microcontroller as follows, matching the program below:

  • Rows: R1 to P1.0, R2 to P1.1, R3 to P1.2, R4 to P1.3 (keypad pins 1 to 4)
  • Columns: C1 to P3.3, C2 to P3.4, C3 to P3.5 (keypad pins 5 to 7)
  • LCD: RS to P2.3, EN to P2.2, D4 to P2.1, D5 to P2.0, D6 to P2.4, D7 to P2.5
See also
CUK CONVERTER SIMULATION AND HARDWARE USING ARDUINO AND MATLAB SIMULINK

The LCD runs in 4-bit mode, so only four data lines are used. If you are new to the display part, LCD interfacing with 8051 explains how it works in more detail.

Proteus circuit of AT89S52 microcontroller connected to a 4x3 keypad and character LCD

Key layout

The program assigns each key a number from 0 to 11. Keys 10 and 11 are the star and hash keys.

  • Column 1: 1, 4, 7, * (rows 1 to 4)
  • Column 2: 2, 5, 8, 0
  • Column 3: 3, 6, 9, #

Simulation

In Proteus, place the AT89S52, an LCD (such as the LM016L) and a phone-style keypad (KEYPAD-PHONE), then wire them as above. Compile the code in mikroC to get a HEX file. Double-click the microcontroller in Proteus, load the HEX file as the program file, and set the clock frequency to 11.0592 MHz. Run the simulation and click the keypad buttons. The digit should appear on the second line of the LCD.

DOWNLOAD SIMULATION FILES

Program

Before compiling, make sure the LCD library is ticked in the mikroC Library Manager. The code below is the original program with one fix. The old version added 0x30 to every key value, which turns 0 to 9 into the right characters but shows : and ; for the star and hash keys. A small lookup table now returns the correct character for all 12 keys.

#define R1 P1.F0  //pin 1
#define R2 P1.F1  //pin 2
#define R3 P1.F2  //pin 3
#define R4 P1.F3  //pin 4
#define C1 P3.F3  //pin 5
#define C2 P3.F4  //pin 6
#define C3 P3.F5  //pin 7
sbit LCD_RS at P2_3_bit;
sbit LCD_EN at P2_2_bit;
sbit LCD_D4 at P2_1_bit;
sbit LCD_D5 at P2_0_bit;
sbit LCD_D6 at P2_4_bit;
sbit LCD_D7 at P2_5_bit;
// End Lcd module connections
int key=12;
const char keymap[] = "0123456789*#";
void keydelay(void);
void main()
{
    C1=1; C2=1;C3=1;   //pull up
    Lcd_Init();                        // Initialize Lcd
    Lcd_Cmd(_LCD_CLEAR);               // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
    Lcd_Out(1,6,"Keypad");             // Write text in first row
    delay_ms(500);
    Lcd_Cmd(_LCD_CLEAR);               // Clear display
    while(1)
    {
        C1=0;C2=1;C3=1;
        if(R1==0) {key=1;}
        if(R2==0) {key=4;}
        if(R3==0) {key=7;}
        if(R4==0) {key=10;}
        C1=1;C2=0;C3=1;
        if(R1==0) {key=2;}
        if(R2==0) {key=5;}
        if(R3==0) {key=8;}
        if(R4==0) {key=0;}
        C1=1;C2=1;C3=0;
        if(R1==0) {key=3;}
        if(R2==0) {key=6;}
        if(R3==0) {key=9;}
        if(R4==0) {key=11;}
        if(key<12) {Lcd_Chr(2, 3, keymap[key]); key=12; keydelay();  }
    }
}
void keydelay(void)
{
    delay_ms(500);
}

How the code works

  • Pin definitions: the #define lines give names to the row and column pins, so the scanning code is easy to read.
  • key = 12: 12 means no key is pressed. Valid keys are 0 to 11.
  • Scanning: inside the loop, one column is set low at a time. Each row that reads 0 gives the key number for that position.
  • Display: if a key was found, its character is written to row 2, column 3 of the LCD. The key value is then reset to 12.
  • keydelay(): a 500 ms pause stops one long press from being read many times. It also acts as a crude debounce.
See also
EEE - POWER ELECTRONIC MINI PROJECTS

Each new key overwrites the previous one at the same position. To build a multi-digit entry such as a PIN, keep a counter and increase the column each time a key is accepted.

Troubleshooting

  • Nothing on the LCD: check the six LCD pin definitions against your wiring, and confirm the HEX file is loaded in Proteus.
  • Wrong digit appears: the row or column wires are probably swapped. Compare them with the key layout above.
  • Keys repeat or lag: adjust the delay in keydelay(). A shorter delay reads faster but may register one press twice.
  • Timing looks off: make sure the clock in mikroC and in Proteus is the same value.

If you use other platforms, the same scanning idea applies. Compare it with keypad interfacing with PIC16F877A or a matrix keypad with Arduino.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

FAQ

Why scan columns and read rows?

Only one column is driven low at a time, so a low reading on a row tells you exactly which key is down. The roles can be swapped, but the scan logic stays the same.

Can I use different pins on the AT89S52?

Yes. Any free port pins work. Change the #define lines and the LCD sbit lines, and rewire the circuit to match.

How do I use a 4×4 keypad instead?

Add a fourth column pin, define it, and add one more scan block with four more row checks. The rest of the program stays the same.

Do I need external pull-up resistors?

Port 1 on the 8051 has internal pull-ups, so the row inputs read high by default. For long cables or noisy environments, external pull-ups can make the readings more stable.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works