Arduino 4x4 Keypad Password Lock: Wiring, Code and Simulator

To build this Arduino 4x4 Keypad Password Lock project you need an Arduino Uno and a 4x4 membrane keypad, two 220 Ω resistors, a green LED and a red LED. A 4×4 membrane keypad read with the Keypad library. Type 1234 then # to unlock. Make the 14 connections in the wiring table, upload the Arduino C++ code, and click 1, 2, 3, 4 and # on the keypad: the green LED lights. A wrong code keeps the red one on.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
14x4 membrane keypadRows R1–R4 and columns C1–C4: a pressed key joins its row to its column. Use the Keypad library.
2220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1green LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.
1red LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.

Wiring

#FromTo
1Arduino Uno pin 94x4 membrane keypad R1
2Arduino Uno pin 84x4 membrane keypad R2
3Arduino Uno pin 74x4 membrane keypad R3
4Arduino Uno pin 64x4 membrane keypad R4
5Arduino Uno pin 54x4 membrane keypad C1
6Arduino Uno pin 44x4 membrane keypad C2
7Arduino Uno pin 34x4 membrane keypad C3
8Arduino Uno pin 24x4 membrane keypad C4
9Arduino Uno pin 12220 Ω resistor (rG) leg 1
10220 Ω resistor (rG) leg 2green LED anode (A, long leg)
11Arduino Uno GNDgreen LED cathode (C, short leg)
12Arduino Uno pin 13220 Ω resistor (rR) leg 1
13220 Ω resistor (rR) leg 2red LED anode (A, long leg)
14Arduino Uno GNDred LED cathode (C, short leg)

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a 4x4 membrane keypad, two 220 Ω resistors, a green LED and a red LED from the parts palette.
  3. Wire 1. Connect Arduino Uno pin 9 to 4x4 membrane keypad R1.
  4. Wire 2. Connect Arduino Uno pin 8 to 4x4 membrane keypad R2.
  5. Wire 3. Connect Arduino Uno pin 7 to 4x4 membrane keypad R3.
  6. Wire 4. Connect Arduino Uno pin 6 to 4x4 membrane keypad R4.
  7. Wire 5. Connect Arduino Uno pin 5 to 4x4 membrane keypad C1.
  8. Wire 6. Connect Arduino Uno pin 4 to 4x4 membrane keypad C2.
  9. Wire 7. Connect Arduino Uno pin 3 to 4x4 membrane keypad C3.
  10. Wire 8. Connect Arduino Uno pin 2 to 4x4 membrane keypad C4.
  11. Wire 9. Connect Arduino Uno pin 12 to 220 Ω resistor (rG) leg 1.
  12. Wire 10. Connect 220 Ω resistor (rG) leg 2 to green LED anode (A, long leg).
  13. Wire 11. Connect Arduino Uno GND to green LED cathode (C, short leg).
  14. Wire 12. Connect Arduino Uno pin 13 to 220 Ω resistor (rR) leg 1.
  15. Wire 13. Connect 220 Ω resistor (rR) leg 2 to red LED anode (A, long leg).
  16. Wire 14. Connect Arduino Uno GND to red LED cathode (C, short leg).
  17. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  18. Run it. Press Run. Click 1, 2, 3, 4 and # on the keypad: the green LED lights. A wrong code keeps the red one on.

Arduino C++ code

// Keypad lock: type 1234 then # to unlock. * clears what you typed.
// Rows R1-R4 on pins 9, 8, 7, 6; columns C1-C4 on pins 5, 4, 3, 2.
#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 int GREEN = 12;
const int RED = 13;
String typed = "";

void setup() {
  pinMode(GREEN, OUTPUT);
  pinMode(RED, OUTPUT);
  digitalWrite(RED, HIGH);
  Serial.begin(9600);
  Serial.println("Locked. Enter the code, then #.");
}

void loop() {
  char key = keypad.getKey();
  if (!key) return;
  Serial.print(key);
  if (key == '*') {
    typed = "";
    Serial.println(" cleared");
  } else if (key == '#') {
    bool ok = typed == "1234";
    digitalWrite(GREEN, ok ? HIGH : LOW);
    digitalWrite(RED, ok ? LOW : HIGH);
    Serial.println(ok ? " unlocked" : " wrong code");
    typed = "";
  } else {
    typed += key;
  }
}

What to try

Click 1, 2, 3, 4 and # on the keypad: the green LED lights. A wrong code keeps the red one on.

Frequently asked questions

How do I wire the 4x4 Keypad Password Lock project on an Arduino Uno?

Make 14 connections: Arduino Uno pin 9 to 4x4 membrane keypad R1; Arduino Uno pin 8 to 4x4 membrane keypad R2; Arduino Uno pin 7 to 4x4 membrane keypad R3; Arduino Uno pin 6 to 4x4 membrane keypad R4; Arduino Uno pin 5 to 4x4 membrane keypad C1; Arduino Uno pin 4 to 4x4 membrane keypad C2; Arduino Uno pin 3 to 4x4 membrane keypad C3; Arduino Uno pin 2 to 4x4 membrane keypad C4; Arduino Uno pin 12 to 220 Ω resistor (rG) leg 1; 220 Ω resistor (rG) leg 2 to green LED anode (A, long leg); Arduino Uno GND to green LED cathode (C, short leg); Arduino Uno pin 13 to 220 Ω resistor (rR) leg 1; 220 Ω resistor (rR) leg 2 to red LED anode (A, long leg); Arduino Uno GND to red LED cathode (C, short leg).

What parts do I need for this Arduino project?

An Arduino Uno and a 4x4 membrane keypad, two 220 Ω resistors, a green LED and a red LED.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

A 4×4 membrane keypad read with the Keypad library. Type 1234 then # to unlock.

More Arduino Uno projects