from machine import Pin, PWM, I2C, ADC
import utime
import math
from dht import DHT22
from pico_i2c_lcd import I2cLcd
# Initialize PWM for servo motor
servo_pwm = PWM(Pin(15))
servo_pwm.freq(50)
# Initialize I2C for LCD
i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
# Define keypad and pin configurations
matrix_keys = [['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']]
keypad_rows = [14, 13, 12, 11]
keypad_columns = [10, 9, 8, 7]
# Initialize row and column pins for keypad
col_pins = [Pin(keypad_columns[x], Pin.IN, Pin.PULL_DOWN) for x in range(4)]
row_pins = [Pin(keypad_rows[x], Pin.OUT) for x in range(4)]
for row_pin in row_pins:
row_pin.value(1)
# Define secret pin and guess
secret_pin = ['2', '5', '8', '8', '0', '0']
guess = []
# Ultrasonic sensor setup
echo = Pin(4, Pin.IN)
trigger = Pin(5, Pin.OUT)
led = Pin(6, Pin.OUT)
# Define buzzer pin
buzzer = PWM(Pin(2))
# Initialize DHT22 sensor
dht = DHT22(Pin(26))
# NTC temperature sensor setup
BETA = 3950
R1 = 10000
V_REF = 3.3
adc = ADC(Pin(28))
# Define LED pins
red_led = Pin(27, Pin.OUT)
blue_led = Pin(21, Pin.OUT)
def buzzerFunc():
buzzer.duty_u16(1000)
utime.sleep(1)
buzzer.duty_u16(0)
def setup():
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Enter Password:")
def scankeys():
global guess
for row in range(4):
row_pins[row].high()
for col in range(4):
if col_pins[col].value() == 1:
key_press = matrix_keys[row][col]
print("You have pressed:", key_press)
lcd.move_to(0, 1)
lcd.putstr("".join(guess) + key_press)
utime.sleep(0.3)
guess.append(key_press)
if len(guess) == 6:
checkPin(guess)
guess = []
lcd.clear()
lcd.putstr("Enter Password:")
row_pins[row].low()
def checkPin(guess):
if guess == secret_pin:
print("You got the secret pin correct")
lcd.clear()
lcd.putstr("Door unlock")
# Turn servo to 180 degrees
for position in range(4500, 9000, 50):
servo_pwm.duty_u16(position)
utime.sleep(0.05)
utime.sleep(1)
# Turn servo back to 0 degrees
for position in range(9000, 4500, -50):
servo_pwm.duty_u16(position)
utime.sleep(0.05)
utime.sleep(1)
else:
print("Pin is wrong")
lcd.clear()
lcd.putstr("Incorrect pin")
utime.sleep(2)
lcd.clear()
lcd.putstr("Enter Password:")
def measure_distance():
trigger.value(1)
utime.sleep_us(10)
trigger.value(0)
while echo.value() == 0:
pulse_start = utime.ticks_us()
while echo.value() == 1:
pulse_end = utime.ticks_us()
pulse_duration = pulse_end - pulse_start
distance = (pulse_duration * 0.0343) / 2 # Speed of sound is 343 m/s
return distance
def read_ntc_temperature():
analog_value = adc.read_u16()
voltage = (analog_value / 65535) * V_REF
resistance = R1 / ((V_REF / voltage) - 1)
kelvin = 1 / (1 / 298.15 + 1 / BETA * math.log(resistance / 10000))
celsius = kelvin - 273.15
return celsius
def read_dht_sensor():
try:
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
return temp, hum
except Exception as e:
print("Error reading DHT sensor:", e)
return None, None
print("Enter the secret Pin")
setup()
while True:
scankeys()
# Read DHT sensor
temp, hum = read_dht_sensor()
if temp is not None and hum is not None:
lcd.clear()
lcd.putstr(f"T:{temp}C H:{hum}%")
print(f"Temperature: {temp}°C Humidity: {hum}% ")
if temp > 30 or hum > 70: # Adjust thresholds as needed
buzzerFunc()
else:
print("DHT sensor reading failed.")
lcd.clear()
lcd.putstr("DHT read error")
utime.sleep(2)
lcd.clear()
# Read NTC temperature
ntc_temp = read_ntc_temperature()
if ntc_temp is not None:
print("NTC Temperature: {:.2f} ℃".format(ntc_temp))
lcd.putstr(f"NTC Temp:{ntc_temp:.2f}C")
# Control LED based on NTC temperature
if ntc_temp > 25:
red_led.on()
blue_led.off()
else:
red_led.off()
blue_led.on()
else:
print("NTC temperature sensor reading failed.")
lcd.clear()
lcd.putstr("NTC read error")
utime.sleep(2)
lcd.clear()
# Measure distance
distance = measure_distance()
lcd.putstr(f"Distance: {distance:.2f}cm")
if distance < 10: # Adjust threshold as needed
led.on()
else:
led.off()
utime.sleep(2)
lcd.clear()