from machine import ADC, Pin
from utime import sleep
from math import log
## Constants
BETA = 3950
KELVIN_CONSTANT = 273.15
## Functions
def adc_to_celsius(x):
return (1 / (log(1/(65535/x - 1))/BETA + 1/298.15) - KELVIN_CONSTANT)
sensor_temp = ADC(26)
green_led = Pin(14, Pin.OUT)
red_led = Pin(15, Pin.OUT)
while True:
reading = sensor_temp.read_u16()
temperature = adc_to_celsius(reading)
print(temperature)
if (temperature > 20):
red_led.on()
green_led.off()
print("Too Hot! The temperature is:", temperature, "(Celcius)")
elif (temperature <15):
red_led.off()
green_led.on()
print("Too Cold! The temperature is:", temperature, "(Celcius)")
else:
print("In range! The temperature is:", temperature, "(Celcius)")
red_led.off()
green_led.off()
sleep(2)