from machine import Pin
from time import sleep
import LiquidCrystal_I2C
import Keypad
# Pin configuration
RELAY_PIN = 19
row_pins = [12, 14, 27, 26] # GPIO pins connected to the keypad rows
col_pins = [25, 33, 32, 17] # GPIO pins connected to the keypad columns
# Passwords
password_1 = "1234"
password_2 = "4444"
password_3 = "55555"
input_password = ""
# Initialize LCD and keypad
lcd = LiquidCrystal_I2C(0x27, 16, 2) # Address: 0x27, 16x2 display
keypad = Keypad(row_pins, col_pins)
relay = Pin(RELAY_PIN, Pin.OUT)
relay.value(1) # Lock the door initially (active low)
lcd.init()
lcd.backlight()
def reset_password():
"""Resets the entered password and clears the LCD."""
global input_password
input_password = ""
lcd.clear()
def unlock_door():
"""Unlocks the door for 20 seconds and displays a success message."""
lcd.clear()
lcd.putstr("CORRECT!\nDOOR UNLOCKED!")
relay.value(0) # Unlock the door
sleep(20) # Wait for 20 seconds
relay.value(1) # Lock the door
reset_password()
def deny_access():
"""Denies access and displays an error message."""
lcd.clear()
lcd.putstr("INCORRECT!\nACCESS DENIED!")
sleep(2)
reset_password()
while True:
key = keypad.get_key()
if key:
print("Key Pressed:", key)
if key == '*':
reset_password()
elif key == '#':
if input_password in [password_1, password_2, password_3]:
unlock_door()
else:
deny_access()
else:
input_password += key
if len(input_password) == 1:
lcd.clear()
lcd.set_cursor(len(input_password) - 1, 0)
lcd.putstr('*') # Show '*' for hidden input