RSS feed: Daily tech news Daily tech news
  • A new recipe unlocks “impossible” nanocrystals for LEDs, implants, and superconductors
    Scientists have cracked a long-standing chemistry problem, creating nanocrystals from tough metal nitrides that were previously extremely difficult to produce at this scale. The breakthrough could turn familiar materials used in LEDs, implants, and superconductors into building blocks for flexible electronics, printable devices, and other technologies.
  • Scientists turn one of the hardest plastics to recycle into high-performance engine lubricant
    Researchers have discovered a way to turn notoriously difficult-to-recycle PVC plastic into a key ingredient used in high-performance lubricants such as engine oil. The technique could give mountains of plastic waste a valuable second life while making lubricant production more sustainable.
  • Scientists catch a hidden electronic state forming in just 30 femtoseconds
    Scientists watched a light-triggered hidden state form inside a material in only 30 femtoseconds, revealing a step that had never been seen before. The material first entered a fleeting electronic state in which its bonds reorganized in a repeating pattern, followed by tiny atomic shifts. This ultrafast pathway could offer a new way to control […]
  • MIT physicists discover electrons rebuilding like ice inside a quantum material
    MIT physicists found that two electronic phases inside the same quantum material emerge through surprisingly different mechanisms—one smoothly and the other in expanding pockets resembling growing ice crystals. The discovery could help explain how exotic properties such as superconductivity and magnetism develop and coexist.
  • Tiny quantum engines reveal useful energy hiding in “waste heat”
    A tiny machine made from just an atom and particles of light may sound impossibly simple, but it raises a surprisingly difficult question: what counts as heat, and what energy can still do useful work? University of Basel researchers have developed a theoretical framework that brings quantum physics and thermodynamics into better agreement for these […]
  • Tiny graphene wrinkles create surprisingly powerful electrical effects
    Scientists have discovered that tiny, sharply curved wrinkles in graphene can dramatically alter its electrical behavior, creating surprisingly strong charge separation. The finding suggests future electronics could be tuned by reshaping materials at the atomic scale instead of changing what they’re made of.

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.