from machine import Pin, ADC
from time import sleep
import math
leds = [
Pin(22, Pin.OUT),
Pin(21, Pin.OUT),
Pin(19, Pin.OUT),
Pin(18, Pin.OUT),
Pin(5, Pin.OUT),
Pin(17, Pin.OUT),
Pin(16, Pin.OUT),
Pin(4, Pin.OUT),
Pin(2, Pin.OUT),
Pin(15, Pin.OUT)
]
buzzer = Pin(14, Pin.OUT)
ntc = ADC(Pin(13))
ntc.atten(ADC.ATTN_11DB)
R_0 = 10000
T_0 = 298.15
Max = 2450
def temp():
valor = ntc.read()
V = (valor * Max) / 4095
R_NTC = R_0 * (V / (Max - V))
temperatura = (1 / ((math.log( R_NTC / R_0 ) / 3950) + (1 / T_0))) - 273.15 #Formula Steinhart-Hart
return temperatura
def acender(temperatura):
nivel = int(((temperatura + 24) / 104) * 10)
if nivel > 9:
nivel = 9
elif nivel < 0:
nivel = 0
for x in range(10):
if x <= nivel:
leds[x].value(1)
else:
leds[x].value(0)
def ligar_buzzer(temperatura):
if temperatura > 75:
buzzer.value(1)
else:
buzzer.value(0)
while True:
temperatura = temp()
acender(temperatura)
ligar_buzzer(temperatura)
print(f'Temperatura: {temperatura:.1f} °C')
sleep(1)