from machine import Pin, ADC
import time
# Setup pins
pir = Pin(15, Pin.IN)
fan_led = Pin(2, Pin.OUT)
# MQ-2 connected to ADC GPIO34
mq2 = ADC(Pin(34))
mq2.atten(ADC.ATTN_11DB) # Full range: 0-3.3V
# Threshold for high gas detection (adjust based on calibration)
GAS_THRESHOLD = 2000 # Example threshold value (ADC units)
while True:
gas_value = mq2.read()
person_present = pir.value() == 1
print(f"Gas Level: {gas_value}, Person Present: {person_present}")
if gas_value > GAS_THRESHOLD and person_present:
fan_led.on() # Turn fan ON
else:
fan_led.off() # Turn fan OFF
time.sleep(1)