Arduino Uno: code lock with keypad
Keypad + LCD + piezo. Code: 1234.
- Arduino Uno
- HD44780 LCD
- Keypad
Open in the simulatorNo account needed: press Simulate and it runs.
How it works
Click the keys in the schematic (4×4 membrane keypad, Keypad library). The parallel 16×2 LCD (LiquidCrystal, 4-bit bus) shows asterisks, # checks the code, * clears. Correct code: 1234. Every key press beeps through the piezo on A2 (tone(), audible with 🔈). Potentiometer RV1 sets the LCD contrast at V0: if V0 is above ~2 V, the display stays blank.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | A1 GNDBZ1 −DS1 VSSDS1 RWDS1 KRV1 2V1 − |
| 5V | A1 5VDS1 VDDDS1 ARV1 1V1 + |
| V0 | DS1 V0RV1 W |
| BZ | A1 A2BZ1 + |
| C1 | A1 ~D5SW1 C1 |
| C2 | A1 D4SW1 C2 |
| C3 | A1 ~D3SW1 C3 |
| C4 | A1 D2SW1 C4 |
| D4 | A1 ~D10/SSDS1 D4 |
| D5 | A1 ~D11/MOSIDS1 D5 |
| D6 | A1 D12/MISODS1 D6 |
| D7 | A1 D13/SCKDS1 D7 |
| E | A1 A1DS1 E |
| R1 | A1 ~D9SW1 R1 |
| R2 | A1 D8SW1 R2 |
| R3 | A1 D7SW1 R3 |
| R4 | A1 ~D6SW1 R4 |
| RS | A1 A0DS1 RS |
Parts
Firmware
sketch.ino · 38 lines
// Code lock: 4×4 keypad (rows D9..D6, columns D5..D2), parallel 16×2 LCD (RS A0, E A1,
// D4..D7 = D10..D13), piezo on A2. Enter the code, # confirms, * clears. Correct code: 1234
#include <Keypad.h>
#include <LiquidCrystal.h>
const char CODE[] = "1234";
char keys[4][4] = { {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} };
byte rowPins[4] = {9, 8, 7, 6}, colPins[4] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, 4, 4);
LiquidCrystal lcd(A0, A1, 10, 11, 12, 13);
const int BUZZER = A2;
char input[9];
byte len = 0;
void prompt() { len = 0; input[0] = 0; lcd.clear(); lcd.print(F("Enter code:")); lcd.setCursor(0, 1); }
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
prompt();
}
void loop() {
char k = keypad.getKey();
if (!k) return;
tone(BUZZER, 2000, 40);
Serial.print(F("Key ")); Serial.println(k);
if (k == '*') { prompt(); return; }
if (k == '#') {
lcd.clear();
if (strcmp(input, CODE) == 0) { lcd.print(F("Open!")); tone(BUZZER, 1500, 400); }
else { lcd.print(F("Wrong code")); tone(BUZZER, 300, 700); }
delay(1500);
prompt();
return;
}
if (len < 8) { input[len++] = k; input[len] = 0; lcd.print('*'); }
}Open in the simulator