Fork me on GitHub

Project Notes

#304 KeyboardMatrixModule/KeyMatrixInput

Demo multiplexed key input on a simple pushbutton/LED matrix module and how to use INPUT_PULLUP.



See LEAP#303 KeyboardMatrixModule for details of the module I am testing here.

It features a 4x4 matrix of Push Buttons.

4x4 Matrix Push Button Array

The module includes an array of 16 (black) pushbuttons K1-K16.

These are connected in a 4x4 matrix, with 8 pins: L1-4, R1-4. Looking at the module with the LEDs at the bottom:

  • the “L” pins connect to all the pushbuttons in a row
  • the “R” pins connect to all the pushbuttons in a column

So for example pressing the button on the second row, third from the left, will open a circuit between L2 and R3.

Test Program

The KeyMatrixInput.ino sketch demonstrates reading the key matrix directly from an Arduino.

It scans the matrix and prints any pressed keys to the serial out. Scanning the matrix involves by sequentially setting one of the L1-4 pins, and testing for a corresponding signal on R1-4.

NB: The role of L and R pins can be switched - but doing it this way means the corresponding pin label (K1-16) can be easily determined.

There’s a basic decision to be made here: detect input swinging HIGH or LOW?

Since the key matrix does not include any pull-up/pull-down resistors, detecting HIGH is asking for trouble, as when no keys are pressed, inputs L1-4 are effectively floating. To detect a normally LOW state swinging high requires additional pull-down resistors (say 10kΩ).

That’s possible, but things are easier if we use the INPUT_PULLUP feature of the ATmega:

  • R1-4 are normally pulled high with INPUT_PULLUP
  • L1-4 are HIGH by default - pressing a button would have no effect on the HIGH state of the R1-4 pin
  • when an L pin is set LOW (say L2), then if any R1-4 also read LOW, then we know the corresponding button is pressed


Multiple key registrations (or missed button presses) are possible depending on the scanning frequency. Since this is a polling approach, normal mechanical bouncing is not such a problem. I’ve avoided the issue by setting the polling frequency at a relatively slow 1/150ms

Test Run

Here’s an example of the console output, with me pressing all the buttons from K1 to K16 and then down the even numbers:






Credits and References

Project Source on GitHub Project Gallery Return to the LEAP Catalog

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

LEAP is just my personal collection of projects. Two main themes have emerged in recent years, sometimes combined:

  • electronics - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting
  • scale modelling - I caught the bug after deciding to build a Harrier during covid to demonstrate an electronic jet engine simulation. Let the fun begin..
To be honest, I haven't quite figured out if these two interests belong in the same GitHub repo or not. But for now - they are all here!

Projects are often inspired by things found wild on the net, or ideas from the many great electronics and scale modelling podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know (or send a PR!). See the individual projects for credits where due.