from machine import Pin, ADC, PWM
import time
# MQ2 Gas Sensor
gas_sensor = ADC(26)
# Buzzer
buzzer = PWM(Pin(15))
# Stepper driver pins
step = Pin(2, Pin.OUT)
direction = Pin(3, Pin.OUT)
enable = Pin(4, Pin.OUT)
# Enable driver
enable.value(0)
# Correct motor direction
direction.value(0)
# Gas threshold
threshold = 35000
def rotate_fan():
# slower motor rotation
step.value(1)
time.sleep_ms(8)
step.value(0)
time.sleep_ms(8)
while True:
gas_value = gas_sensor.read_u16()
print("Gas Level:", gas_value)
if gas_value > threshold:
# Alarm ON
buzzer.freq(1000)
buzzer.duty_u16(30000)
# Fan ON
rotate_fan()
else:
# Alarm OFF
buzzer.duty_u16(0)
time.sleep(0.1)