Smart Door Lock System with Keypad and Servo Control
The Smart Door Lock System is designed to provide an enhanced level of security with keypad access and servo-controlled locking mechanism. It integrates a 4×4 keypad for password input and a servo motor to control the door lock.
Users can unlock the door by entering the correct password or engage additional functionalities like changing the password.
Component List:
Name | Quantity | Component |
---|---|---|
U1 | 1 | Arduino Uno R3 |
KEYPAD1 | 1 | Keypad 4×4 |
U2 | 1 | LCD 16 x 2 |
PIEZO1 | 1 | Piezo |
Rpot1 | 1 | 250 kΩ Potentiometer |
D1 | 1 | Green LED |
D2 | 1 | Red LED |
SERVO1 | 1 | Positional Micro Servo |
R1 R2 R4 R3 | 4 | 1 kΩ Resistor |
S1 | 1 | Pushbutton |
The circuit involves a 4×4 keypad, a servo motor, LEDs for visual indication, and a buzzer for audible feedback. The keypad is connected to the Arduino via defined rows and columns.
A servo motor is attached to the door lock and controlled through the Arduino’s output pin. LEDs represent the lock status, turning green on successful unlock and red on failed attempts. The buzzer provides an audible alarm for unsuccessful attempts.
Arduino Code:
#include <Keypad.h>
#include <Servo.h>
#include <string.h>
#include <LiquidCrystal.h>
const byte rows = 4;
const byte columns = 4;
int holdDelay = 700;
int n = 3;
int state = 0;
char key = 0;
int pos = 0;
int buzzer = A3;
int redled = A1;
int greenled = A2;
int button = 2;
int btnState;
int statebtn = 0;
String default_passwd = "0000";
String input_passwd = "";
char lock_key = ''; char unlock_key = '#'; char change_pass_key = '';
Servo servo_A0;
char keys[rows][columns] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte rowPins[rows] = {6, 7, 8, 9};
byte columnPins[columns] = {10, 11, 12, 13};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, columnPins, rows, columns);
LiquidCrystal lcd(0, 1, A4, 3, 4, 5);
char function_key(int n)
{
char temp = keypad.getKey();
if ((int)keypad.getState() == PRESSED)
{
if (temp != 0) {key = temp;}
}
if ((int)keypad.getState() == HOLD)
{
state++;
state = constrain(state, 1, n);
delay(holdDelay);
}
if ((int)keypad.getState() == RELEASED)
{
key += state;
state = 0;
}
delay(100);
return key;
}
String input_password(int num_char)
{
String passwd = "";
do
{
char temp = keypad.getKey();
if (temp != 0)
{
LCD_display(passwd.length(), 1, "*");
passwd += temp;
}
delay(100);
}
while (passwd.length() < num_char);
return passwd;
}
String Change_password(int num_char, String current_passwd)
{
LCD_display(0, 0, "OLD PASSWORD:");
String old_passwd = input_password(num_char);
if (old_passwd != current_passwd)
{
lcd.clear();
LCD_display(0, 0, "WRONG PASSWORD!");
return current_passwd;
}
LCD_display(0, 0, "NEW PASSWORD:");
String new_passwd = input_password(num_char);
LCD_display(0, 0, "CONFIRM PASSWORD");
String confirm_passwd = input_password(num_char);
if (confirm_passwd == new_passwd)
{
lcd.clear();
LCD_display(0, 0, "CHANGED PASSWORD");
return confirm_passwd;
}
else
{
lcd.clear();
LCD_display(0, 0, "NOTHING CHANGES!");
return current_passwd;
}
}
void Unlock()
{
lcd.clear();
LCD_display(0, 0, "INPUT PASSWORD:");
input_passwd = input_password(4);
if (input_passwd == default_passwd)
{
lcd.clear();
LCD_display(0, 0, "OPENNING!!!");
digitalWrite(greenled, HIGH);
digitalWrite(redled,LOW);
for (pos = 0; pos <= 180; pos += 1) { servo_A0.write(pos); delay(15); } delay(3000); lcd.clear(); LCD_display(0, 0, "CLOSING!!!"); for (pos = 180; pos >= 0; pos -= 1)
{
servo_A0.write(pos);
delay(15);
}
}
else
{
LCD_display(0, 0, "WRONG PASSWORD!");
tone(buzzer, 900, 2000);
digitalWrite(greenled, LOW);
digitalWrite(redled, HIGH);
delay(2000);
}
input_passwd = "";
key = 0;
}
void LCD_display(int column, int line, String message)
{
lcd.setCursor(column, line);
lcd.print(message);
}
void setup()
{
servo_A0.attach(A0);
servo_A0.write(pos);
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(button, INPUT);
lcd.begin(16, 2);
LCD_display(4, 0, "Welcome!");
delay(2000);
lcd.clear();
}
void loop()
{
btnState = digitalRead(button);
digitalWrite(greenled, LOW);
digitalWrite(redled,LOW);
char tempKey = keypad.getKey();
LCD_display(0, 0, "STATUS: LOCKED!");
LCD_display(0, 1, "UNLOCK: PRESS #");
if(btnState == LOW)
{
statebtn = 1;
}
if(tempKey == '#' || tempKey == '*')
{
statebtn = 2;
}
if(statebtn == 1)
{
lcd.clear();
LCD_display(0, 0, "OPENNING!!!");
digitalWrite(greenled, HIGH);
digitalWrite(redled,LOW);
for (pos = 0; pos <= 180; pos += 1) { servo_A0.write(pos); delay(15); } delay(3000); lcd.clear(); LCD_display(0, 0, "CLOSING!!!"); for (pos = 180; pos >= 0; pos -= 1)
{
servo_A0.write(pos);
delay(15);
}
statebtn = 0;
}
else if(statebtn == 2)
{
if (tempKey == unlock_key)
{
Unlock();
}
if (tempKey == change_pass_key) { default_passwd = Change_password(4, default_passwd); delay(2000); key =0; }
}
}
}
This Smart Door Lock System enhances security by combining keypad access with servo control. The flexibility to change passwords and the visual/auditory feedback add user-friendly features. Future improvements could involve integrating biometric sensors or connecting the system to a home automation network for remote monitoring and control.
The project demonstrates a simple yet effective approach to smart home security.