Table of Contents
KEY TAKEAWAYS
- A matrix keypad uses rows and columns to scan multiple buttons with fewer GPIO pins
- A 4×4 keypad requires only 8 pins instead of 16 by scanning rows and reading columns
- Row-column scanning works by driving one row low at a time and reading which column goes low
- Debouncing is essential to prevent false multiple detections from a single key press
Many embedded applications require a large number of keys connected to a computing system. For example – a PC keyboard, Cell Phone keypad and Calculators.
If we connect a single key, we just connect it directly to a GPIO. But what when want to connect, say 10 or 100 keys directly MCUs. Most MCUs won’t have so many IO pins. The ATmega8 23 GPIOs. Moreover, connecting so many keys on a one-one basis will have the following problems:
- It will eat up precious i/o line.
- MCU to Keypad interface will contain lots of wires.
The 4×4 Matrix KeypadTo save us from all these troubles, some genius people came up with a clever technique – called the Multiplexed Matrix Keypad. In this arrangement of keypad, the keys are connected in the matrix style, i.e., rows and columns like shown below:

The Algorithm
The algorithm is actually very simple. The first thing we need to do is
- Connect all the Row pins (R1-R4) to the MCU as input with internal pull-ups enabled
- Connect all the Column pins (C1-C4) to the MCU as output
Now we put the following logic in a loop:
- Set C1 to LOW and all other column lines to HIGH Z state
- Read all the Row pins one by one
- Move to the next column
- Repeat

As you can see in the image above C1 is made LOW while all other Columns are in HIGH Z State. We can read the Value of R1 to R4 to get their pressed status. If they are high the button is NOT pressed. As we have enabled internal pullups on the Row pins, these pullups keep their value high when they are floating (that means NOT connected to anything). But when a key is pressed it is connected to LOW line from the column thus making it LOW.
All we need to do now is monitor which of the four Row pins is low. Since C1 is low, if we get R1 as low, it means button 1 is pressed. Similarly R2 will give us button 5, R3 -> 9 and R4 -> 13
After that we make the C1 High Z again and make C2 LOW. And read R1 to R4 again. This gives us status of the second column of keys. Similarly we scan all columns.
Doing it all in AVR
Each i/o port in AVR has three related registers PORTx, DDRx and PINx. For example port A has
- PORTA Port Driver – when any bit is set to 1 it appears as HIGH i.e. 5v . But this is the case only if that bit is OUTPUT. If it is input, setting any bit to 1 enables the internal pullup on that bit.
- DDRA DATA DIRECTION REGISTER – Make any pin on than port as IN or OUT. When bit is 1 it represents Output. When bit is 0 it represents Input. Input state is also called tristate or high Z state.
- PINA – Read it to get the level (HIGH or LOW) at the actual i/o pin. It is read when the pin is made input.
So now you know
- How to make any i/o line Input(high Z) or Output.
- How to enable internal pullup register on input lines.
- How to read value that is present on input lines.
In the follow up post, we will see why it is important to make the columns LOW and in High Z state or Tristate. I will also share the code with you !!You can read the next part here.
Practical Use Cases for Matrix Keypads
Matrix keypads are used in embedded systems wherever a user needs to enter numeric data or navigate menus without a full keyboard. Here are real applications:
- Door lock / access control: User enters a 4–6 digit PIN. The MCU compares it against a stored hash and drives a solenoid lock. Add a timeout (e.g., 5 seconds of inactivity resets the input) and a lockout after 3 failed attempts.
- Industrial equipment HMI: Set temperature setpoints, timer durations, or batch counts on machines that have no touchscreen. A 4×4 keypad with 0–9, *, #, and four function keys is sufficient for most parameter entry.
- Vending machines: Product selection (A1, B2, etc.) uses a matrix keypad. The row identifies the letter, the column identifies the number.
- Calculator / scientific instrument: The classic use case. Each key maps to a function or digit. The scanning algorithm is identical regardless of what the key “means” to the application.
Common Pitfalls When Using Matrix Keypads
- No debouncing: Mechanical keys bounce for 5–20 ms. Without debouncing (delay or state machine), one press registers as 3–5 presses. Always add a debounce delay of at least 10 ms after detecting a key press.
- Ghosting: If the user presses two keys in the same row simultaneously, the scanning algorithm can report a third (phantom) key. Use diodes on each key to prevent ghosting if multi-key press is possible in your application.
- Forgetting pull-ups: The input pins (rows or columns, depending on your scan direction) need pull-up resistors. Without them, the inputs float and the scan reads random values.

Vivek Bhageria — Lead Firmware R&D Engineer, 12+ years. Ex-Bosch (automotive powertrain), MusicTribe (real-time audio), medical devices. M.Tech BITS Pilani. I write at NerdyElectronics — practical, register-level embedded systems for engineers who want to understand what’s actually happening under the hood.





