from machine import Pin, ADC, PWM
from time import sleep
# Inisialisasi sensor gas
mq135 = ADC(Pin(34))
mq135.atten(ADC.ATTN_11DB) # agar bisa membaca hingga ~3.3V
mq135.width(ADC.WIDTH_12BIT) # hasil baca 0 - 4095
# Inisialisasi LED RGB (PWM)
red = PWM(Pin(15), freq=1000)
green = PWM(Pin(2), freq=1000)
blue = PWM(Pin(4), freq=1000)
def set_rgb(r, g, b):
red.duty(1023 - r)
green.duty(1023 - g)
blue.duty(1023 - b)
while True:
gas_value = mq135.read()
print("Gas value:", gas_value)
if gas_value > 2000:
# Banyak gas: LED hijau
set_rgb(0, 1023, 0)
else:
# Sedikit gas: LED merah
set_rgb(1023, 0, 0)
sleep(0.5)