from machine import Pin, PWM
import time
# LED
led = Pin(2, Pin.OUT)
# Buzzer
buzzer = PWM(Pin(15))
buzzer.freq(1000)
# Servo
servo = PWM(Pin(18))
servo.freq(50)
# Password
PASSWORD = "1234"
entered = ""
# Keypad
rows = [Pin(13, Pin.OUT),
Pin(12, Pin.OUT),
Pin(14, Pin.OUT),
Pin(27, Pin.OUT)]
cols = [Pin(26, Pin.IN, Pin.PULL_DOWN),
Pin(25, Pin.IN, Pin.PULL_DOWN),
Pin(33, Pin.IN, Pin.PULL_DOWN),
Pin(32, Pin.IN, Pin.PULL_DOWN)]
keys = [
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']
]
def set_angle(angle):
duty = int((angle / 180) * 75 + 40)
servo.duty(duty)
def lock():
set_angle(0)
def unlock():
set_angle(90)
def beep():
buzzer.duty(512)
time.sleep(0.2)
buzzer.duty(0)
def alarm():
for _ in range(5):
buzzer.duty(512)
led.on()
time.sleep(0.2)
buzzer.duty(0)
led.off()
time.sleep(0.2)
def read_key():
for r in range(4):
for row in rows:
row.value(0)
rows[r].value(1)
for c in range(4):
if cols[c].value():
while cols[c].value():
pass
return keys[r][c]
return None
lock()
print("Enter PIN")
while True:
key = read_key()
if key:
print("Pressed:", key)
beep()
if key.isdigit():
entered += key
print("*" * len(entered))
if len(entered) == 4:
if entered == PASSWORD:
print("ACCESS GRANTED")
led.on()
unlock()
time.sleep(5)
lock()
led.off()
else:
print("WRONG PASSWORD")
alarm()
entered = ""
time.sleep(0.2)