from machine import Pin, ADC
import time
# --- Pins ---
pir = Pin(14, Pin.IN)
led = Pin(2, Pin.OUT)
mq2 = ADC(Pin(36))
mq2.atten(ADC.ATTN_11DB)
# --- Thresholds ---
GAS_THRESHOLD = 300 # adjust according to MQ-2 calibration
# --- Main Loop ---
while True:
gas_value = mq2.read()
motion = pir.value()
print(f"Gas: {gas_value}, Motion: {motion}")
# Emergency Alert Condition
if gas_value > GAS_THRESHOLD and motion == 1:
# Flash LED rapidly
for _ in range(10): # flash 10 times
led.on()
time.sleep(0.1)
led.off()
time.sleep(0.1)
else:
led.off()
time.sleep(0.5)