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
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | 4x4 membrane keypad | Rows R1–R4 and columns C1–C4: a pressed key joins its row to its column. Use the Keypad library. |
| 2 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | green LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
| 1 | red LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno pin 9 | 4x4 membrane keypad R1 |
| 2 | Arduino Uno pin 8 | 4x4 membrane keypad R2 |
| 3 | Arduino Uno pin 7 | 4x4 membrane keypad R3 |
| 4 | Arduino Uno pin 6 | 4x4 membrane keypad R4 |
| 5 | Arduino Uno pin 5 | 4x4 membrane keypad C1 |
| 6 | Arduino Uno pin 4 | 4x4 membrane keypad C2 |
| 7 | Arduino Uno pin 3 | 4x4 membrane keypad C3 |
| 8 | Arduino Uno pin 2 | 4x4 membrane keypad C4 |
| 9 | Arduino Uno pin 12 | 220 Ω resistor (rG) leg 1 |
| 10 | 220 Ω resistor (rG) leg 2 | green LED anode (A, long leg) |
| 11 | Arduino Uno GND | green LED cathode (C, short leg) |
| 12 | Arduino Uno pin 13 | 220 Ω resistor (rR) leg 1 |
| 13 | 220 Ω resistor (rR) leg 2 | red LED anode (A, long leg) |
| 14 | Arduino Uno GND | red LED cathode (C, short leg) |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a 4x4 membrane keypad, two 220 Ω resistors, a green LED and a red LED from the parts palette.
- Wire 1. Connect Arduino Uno pin 9 to 4x4 membrane keypad R1.
- Wire 2. Connect Arduino Uno pin 8 to 4x4 membrane keypad R2.
- Wire 3. Connect Arduino Uno pin 7 to 4x4 membrane keypad R3.
- Wire 4. Connect Arduino Uno pin 6 to 4x4 membrane keypad R4.
- Wire 5. Connect Arduino Uno pin 5 to 4x4 membrane keypad C1.
- Wire 6. Connect Arduino Uno pin 4 to 4x4 membrane keypad C2.
- Wire 7. Connect Arduino Uno pin 3 to 4x4 membrane keypad C3.
- Wire 8. Connect Arduino Uno pin 2 to 4x4 membrane keypad C4.
- Wire 9. Connect Arduino Uno pin 12 to 220 Ω resistor (rG) leg 1.
- Wire 10. Connect 220 Ω resistor (rG) leg 2 to green LED anode (A, long leg).
- Wire 11. Connect Arduino Uno GND to green LED cathode (C, short leg).
- Wire 12. Connect Arduino Uno pin 13 to 220 Ω resistor (rR) leg 1.
- Wire 13. Connect 220 Ω resistor (rR) leg 2 to red LED anode (A, long leg).
- Wire 14. Connect Arduino Uno GND to red LED cathode (C, short leg).
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- 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
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator