from machine import Pin, ADC
from neopixel import NeoPixel
import time
# Configurar el sensor MQ-2 en GPIO 34 (Entrada Analógica)
mq2 = ADC(Pin(34))
mq2.atten(ADC.ATTN_11DB) # Rango completo de 0-3.3V
# Configurar NeoPixels en GPIO 27 con 4 LEDs
NUM_LEDS = 4
neo = NeoPixel(Pin(27), NUM_LEDS)
def set_color(r, g, b):
"""Función para cambiar todos los LEDs de color"""
for i in range(NUM_LEDS):
neo[i] = (r, g, b)
neo.write()
while True:
gas_value = mq2.read() # Leer el valor del sensor
print("Nivel de gas:", gas_value)
# Encender LEDs según el nivel de gas detectado
if gas_value < 1000:
set_color(0, 255, 0) # Verde (bajo nivel de gas)
elif gas_value < 2000:
set_color(255, 165, 0) # Naranja (nivel medio)
else:
set_color(255, 0, 0) # Rojo (nivel alto, peligro)
time.sleep(1) # Esperar 1 segundo