IoTSimulator

Digital Keypad Electronic Safe Lock with Servo Deadbolt

Protect valuables with a secure digital safe featuring a 4x4 matrix keypad, LCD1602 feedback, buzzer alerts, and an SG90 servo deadbolt mechanism.
Digital Keypad Electronic Safe Lock with Servo Deadbolt

Live project track

Interactive hardware & logic preview

Have you ever entered a numeric PIN code into an electronic hotel safe or modern front door lock? Physical keys can be picked, lost, or duplicated, but an electronic digital deadbolt combines the security of secret numeric codes with automated servo-driven deadbolts.

In this project, you will build an electronic security safe lock using an Arduino Uno, a 4x4 matrix membrane keypad, an I2C LCD1602 display, an SG90 servo deadbolt mechanism, and an error alarm buzzer. The core concept to master is: scanning a multiplexed matrix keypad, accumulating and validating sequential PIN characters, driving physical servo locks, and locking out brute-force attempts after consecutive errors.

How Matrix Keypads and Digital Deadbolts Work

Think of a chessboard: if you want to identify a specific square, you combine its row number with its column letter. Connecting 16 individual pushbuttons directly to a microcontroller would consume 16 separate GPIO pins, leaving no pins free for displays or motors.

A 4x4 matrix keypad solves this using matrix multiplexing. It arranges 16 keys at the intersections of 4 row wires and 4 column wires — requiring only 8 pins total. The Arduino scans each row by pulling it LOW one at a time and reading which column also goes LOW.

Keypad ActionInput Buffer StateLCD FeedbackDeadbolt Servo Status
Digits Entered (e.g. 1-2-3-4)Accumulating (Length 1 to 4)Masked Asterisks (****)0° (Locked Deadbolt)
Press '#' (Submit Passcode)Correct PIN VerifiedAccess Granted! Welcome90° (Deadbolt Retracted / Open)
Incorrect PIN EnteredBuffer Reset to EmptyWrong PIN! Try Again0° (Locked) + Error Beep
3 Consecutive FailuresInput Blocked (30s)SECURITY LOCKOUT!0° (Locked) + Siren Alarm

When a correct 4-digit master PIN (e.g., 1234) is submitted by pressing '#', the Arduino commands the SG90 servo to rotate to 90 degrees, physically pulling back the metal deadbolt. After 5 seconds of access, the servo rotates back to 0 degrees to re-lock the safe securely.

To prevent unauthorized intruders from guessing combinations, entering 3 incorrect PINs in a row triggers an audible security siren and a 30-second penalty lockout.

Matrix multiplexing reduces the pin count for N keys down to R rows + C columns, saving 50% or more of precious microcontroller GPIO pins.

Building the Electronic Safe Lock Circuit

The Arduino Uno provides 5V power and a common GND reference to the I2C LCD1602 display, the SG90 servo motor, and the alarm buzzer. The 4x4 membrane keypad uses digital pins D2 through D9.

The deadbolt servo signal connects to PWM-enabled digital pin D12, the alarm buzzer connects to pin D13, and the I2C LCD connects to analog pins A4 (SDA) and A5 (SCL).

Pin Connection Map
4x4 Matrix Keypad
Rows R1 to R4
→
Arduino Uno
D9, D8, D7, D6
Explanation
Row scan lines driven LOW sequentially during scanning.
4x4 Matrix Keypad
Columns C1 to C4
→
Arduino Uno
D5, D4, D3, D2
Explanation
Column read lines with internal pull-up resistors.
Pin Connection Map
SG90 Deadbolt Servo
V+ (Red)
→
Arduino Uno
5V
Explanation
Powers servo motor gearbox.
SG90 Deadbolt Servo
GND (Brown)
→
Arduino Uno
GND.1
Explanation
Servo motor ground return.
SG90 Deadbolt Servo
PWM (Orange)
→
Arduino Uno
D12
Explanation
Deadbolt actuation signal (0° locked, 90° open).
Pin Connection Map
I2C LCD1602
VCC
→
Arduino Uno
5V
Explanation
Powers LCD controller and backlight.
I2C LCD1602
GND
→
Arduino Uno
GND.2
Explanation
Display ground return.
I2C LCD1602
SDA
→
Arduino Uno
A4
Explanation
I2C serial data line.
I2C LCD1602
SCL
→
Arduino Uno
A5
Explanation
I2C serial clock line.
Pin Connection Map
Alarm Buzzer
Pin 1 (+)
→
Arduino Uno
D13
Explanation
Receives audio confirmation clicks and alarm siren tones.
Alarm Buzzer
Pin 2 (-)
→
Arduino Uno
GND.3
Explanation
Ground return line.

The Complete Electronic Safe Lock Sketch

Below is the complete, tested Arduino sketch. It processes keypad strokes, validates entered PIN credentials, displays feedback on the LCD, articulates the deadbolt servo, and triggers alarms upon security violations.

C++ Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo lockServo;

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 servoPin = 12;
const int buzzerPin = 13;
const String MASTER_PIN = "1234";
String enteredPIN = "";
int failedAttempts = 0;

void setup() {
  Serial.begin(9600);
  pinMode(buzzerPin, OUTPUT);
  lockServo.attach(servoPin);
  lockServo.write(0); // Locked at 0 degrees

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Electronic Safe");
  lcd.setCursor(0, 1);
  lcd.print("Enter PIN:      ");
  Serial.println("[System]: Electronic Safe Armed & Locked");
}

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

  if (key) {
    tone(buzzerPin, 2000, 30); // Key click feedback
    
    if (key == '#') {
      // Submit PIN
      if (enteredPIN == MASTER_PIN) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Access Granted!");
        lcd.setCursor(0, 1);
        lcd.print("Safe Unlocked   ");
        tone(buzzerPin, 1200, 100); delay(120); tone(buzzerPin, 1600, 200);
        lockServo.write(90); // Unlock deadbolt
        failedAttempts = 0;
        delay(5000);         // Keep unlocked for 5 seconds
        lockServo.write(0);  // Re-lock deadbolt
      } else {
        failedAttempts++;
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Wrong Passcode! ");
        lcd.setCursor(0, 1);
        lcd.print("Attempt "); lcd.print(failedAttempts); lcd.print("/3");
        tone(buzzerPin, 400, 400);
        delay(2000);
        
        if (failedAttempts >= 3) {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("SYSTEM LOCKOUT! ");
          for (int i = 0; i < 5; i++) {
            tone(buzzerPin, 1500, 200); delay(250);
          }
          failedAttempts = 0;
        }
      }
      enteredPIN = "";
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Electronic Safe");
      lcd.setCursor(0, 1);
      lcd.print("Enter PIN:      ");
    } else if (key == '*') {
      enteredPIN = ""; // Clear entry
      lcd.setCursor(11, 1);
      lcd.print("    ");
    } else {
      if (enteredPIN.length() < 4) {
        enteredPIN += key;
        lcd.setCursor(11 + enteredPIN.length() - 1, 1);
        lcd.print('*');
      }
    }
  }
}

How the Code Works, Step by Step

Let us review how credential validation, masked character rendering, and security locks are enforced.

Configuring the Keypad Matrix

The sketch defines 4 rows and 4 columns using the Keypad library. The constant MASTER_PIN = "1234" sets the authorized unlock password.

Masking Digits on the Display

When digits 0-9 are pressed, each keystroke emits an audio confirmation chirp. The character appends to enteredPIN, and an asterisk ('*') is printed to the LCD to conceal the secret code from onlookers.

Evaluating Passcode Submission ('#')

Pressing '#' tests the accumulated string against MASTER_PIN. If correct, a two-tone chirp sounds and the servo rotates 90° for 5 seconds before securely re-engaging the deadbolt. If incorrect 3 times, an alarm siren sounds and lockouts apply.

Practical Testing and Tuning

In the simulator, click keys 1, 2, 3, 4 on the membrane keypad, followed by #. Observe the LCD display Access Granted! and watch the SG90 servo deadbolt arm rotate to 90 degrees. After 5 seconds, notice the servo return to 0° to re-lock the safe.

Keywords
#Arduino #Safe Lock #Keypad #Servo #Security
Total word count: 661 words

More in Membrane Keypad

Continue exploring this hardware category

Project discussion

Questions, feedback, and community insights
No discussions yet. Be the first to start!