RSS Daily tech news
  • A lab mistake at Cambridge reveals a powerful new way to modify drug molecules
    Cambridge scientists have discovered a light-powered chemical reaction that lets researchers modify complex drug molecules at the final stages of development. Unlike traditional methods that rely on toxic chemicals and harsh conditions, the new approach uses an LED lamp to create essential carbon–carbon bonds under mild conditions. This could make drug discovery faster and more […]
  • Scientists just found a way to 3D print one of the hardest metals on Earth
    Scientists have found a promising new way to manufacture one of industry’s toughest materials—tungsten carbide–cobalt—using advanced 3D printing. Normally, producing this ultra-hard material requires high-pressure processes that waste large amounts of expensive tungsten and cobalt. The new approach uses a hot-wire laser technique that softens the metals rather than fully melting them, allowing manufacturers to […]
  • Scientists turn scrap car aluminum into high-performance metal for new vehicles
    Scientists at Oak Ridge National Laboratory have created a new aluminum alloy called RidgeAlloy that can turn contaminated car-body scrap into strong structural vehicle parts. Normally, impurities introduced during recycling make this scrap unsuitable for high-performance applications. RidgeAlloy overcomes that challenge, enabling recycled aluminum to meet the strength and durability standards required for modern vehicles. […]
  • Electrons catapult across solar materials in just 18 femtoseconds
    Electrons in solar materials can be launched across molecules almost as fast as nature allows, thanks to tiny atomic vibrations acting like a “molecular catapult.” In experiments lasting just 18 femtoseconds, researchers at the University of Cambridge observed electrons blasting across a boundary in a single burst, far faster than long-standing theories predicted. Instead of […]
  • Record-breaking photodetector captures light in just 125 picoseconds
    A new ultrathin photodetector from Duke University can sense light across the entire electromagnetic spectrum and generate a signal in just 125 picoseconds, making it the fastest pyroelectric detector ever built. The breakthrough could power next-generation multispectral cameras used in medicine, agriculture, and space-based sensing.
  • For the first time, light mimics a Nobel Prize quantum effect
    Scientists have pulled off a feat long considered out of reach: getting light to mimic the famous quantum Hall effect. In their experiment, photons drift sideways in perfectly defined, quantized steps—just like electrons do in powerful magnetic fields. Because these steps depend only on nature’s fundamental constants, they could become a new gold standard for […]

Interfacing 4×3 keypads with PIC16F877A

by Florius
Educational graphic showing how to interface 4x3 matrix keypads with a PIC16F877A microcontroller. The image includes two physical keypads, a schematic layout of the 4x3 keypad connections, the PIC16F877A chip, and the MPLAB X IDE logo. Text reads "Interfacing PIC16F877A with 4x3 keypads."

In this tutorial, we will provide an overview of the 4×3 membrane keypad. The keypad serves as a reliable and budget-friendly tool for having inputs in your project. Understanding how to interface with the keypad will prove useful in future projects that require menu selection or similar inputs. Our guide intends to provide a comprehensive understanding of the the basics of the 4×3 membrane keypad. Through this, you will gain valuable insight that will help you in your projects.

Membrane keypads are available in various sizes, including the widely used 4×3 keypad with 12 keys (0-9 with 2 symbols), and the 4×4 keypad with 16 keys (0-9, A-D and 2 symbols). These keypads are designed in a standard telephone keypad layout which makes them user-friendly for everyone.

Image showing two matrix keypads. The left keypad is a 4x3 layout with numbers 0–9, asterisk (*), and hash (#). The right keypad is a 4x4 layout with the same keys plus A, B, C, and D in the last column. Both keypads are commonly used in microcontroller projects.
Fig 1. Side-by-side comparison of a 4x3 and 4x4 matrix keypad used in embedded systems for user input.

1. The layers of a membrane keypad

The membrane keypad is made with flexible materials making it ideal for use on a variety of surfaces, including curved ones. As illustrated in Figure 2, the keypad is comprised of multiple layers. The topmost layer, the keypad overlay, functions as a visual interface similar to that of conventional telephones. Directly below it lies a layer of metal domes that provide tactile feedback upon button activation. The following three layers serve to create a complete circuit. At the top circuit, a voltage is applied, and a spacer separates it from the bottom circuit when the button is not pushed. Upon activation, the top circuit connects with the bottom circuit, completing the entire circuit. Finally, the bottom layer acts as a seal, enclosing the entire package.

Exploded view of a membrane keypad showing its construction layers: keypad overlay, metal domes, top circuit, spacer, bottom circuit, and bottom layer. Each layer plays a role in registering key presses in devices like matrix keypads.
Fig 2. Internal structure of a membrane keypad: layered design for durability and responsiveness.

Because the complete circuit is made by connecting a row with a column, for the 4×3 keypad, only 7 I/O connections are required for a total of 12 different inputs. The 4×4 keypad, as depicted in Figure 2, requires only 8 connections for 16 different inputs.

2. Membrane keypad scanning

As shown in the previous section, the keypad consists of 12 (or 16) buttons, that can be pushed down; which basically acts as a switch between the row and columns. To illustrate, take a look at Figure 3. The leads to button “6” are connected to Row 2 and Col 3. If button “6” is pushed down, and Row 2 is pulled high, this can be seen on Col 3 as there is a direct connection. The other two columns will stay grounded and no input is seen on those.

Diagram of a 4x3 matrix keypad showing the internal wiring. The keys are arranged in 4 rows and 3 columns, with each button represented as a normally open switch. Pressing a key connects a specific row to a column, allowing microcontrollers to detect which key was pressed.
Fig 3. Schematic layout of a 4x3 matrix keypad showing the row and column wiring for button detection.

Here is how a keypad works with a microcontroller:

  • Each row of the keypad is connected to an output pin of the microcontroller.
  • All columns of the keypad are connected to the input pins of the microcontroller.
  • The columns are normally pulled low by pull-down resistors (either external or internal).
  • The output rows are all set to low.
  • If the input pins measure low, the buttons are not activated.
  • The microcontroller starts sequencing through the rows, setting them high one by one.
  • If any of the buttons is pressed, the corresponding row will be pulled high. This will also cause the corresponding column input to go high.
  • The microcontroller can then read the column input to determine which button is pressed.
Animation of the 12 buttons on the keypad, with their connections.

3. Connecting 4x3 and 4x4 keypad to a microcontroller

The following figures demonstrate a straightforward connection process. However, it is essential to emphasize that the exact pin diagram of the keypad varies by maker, always do check with the datasheet of your own keypad. But in nearly all cases, the first 4 buttons on the left correspond to the Rows (R1, R2, R3, R4), and the 3 buttons on the right correspond to the columns (C1, C2, C3).

Assuming a PIC16F877A microcontroller is being used and a 4×3 matrix keypad, you may omit Column 4 (C4). Pins RB0 to RB3 must be set as output as they connect to the four rows, whereas pins RB4 to RB7 can be set as inputs. In addition, all columns have a pull-down resistor to obtain a proper ground on the inputs. This can also be accomplished through internal pull-down resistors inside the microcontroller. If this programmed it like that, you can entirely eliminate external pull-down resistors.

Keypad circuit
Image of a 4x3 matrix keypad with labeled connector pins. The pins are labeled from left to right as R1, R2, R3, R4 (rows), and C1, C2, C3 (columns), which correspond to the keypad's internal switch matrix for key detection.

4. Microcontroller example code

The following is an example code where I made a scanKeypad function. The function utilizes a loop to make only one row high per iteration, and subsequently checks the columns for any button press. Please note that determining the use of the function’s return value rests with the user, and you can do anything with it. If you want to display this number on an LCD screen, please check my other page on interfacing with LCD.

				
					#include <xc.h>
#define _XTAL_FREQ 20000000 // Define crystal frequency to 20 MHz

// Configuration bits
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = OFF
#pragma config LVP = OFF


char scanKeypad() {
    char row, col;
    const char keypad[4][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9},
        {'*', 0, '#'}
    };
    
    // Loop through each row
    for (row = 0; row < 4; row++) {
        PORTB = (PORTB & 0xF0) | (1 << row); // Set only the specified row bit
        __delay_ms(10); // Delay for debounce
        
        // Read the column inputs, set them to the lower nibble first and mask them with 0b00000111
        col = (PORTB >> 4) & 0x07;
        
        if (col == 0x01 || col == 0x02 || col == 0x04) {
            return keypad[row][col]; // Return keypad value
        }
    }
    return -1; // Invalid col value (multiple same time) or no button pressed
}

void main() {
    // Initialize ports
    TRISB = 0xF0; // RB0 - RB3 as output (rows), RB4 - RB6 as input (columns)
    PORTB = 0x0F; // Set initial output values for rows to low, and enable weak pull-ups for column inputs
    char buttonPressed;
    
    while (1) {
        buttonPressed = scanKeypad(); // Performs a scan of the keypad
        
        if (buttonPressed != -1) {
            // A button is pressed! Look at my LCD page to find more on how to display it:
            // https://florisera.com/embedded-systems/connecting-pic16f877a-with-an-lcd-screen/
        }
    }
}
				
			

5. Summary

In this tutorial we have seen the workings of a keypad membrane by providing a detailed examination inside by corresponding illustrations. It also offers guidance on interfacing the keypad with the PIC16F877A microcontroller using both a pin diagram and program code.

Florius

Hi, welcome to my website. I am writing about my previous studies, work & research related topics and other interests. I hope you enjoy reading it and that you learned something new.

More Posts

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.