from machine import Pin, ADC, PWM
import time
# ---------- GAS SENSOR ----------
gas_sensor = ADC(26)
# ---------- BUZZER ----------
buzzer = PWM(Pin(15))
# ---------- STEPPER MOTOR ----------
A_plus = Pin(2, Pin.OUT)
A_minus = Pin(3, Pin.OUT)
B_plus = Pin(4, Pin.OUT)
B_minus = Pin(5, Pin.OUT)
# Gas threshold
threshold = 30000
sequence = [
(1,0,1,0),
(0,1,1,0),
(0,1,0,1),
(1,0,0,1)
]
def rotate_motor():
for step in sequence:
A_plus.value(step[0])
A_minus.value(step[1])
B_plus.value(step[2])
B_minus.value(step[3])
time.sleep(0.003)
def stop_motor():
A_plus.value(0)
A_minus.value(0)
B_plus.value(0)
B_minus.value(0)
# ---------- MAIN LOOP ----------
while True:
gas_value = gas_sensor.read_u16()
print("Gas Level:", gas_value)
if gas_value > threshold:
print("Gas Detected!")
buzzer.freq(1000) # sound frequency
buzzer.duty_u16(30000) # continuous sound
rotate_motor() # fan ON
else:
print("Gas Normal")
buzzer.duty_u16(0) # buzzer OFF
stop_motor() # fan OFF
time.sleep(0.1)