Door Lock System with Password – Arduino Tutorial

4,139 views

Introduction

An Arduino password door lock system is a security system that utilizes an Arduino microcontroller, a 4×4 matrix keypad, a 5V Single Pole Double Throw (SPDT) relay, and a 12V Direct Current (DC) electric door lock to control access to a secure area. The system requires a user to input a password through the keypad, which is then processed by Arduino. If the entered password is correct, the Arduino will activate the SPDT relay, which in turn unlocks the door lock, allowing the user access. This system provides a convenient and secure way to control access to a room or building, making it ideal for use in homes, offices, and other secure environments.

A 4×4 matrix keypad is a device used to input numbers or characters into an electronic system. It consists of 16 buttons arranged in a 4×4 matrix format. These buttons are connected to a microcontroller, such as an Arduino, through a series of wires or connections. The microcontroller can then read the button presses, allowing the user to input information into the system. The 4×4 matrix keypad is a popular choice for projects such as password-protected door locks, calculators, or other applications where numeric or alphabetical input is needed.

Hardware Components

You will require the following hardware for Door Lock System using a Password with Arduino.

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Keypad 3×4 and 4×4 Kit1
4.Relay1
5.Power Adapter12V1
6.DC Power Jack1
7.LCDI2C1
8.Power Adapter for Arduino9V1
9.Electromagnetic Lock1
10.Jumper Wires1

Door Lock System with Arduino

  1. Start by including the Keypad library, which makes it easier to work with matrix keypads.
#include <Keypad.h>#include <Wire.h>
  1. Define the number of rows and columns on your keypad. In this case, it is a 4×4 keypad.
const int ROW_NUM = 4;
const int COLUMN_NUM = 4;
  1. Define the keymap of your keypad, which is the mapping of the keys on your keypad to the characters they represent.
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
  1. Specify the pins for each row and column of the keypad.
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};
  1. Create a Keypad object using the makeKeymap function and the pins for the rows and columns.
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
  1. In the setup function, start the serial communication at 9600 baud rate.
void setup(){
  Serial.begin(9600);
}
  1. In the loop function, read the keypad and store the pressed key in a variable.
void loop(){
  char key = keypad.getKey();
  ...
  1. Check if a key is pressed and compare it to the password. If the password is correct, set the relay pin to HIGH unlock the electromagnetic lock. If the password is incorrect, set the relay pin to LOW and keep the lock locked.
  ...
  if (key){
    if (key == '#'){
      if (password == "1234"){
        digitalWrite(relayPin, HIGH);
        Serial.println("Unlocked");
      } else {
        digitalWrite(relayPin, LOW);
        Serial.println("Wrong password");
      }
      password = "";
    } else {
      password += key;
      Serial.println(password);
    }
  }
}

Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

ArduinoRelay4×4 keypad
5VVCC
GNDGND
A0INP
D2R1
D3R2
D4R3
D5R4
D6C1
D7C2
D8C3
D9C4

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Progam Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

#include <Keypad.h>
const int RELAY_PIN  = A0; // the Arduino pin, which connects to the IN pin of relay
const int ROW_NUM    = 4; //four rows
const int COLUMN_NUM = 4; //four columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

const String password_1 = "1234ABC"; // change your password here
const String password_2 = "5642CD";  // change your password here
const String password_3 = "4545B";   // change your password here
String input_password;

void setup() {
  Serial.begin(9600);
  input_password.reserve(32); // maximum input characters is 32, change if needed
  pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output.
  digitalWrite(RELAY_PIN, HIGH); // lock the door
}

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

  if (key) {
    Serial.println(key);

    if (key == '*') {
      input_password = ""; // reset the input password
    } else if (key == '#') {
      if (input_password == password_1 || input_password == password_2 || input_password == password_3) {
        Serial.println("The password is correct, unlocking the door in 20 seconds");
        digitalWrite(RELAY_PIN, LOW);  // unlock the door for 20 seconds
        delay(20000);
        digitalWrite(RELAY_PIN, HIGH); // lock the door
      } else {
        Serial.println("The password is incorrect, try again");
      }

      input_password = ""; // reset the input password
    } else {
      input_password += key; // append new character to input password string
    }
  }
}

Working Explanation

The setup function in the code initializes the serial communication and sets the pin for the relay to output. The initial state of the relay is set to high, which locks the door.

The loop function continuously reads the input from the 4×4 matrix keypad. When a key is pressed, the code checks if it is the * or # key. If it is the * key, the input password string is reset. If it is the # key, the code checks if the input password matches one of the predefined passwords (password_1, password_2, or password_3). If the password is correct, the door is unlocked for 20 seconds and then locked again. If the password is incorrect, a message is printed saying to try again. If the key pressed is not * or #, the character is added to the input password string.

Applications

  • Electronic safes and cabinets
  • Automated teller machines (ATMs)
  • Personal identification number (PIN) verification
  • Time and attendance systems
  • Electronic voting systems
  • Electronic keyless door locks

Conclusion.

We hope you have found this Arduino – Door Lock System using a Password Circuit very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.