import machine
import utime
from pico_i2c_lcd import I2cLcd
lm35_pin = machine.ADC(26)
potentiometer_pin = machine.ADC(27)
i2c = machine.I2C(0, sda=machine.Pin(0), scl=machine.Pin(1))
lcd = I2cLcd(i2c, 0x27, 4, 20) # Dirección I2C del LCD: 0x27, 4 filas, 20 columnas
relay_pin = machine.Pin(16, machine.Pin.OUT)
def read_lm35_temperature():
# Leer el valor del LM35 y convertirlo a temperatura en grados Celsius
lm35_value = lm35_pin.read_u16()
voltage = lm35_value / 65535 * 3.3 # Convertir el valor del ADC a voltaje (3.3V es la referencia)
temperature = voltage * 100 # Cada 1V equivale a 100 grados Celsius en el LM35
return temperature
def read_potentiometer_value():
# Leer el valor del potenciómetro (ajuste del setpoint) y convertirlo a temperatura en grados Celsius
potentiometer_value = potentiometer_pin.read_u16() / 65535 * 50 # Rango de 0 a 50 °C
return potentiometer_value
def display_readings_lcd(temperature, potentiometer_value):
# Mostrar las lecturas en el LCD
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Temp: {:.1f} C".format(temperature))
lcd.move_to(0, 1)
lcd.putstr("Pot.: {:.1f} C".format(potentiometer_value))
def control_temperature(temperature, potentiometer_value):
# Controlar el relé en función de la diferencia entre la temperatura y el valor del potenciómetro
if temperature > potentiometer_value:
relay_pin.value(1) # Encender el relé
else:
relay_pin.value(0) # Apagar el relé
# Bucle principal
while True:
temperature = read_lm35_temperature()
potentiometer_value = read_potentiometer_value()
display_readings_lcd(temperature, potentiometer_value)
control_temperature(temperature, potentiometer_value)
utime.sleep(1)