from machine import ADC, Pin
from time import sleep
# MQ sensor analog output on GP26 (ADC0)
gas_sensor = ADC(26)
# Alert devices
led = Pin(2, Pin.OUT)
buzzer = Pin(28, Pin.OUT)
# Optional: Digital output from MQ sensor
gas_digital = Pin(15, Pin.IN)
while True:
analog_value = gas_sensor.read_u16() # 0–65535
voltage = analog_value * 3.3 / 65535 # Convert to volts
print("Gas Level:", analog_value, "Voltage:", round(voltage, 2), "V")
# Threshold (adjust as needed)
if analog_value > 35000 or gas_digital.value() == 1:
print("⚠️ High Gas Detected!")
led.on()
buzzer.on()
else:
led.off()
buzzer.off()
sleep(5)