from machine import Pin, I2C
import time
from i2c_lcd import I2cLcd
# Initialize I2C for LCD
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000)
AddressOfLcd = 0x27 # Change this if your LCD has a different I2C address
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16) # 2 rows, 16 columns
# Keypad setup
row_pins = [19, 18, 25, 27] # GPIO pins for rows
col_pins = [32, 33, 26] # GPIO pins for columns
# Define keypad layout
keypad_layout = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['*', '0', '#']
]
# Initialize row pins as outputs and column pins as inputs with pull-up resistors
row_pins_objects = [Pin(pin, Pin.OUT) for pin in row_pins]
col_pins_objects = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in col_pins]
# Password settings
correct_password = "1234" # Set your correct password here
input_password = "" # Variable to store the entered password
# Function to read the keypad
def read_keypad():
for i, row in enumerate(row_pins_objects):
row.value(0) # Activate the current row
for j, col in enumerate(col_pins_objects):
if col.value() == 0: # Check if a key is pressed
row.value(1) # Deactivate the current row
return keypad_layout[i][j] # Return the pressed key
row.value(1) # Deactivate the current row
return None # Return None if no key is pressed
# Function to display a message on the LCD
def display_message(message, clear=True):
if clear:
lcd.clear() # Clear the LCD if needed
lcd.putstr(message) # Display the message
# Function to check the entered password
def check_password():
global input_password
if input_password == correct_password:
display_message("Enter") # Display "Enter" if the password is correct
else:
display_message("Try Again") # Display "Try Again" if the password is incorrect
input_password = "" # Reset the entered password
time.sleep(2) # Wait for 2 seconds
display_message("Enter Password:") # Prompt for password again
# Main program
display_message("Enter Password:") # Initial prompt
while True:
key = read_keypad() # Read the keypad
if key:
if key == '#': # Confirm password when '#' is pressed
check_password()
elif key == '*': # Clear input when '*' is pressed
input_password = ""
display_message("Enter Password:")
else:
input_password += key # Append the pressed key to the input password
display_message("*" * len(input_password), clear=False) # Display '*' for each character
time.sleep(0.3) # Debounce delay