from machine import Pin, ADC
import dht
import time
# Pin setup
pir = Pin(15, Pin.IN)
gas_sensor = ADC(26)
dht_sensor = dht.DHT22(Pin(14))
buzzer = Pin(13, Pin.OUT)
led = Pin(12, Pin.OUT)
def motion_detected(pin):
print("🚨 Motion Detected!")
buzzer.value(1)
led.value(1)
time.sleep(1)
buzzer.value(0)
led.value(0)
# PIR interrupt
pir.irq(trigger=Pin.IRQ_RISING, handler=motion_detected)
while True:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
gas = gas_sensor.read_u16() / 65535 * 100
print(f"Temp: {temp}°C, Humidity: {hum}%, Gas Level: {gas:.1f}%")
# Gas warning test
if gas > 50:
print("⚠️ Gas Leak Detected!")
buzzer.value(1)
led.value(1)
time.sleep(1)
buzzer.value(0)
led.value(0)
time.sleep(2)