A matrix keypad, such as a keypad for a security code (digicode), consists of a grid of buttons. Each press on a key establishes a unique electrical connection, identifiable by a microcontroller like the Raspberry Pi Pico. The rows and columns of this grid are connected to the inputs of the microcontroller, allowing it to precisely determine which key was pressed. This clever system enables the management of a large number of buttons with a reduced number of input pins.

How Does a Keypad Work?

The keypad is structured as a matrix of keys, with each key positioned at the intersection of a row and a column. When a key is pressed, it electrically connects the corresponding row and column. The Raspberry Pi Pico determines which key was pressed by systematically scanning each row. By setting a row to low (ground), the Raspberry checks if any column also connects to ground. If this is the case, it indicates that a key located at the intersection of that row and column was pressed.

Keypad Library

To work on the proposed projects, you will need the Keypad library. Let’s see how to install it:

Installing the Keypad Library

The Keypad library will allow you to operate any button matrix.

  1. Download the library:

  2. Add the Library to Arduino IDE:

    • Once the download is complete, open the Arduino IDE.

    • Go to Sketch > Include Library > Manage Libraries.

    • Click the Add .ZIP Library option and select the library you previously downloaded.

  3. Confirm Installation:

    • After selecting the library, you will receive a message confirming that the library has been installed successfully.

Displaying Pressed Keys on the Serial Monitor

The program we’ll use allows you to light up LEDs for each button on the keypad:

  • Buttons 1 to 8: These buttons will light up blue LEDs.

  • Button 9: This button will light up all the blue LEDs.

  • Button 0: This button will turn off all blue LEDs.

  • Buttons A to D: These buttons will light up 4 red LEDs.

  • Button *: This button will light up all the red LEDs.

  • Button #: This button will turn off all the red LEDs.

#include <Keypad.h>

const uint8_t LEDS = 12;
const uint8_t ROWS = 4;
const uint8_t COLS = 4;

char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

// Pins connected to LED1, LED2, LED3, ...LED12
uint8_t ledPins[LEDS] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 28, 27 };
uint8_t rowPins[ROWS] = { 26, 22, 21, 20 }; // Pins connected to R1, R2, R3, R4
uint8_t colPins[COLS] = { 19, 18, 17, 16 }; // Pins connected to C1, C2, C3, C4

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  for (uint8_t l = 0; l < LEDS; l++) {
    pinMode(ledPins[l], OUTPUT);
    digitalWrite(ledPins[l], LOW);
  }
}

void loop() {
  char key = keypad.getKey();

  if (key != NO_KEY) {
    switch (key) {
      case '1': digitalWrite(ledPins[0], HIGH);
        break;
      case '2': digitalWrite(ledPins[1], HIGH);
        break;
      case '3': digitalWrite(ledPins[2], HIGH);
        break;
      case '4': digitalWrite(ledPins[3], HIGH);
        break;
      case '5': digitalWrite(ledPins[4], HIGH);
        break;
      case '6': digitalWrite(ledPins[5], HIGH);
        break;
      case '7': digitalWrite(ledPins[6], HIGH);
        break;
      case '8': digitalWrite(ledPins[7], HIGH);
        break;
      case '9':
        for (uint8_t l = 0; l < 8; l++) {
          digitalWrite(ledPins[l], HIGH);
        }
        break;
      case '0':
        for (uint8_t l = 0; l < 8; l++) {
          digitalWrite(ledPins[l], LOW);
        }
        break;
      case 'A': digitalWrite(ledPins[8], HIGH);
        break;
      case 'B': digitalWrite(ledPins[9], HIGH);
        break;
      case 'C': digitalWrite(ledPins[10], HIGH);
        break;
      case 'D': digitalWrite(ledPins[11], HIGH);
        break;
      case '*':
        for (uint8_t l = 8; l < 12; l++) {
          digitalWrite(ledPins[l], HIGH);
        }
        break;
      case '#':
        for (uint8_t l = 8; l < 12; l++) {
          digitalWrite(ledPins[l], LOW);
        }
        break;
    }
  }

  delay(10);
}