from machine import ADC, Pin, I2C
import time
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# --- Configuración del LCD I2C ---
I2C_ADDR = 0x27
LCD_COLUMNS = 16
LCD_LINES = 2
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
lcd = I2cLcd(i2c, I2C_ADDR, LCD_LINES, LCD_COLUMNS)
lcd.clear()
# --- Entrada analógica del MQ-2 ---
mq_sensor = ADC(Pin(34))
mq_sensor.atten(ADC.ATTN_11DB)
# --- Entrada del potenciómetro (para el umbral ajustable) ---
pot_umbral = ADC(Pin(35))
pot_umbral.atten(ADC.ATTN_11DB)
# --- Bucle principal ---
while True:
valor_gas = mq_sensor.read() # Lectura del MQ-2
umbral = pot_umbral.read() # Umbral definido por potenciómetro
# Comparación
hay_gas = valor_gas > umbral
mensaje = "Gas Detectado" if hay_gas else "Gas no detectado"
# Mostrar en LCD
lcd.clear()
lcd.putstr("G:{:4d} U:{:4d}".format(valor_gas, umbral)) # Línea 1
lcd.move_to(0, 1)
lcd.putstr(mensaje) # Línea 2
# Mostrar por consola
print("Gas: {:4d} | Umbral: {:4d} | {}".format(valor_gas, umbral, mensaje))
time.sleep(1)