############################
# E036_SENSOR_THERMISTOR.PY: Lee entrada ADC con thermistor
# ENTRADAS: ADC en GPIO34 entre 0 y +3.3V
# SALIDAS: Lectura ADC en voltios GPIO26/27 LED verde/rojo
############################
import time # Gestión timing
from machine import Pin, ADC # Gestión GPIO
# Configura LED de control temperatura
led1=Pin(26, Pin.OUT) # LED verde
led2=Pin(27, Pin.OUT) # LED rojo
led1.off() # Apaga verde
led2.off() # Apaga rojo
# Umbrales de alarma de temperatura
temp_min = 24.0 # Temperatura verde
temp_max = 26.0 # Temperatura rojo
# Configura el pin ADC
pin_thermistor = 34
adc = ADC(Pin(pin_thermistor))
# Configura el rango del ADC a 0-3.3V (defecto 0-1V)
adc.atten(ADC.ATTN_11DB)
# Lee valor del thermistor
def leer_temperatura():
valor_adc = adc.read() # Lee valor del ADC
# Convierte el valor ADC a temperatura
temperatura = convertir_a_temperatura(valor_adc)
if temp_min < temperatura < temp_max:
led1.on() # Temperatura correcta
led2.off() # Enciende verde
else:
led1.off() # Temperatura incorrecta
led2.on() # Enciende rojo
print('Alarma Temperatura: {:.2f} °C'.format(temperatura))
return valor_adc, temperatura
# Convierte temperatura entre límites
def convertir_a_temperatura(valor_adc):
# Realiza la conversión del valor del termistor a temperatura
# ADC y temperatura en inversa
temperatura_min = -24 # Temperatura ADC mínimo
temperatura_max = 80 # Temperatura ADC máximo
valor_adc_min = 462 # Valor ADC mínimo esperado
valor_adc_max = 3813 # Valor ADC máximo esperado
ajuste = .818 # Valor de calibración
# Conversión lineal invertida y ajustada
temperatura = (temperatura_max - (temperatura_max - temperatura_min) * (valor_adc - valor_adc_min) /
(valor_adc_max - valor_adc_min)) * ajuste
return temperatura
# Bucle principal del script
def main():
try:
while True:
valor, temperatura = leer_temperatura()
print("Temperatura: {:.2f} °C".format(temperatura))
time.sleep(1) # Espera 1 segundo antes de leer nuevamente
except KeyboardInterrupt:
led1.off()
led2.off()
print("Programa terminado...")
if __name__ == "__main__":
print('\n'*20)
print('LECTURA DE TEMPERATURA POR THERMISTOR')
main()