from machine import Pin, PWM, I2C
from time import sleep
import ssd1306
# Setup I2C on Pico (default I2C0 pins)
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
# Initialize the OLED display (128x64)
oled = ssd1306.SSD1306_I2C(128, 64, i2c, 0x3c)
# Initialize the Servo motor on GPIO 10
servo = PWM(Pin(10))
servo.freq(50) # Servo frequency
# Initialize the Buzzer on GPIO 11 (PWM for buzzer)
buzzer = PWM(Pin(11))
buzzer.freq(1000)
# Keypad setup
rows = [Pin(2, Pin.OUT),
Pin(3, Pin.OUT),
Pin(4, Pin.OUT),
Pin(5, Pin.OUT)]
cols = [Pin(6, Pin.IN, Pin.PULL_UP),
Pin(7, Pin.IN, Pin.PULL_UP),
Pin(8, Pin.IN, Pin.PULL_UP),
Pin(9, Pin.IN, Pin.PULL_UP)]
keypad = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
# Correct password
correct_password = "1111"
password = ""
count = 0
# Display initial prompt (split text into two lines)
oled.fill(0)
oled.text("Enter 4-digit", 10, 10)
oled.text("password:", 10, 20)
oled.show()
while True:
for row_num, row in enumerate(rows):
row.value(0) # Activate the current row
for col_num, col in enumerate(cols):
if col.value() == 0: # Check if the key is pressed
key = keypad[row_num][col_num]
print("Key pressed:", key)
if key.isdigit():
password += key
count += 1
oled.fill(0)
oled.text("Enter: " + "*" * count, 10, 30) # Show asterisk for each digit entered
oled.show()
sleep(0.3)
if count == 4:
if password == correct_password:
oled.fill(0)
oled.text("Access Granted!", 10, 30)
oled.show()
servo.duty_u16(65000) # Open the door
sleep(2)
servo.duty_u16(0) # Stop the servo
sleep(1)
buzzer.duty_u16(30000) # Start beep
sleep(0.4)
buzzer.duty_u16(0) # Stop beep
sleep(1)
servo.duty_u16(35000) # Close the door
sleep(2)
servo.duty_u16(0) # Stop the servo
else:
oled.fill(0)
oled.text("Access Denied!", 10, 30)
oled.show()
buzzer.duty_u16(30000) # Beep for denial
sleep(0.3)
buzzer.duty_u16(0)
sleep(0.3)
buzzer.duty_u16(30000)
sleep(0.3)
buzzer.duty_u16(0)
# Reset for next attempt
password = ""
count = 0
oled.fill(0)
oled.text("Enter 4-digit", 10, 10)
oled.text("password:", 10, 20)
oled.show()
row.value(1) # Deactivate the current row
sleep(0.1) # Small delay between keypad checks