Class 5 - 4x4 Matrix Keypad & LCD

May 08, 2025

Hi buddies!

In our previous session, we saw how to create a counter using an LCD and a single key. In this class, we’ll learn about the working principle of a 4×4 Matrix Keypad.

🎛 What is a 4×4 Matrix Keypad?

A 4×4 matrix keypad contains 16 keys arranged in 4 rows and 4 columns. These keypads are commonly used in password entry systems, calculators, and DIY electronics.

🔍 How It Works

Each key press connects a specific row and column, forming a temporary short circuit. To detect which key is pressed, we perform row-wise scanning.

  1. Set columns as outputs and rows as inputs.
  2. Drive one column LOW and others HIGH.
  3. Read all row lines – if any row reads LOW, a key is pressed in that column.

To detect a press:

  • Send 0xF0 to keypad port
  • If the read value is still 0xF0, then no key is pressed.
  • If a row reads LOW, proceed to scan the key position.

To find the exact key:

  1. Send these values to the port one by one:
    • B11111110
    • B11111101
    • B11111011
    • B11110111
  2. After each, read the port. If any of these values result in:
    • 00010000
    • 00100000
    • 01000000
    • 10000000

    …perform bitwise & to determine the exact row & column.

Now using the final row and column values, we can look up the correct character from a lookup table.

⚙️ Detecting 16 Keys

By cycling through 4 column states and checking each row, we can identify which of the 16 keys is pressed.

📚 No Need to Do This Manually!

You can simply use the KEYPAD4X4 library included in our BuddyKit tools. Here’s how:

  1. In your build.bat, include:
  2. The keypad is assigned to Port 0 by default in the header file. Change it if needed.
  3. The key layout is already defined for you, for example:

All 16 keys are mapped to characters: numbers and symbols like +, =, X etc.

💻 Sample Code Walkthrough

  1. Include the keypad.h header
  2. Declare a variable to store the pressed key
  3. In the loop, call KEYPAD_GetKey() repeatedly
  4. If the return is non-zero, print the key on LCD at 2nd row, 0th column

Simple and efficient keypad integration!

Leave a reply
Class 4 – Counter using LCDClass 6 – Simple Digital Door Lock

Leave Your Reply