Read a 4×4 keypad

Connect the 4×4 membrane keypad to the UNO R3, print every key press and build a simple code lock.
  • Level:Beginner
  • Duration: 40 min
  • Ages 10+
  • Platform: Arduino

What you'll need

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Read a 4×4 keypad – EEMENTOR STEM™ lesson

Keypads are everywhere: door locks, alarm panels, vending machines. The 4×4 membrane keypad has 16 keys but only 8 wires, because the keys are arranged in a grid of 4 rows and 4 columns. In this lesson you read the keys and build a secret-code lock.

What you'll learn

  • How a key matrix works
  • How to use the Keypad library
  • How to compare a typed code with a secret code

What you need

  • UNO R3 board and USB cable
  • 4×4 membrane keypad
  • 8 male-to-male jumper wires

Step 1: Install the library

Open Tools → Manage Libraries, search for Keypad and install the one by Mark Stanley and Alexander Brevig.

Step 2: Wiring

Hold the keypad with the keys facing you. The 8 pins on the ribbon connector are, from left to right, the 4 rows (R1 to R4) and then the 4 columns (C1 to C4). Plug the jumper wires straight into the connector.

Breadboard wiring diagram: Read a 4×4 keypad
Breadboard view. Check each connection against the wiring table below.
Keypad pin UNO pin
R1 9
R2 8
R3 7
R4 6
C1 5
C2 4
C3 3
C4 2
FREE

Build this circuit in EEMENTOR Lab

Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.

The parts for this circuit laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. Wire the parts using the wiring table above. Pins with no wire show red.
  2. Press Run, hold the mouse on a key, and watch the Serial monitor.
  3. Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.

EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab

Step 3: Print the keys

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

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

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

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

void setup() {
  Serial.begin(9600);
  Serial.println("Press a key");
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    Serial.print("Key pressed: ");
    Serial.println(key);
  }
}

Open the Serial Monitor at 9600 baud and press a few keys.

Step 4: A secret-code lock

Type the code and finish with #. Press * to start again.

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

const String SECRET = "1234";
String typed = "";

void setup() {
  Serial.begin(9600);
  Serial.println("Enter the code, then press #");
}

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

  if (key == '#') {
    if (typed == SECRET) {
      Serial.println("Unlocked!");
    } else {
      Serial.println("Wrong code, try again");
    }
    typed = "";
  } else if (key == '*') {
    typed = "";
    Serial.println("Cleared");
  } else {
    typed += key;
    Serial.print("*"); // hide the digits
  }
}

What you should see

Two Serial Monitors at 9600 baud: one prints each key pressed, the other shows the code lock with lines like ***Wrong code, try again, **Cleared and ****Unlocked!

How it works

The keypad as 4 row wires (pins 9 to 6) and 4 column wires (pins 5 to 2); the library sets one column LOW at a time, reads the rows and finds key 5 where column 2 meets row 2.

Each key sits where one row wire crosses one column wire. The library switches the columns on one at a time and checks which row answers. That tells it exactly which key is pressed, using only 8 pins instead of 16.

Try this next

  • Combine this with lesson 3: turn the servo to 90° when the right code is entered, like a real lock.
  • Add the LED from lesson 1 and blink it three times on a wrong code.

Troubleshooting

  • The wrong keys appear: the connector may be reversed. Swap the order of the wires, or change the order in rowPins and colPins.
  • Nothing appears: check that the Serial Monitor speed is 9600 baud and the library is installed.