How to make a door lock security system with a Raspberry Pi board

How to make a door lock security system with a Raspberry Pi board

Hello and welcome back. In this project, we will learn how to make a door lock security system with a Raspberry Pi board. For that, I mainly used a 4*4 keypad module. That is, we can use a PIN to lock and unlock this system. Also, I added a relay module for this project. Therefore, we can connect the electromagnetic door lock to this system. I think this system is most applicable for door lockers, cabinets, etc. It depends on your requirement. Especially, when this project is assembled on PCB and PVC enclosure box, we can get a project with a good finish and accuracy.

If you are using this project for educational purposes only, please follow the steps below. Also, I have not used the electromagnetic door lock. But, you can connect it to the relay module. For that, use the circuit diagram.

  • If you want to learn more info about the keypad modules, please use this link.

Ok, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, place the GPIO extension board on the breadboard. And then, connect the ribbon cable to it.

Step 3

Thirdly, connect the LCD screen, buzzer, and relay module to the extension board.

How to make a door lock security system with a Raspberry Pi board

Step 4

Now, connect the keypad module to the extension board.

Step 5

Next, connect the ribbon cable, mouse, keypad, monitor, and SD card to the Raspberry Pi board, And then, provide a power supply.

Step 6

OK, now enable the I2C communication. For that, follow the instructions below.

Step 7

Let’s create the python script for this project. It’s as follows.

  • Full details of this project — Download
# Include the library files
import I2C_LCD_driver
import RPi.GPIO as GPIO
from time import sleep
# Enter column pins
C1 = 5
C2 = 6
C3 = 13
C4 = 19
# Enter row pins
R1 = 12
R2 = 16
R3 = 20
R4 = 21
# Enter buzzer pin
buzzer = 17
# Enter LED pin
Relay = 27
relayState = True
# Create a object for the LCD
lcd = I2C_LCD_driver.lcd()
#Starting text
lcd.lcd_display_string("System loading",1,1)
for a in range (0,16):
    lcd.lcd_display_string(".",2,a)
    sleep(0.1)
lcd.lcd_clear()
# The GPIO pin of the column of the key that is currently
# being held down or -1 if no key is pressed
keypadPressed = -1
# Enter your PIN
secretCode = "1111"
input = ""
# Setup GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(buzzer,GPIO.OUT)
GPIO.setup(Relay,GPIO.OUT)
GPIO.output(Relay,GPIO.HIGH)
# Set column pins as output pins
GPIO.setup(C1, GPIO.OUT)
GPIO.setup(C2, GPIO.OUT)
GPIO.setup(C3, GPIO.OUT)
GPIO.setup(C4, GPIO.OUT)
# Set row pins as input pins
GPIO.setup(R1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(R2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(R3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(R4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# This callback registers the key that was pressed
# if no other key is currently pressed
def keypadCallback(channel):
    global keypadPressed
    if keypadPressed == -1:
        keypadPressed = channel
# Detect the rising edges
GPIO.add_event_detect(R1, GPIO.RISING, callback=keypadCallback)
GPIO.add_event_detect(R2, GPIO.RISING, callback=keypadCallback)
GPIO.add_event_detect(R3, GPIO.RISING, callback=keypadCallback)
GPIO.add_event_detect(R4, GPIO.RISING, callback=keypadCallback)
# Sets all rows to a specific state. 
def setAllRows(state):
    GPIO.output(C1, state)
    GPIO.output(C2, state)
    GPIO.output(C3, state)
    GPIO.output(C4, state)
# Check or clear PIN
def commands():
    global relayState
    global input
    pressed = False
    GPIO.output(C1, GPIO.HIGH)
    
    # Clear PIN 
    if (GPIO.input(R1) == 1):
        print("Input reset!");
        lcd.lcd_clear()
        lcd.lcd_display_string("Clear",1,5)
        sleep(1)
        pressed = True
    GPIO.output(C1, GPIO.HIGH)
    # Check PIN
    if (not pressed and GPIO.input(R2) == 1):
        if input == secretCode:
            print("Code correct!")
            lcd.lcd_clear()
            lcd.lcd_display_string("Successful",1,3)
            
            if relayState:
                GPIO.output(Relay,GPIO.LOW)
                GPIO.output(buzzer,GPIO.HIGH)
                sleep(0.3)
                GPIO.output(buzzer,GPIO.LOW)
                sleep(1)
                relayState = False
                
            elif relayState == False:
                GPIO.output(Relay,GPIO.HIGH)
                GPIO.output(buzzer,GPIO.HIGH)
                sleep(0.3)
                GPIO.output(buzzer,GPIO.LOW)
                sleep(1)
                relayState = True
                  
            
        else:
            print("Incorrect code!")
            lcd.lcd_clear()
            lcd.lcd_display_string("Wrong PIN!",1,3)
            GPIO.output(buzzer,GPIO.HIGH)
            sleep(0.3)
            GPIO.output(buzzer,GPIO.LOW)
            sleep(0.3)
            GPIO.output(buzzer,GPIO.HIGH)
            sleep(0.3)
            GPIO.output(buzzer,GPIO.LOW)
            sleep(0.3)
            GPIO.output(buzzer,GPIO.HIGH)
            sleep(0.3)
            GPIO.output(buzzer,GPIO.LOW) 
        pressed = True
    GPIO.output(C1, GPIO.LOW)
    if pressed:
        input = ""
    return pressed
# reads the columns and appends the value, that corresponds
# to the button, to a variable
def read(column, characters):
    global input
    GPIO.output(column, GPIO.HIGH)
    if(GPIO.input(R1) == 1):
        input = input + characters[0]
        print(input)
        lcd.lcd_display_string(str(input),2,0)
    if(GPIO.input(R2) == 1):
        input = input + characters[1]
        print(input)
        lcd.lcd_display_string(str(input),2,0)
    if(GPIO.input(R3) == 1):
        input = input + characters[2]
        print(input)
        lcd.lcd_display_string(str(input),2,0)
    if(GPIO.input(R4) == 1):
        input = input + characters[3]
        print(input)
        lcd.lcd_display_string(str(input),2,0)
    GPIO.output(column, GPIO.LOW)
try:
    while True:       
        lcd.lcd_display_string("Enter your PIN:",1,0)
        
        # If a button was previously pressed,
        # check, whether the user has released it yet
        if keypadPressed != -1:
            setAllRows(GPIO.HIGH)
            if GPIO.input(keypadPressed) == 0:
                keypadPressed = -1
            else:
                sleep(0.1)
        # Otherwise, just read the input
        else:
            if not commands():
                read(C1, ["D","C","B","A"])
                read(C2, ["#","9","6","3"])
                read(C3, ["0","8","5","2"])
                read(C4, ["*","7","4","1"])
                sleep(0.1)
            else:
                sleep(0.1)
except KeyboardInterrupt:
    print("Stopped!")
  • Now, enter your PIN as you like. I have entered “1111”.

Step 8

Finally, copy and paste this script on the Thonny IDE. And then, save this script and click the run button.

OK, enjoy this project. The full video guide is below. So, see you in the next project and tutorial.

How to make a door lock security system with a Raspberry Pi board

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *