from machine import Pin, I2C, PWM, ADC
import ssd1306
from time import sleep
# OLED display setup
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Define the buzzer, LEDs, and relay pins
buzzer = Pin(33, Pin.OUT) # Buzzer control pin
green_led = Pin(26, Pin.OUT) # Green LED control pin
red_led = Pin(25, Pin.OUT) # Red LED control pin
relay = Pin(32, Pin.OUT) # Relay module control pin
# Servo motor setup
servo_pin = Pin(17)
servo = PWM(servo_pin, freq=50)
# Potentiometer setup (simulating the fingerprint sensor)
potentiometer = ADC(Pin(34))
# Password and input handling
password = "1234"
input_password = ""
# Keypad setup
rows = [Pin(2, Pin.OUT), Pin(4, Pin.OUT), Pin(5, Pin.OUT), Pin(18, Pin.OUT)]
cols = [Pin(19, Pin.IN, Pin.PULL_UP), Pin(16, Pin.IN, Pin.PULL_UP), Pin(15, Pin.IN, Pin.PULL_UP), Pin(23, Pin.IN, Pin.PULL_UP)]
keys = [['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']]
def unlock_door():
relay.on() # Activate relay to unlock
green_led.on()
sleep(2)
relay.off() # Deactivate relay to lock
green_led.off()
def wrong_password_feedback():
red_led.on()
for _ in range(3):
buzzer.on()
sleep(0.1)
buzzer.off()
sleep(0.1)
red_led.off()
def read_keypad():
for row_num, row in enumerate(rows):
row.on()
for col_num, col in enumerate(cols):
if col.value() == 0:
row.off()
return keys[row_num][col_num]
row.off()
return None
def read_fingerprint():
# Read potentiometer value to simulate fingerprint detection
value = potentiometer.read()
return value
def display_message(message):
oled.fill(0) # Clear display
oled.text(message, 0, 0)
oled.show()
def main():
display_message("Enter Password:")
global input_password
while True:
key = read_keypad()
if key:
if key == '#': # '#' is the confirm key
if input_password == password:
display_message("Enter Fingerprint:")
while True:
fingerprint_value = read_fingerprint()
if fingerprint_value < 1000: # Adjust this threshold based on testing
display_message("Fingerprint Detected")
unlock_door()
break
else:
display_message("No Fingerprint Detected")
sleep(1)
input_password = ""
display_message("Enter Password:")
else:
display_message("Access Denied")
wrong_password_feedback()
input_password = ""
display_message("Enter Password:")
elif key == '*': # '*' is the reset key
input_password = ""
display_message("Enter Password:")
else:
input_password += key
display_message(input_password)
sleep(0.2) # Debounce delay
main()