from machine import Pin, ADC
from time import sleep
# Define pins
mq2_pin = ADC(Pin(34)) # MQ-2 sensor (analog)
pir_pin = Pin(27, Pin.IN) # PIR sensor (digital)
led_pin = Pin(25, Pin.OUT) # Red LED
#buzzer_pin = Pin(26, Pin.OUT)# Buzzer
# Configure ADC
mq2_pin.atten(ADC.ATTN_11DB) # Full range 0–3.3V
# Set a gas threshold (adjust as needed)
GAS_THRESHOLD = 1500
while True:
gas_value = mq2_pin.read()
motion_detected = pir_pin.value()
print("Gas Level:", gas_value, " | Motion:", motion_detected)
if gas_value > GAS_THRESHOLD:
led_pin.on()
buzzer_pin.on()
print("⚠️ Gas leakage detected!")
if motion_detected:
print("🚨 ALERT: Gas leakage + Motion detected nearby!")
else:
print("Gas detected but no motion.")
else:
led_pin.off()
#buzzer_pin.off()
sleep(1)