from machine import Pin, ADC, Timer
import math
BETA = 3950
R1 = 10000
# This defines the top voltage expected (in the case of the pico 3.3v)
V_REF = 3.3
# Configure a pin for LED control
LED = Pin(0, Pin.OUT)
adc = ADC(Pin(26))
# This function will be called by 'temp_timer' to check
# The current voltage on the pin, then math magic will
# convert this into the temp in celsius
# Note:
def get_temp():
# Read the current voltage from the pin
analog_value = adc.read_u16()
voltage = (analog_value / 65535) * V_REF
resistance = R1 / ((V_REF / voltage) - 1)
kelvin = 1 / (1 / 298.15 + 1 / BETA * math.log(resistance / 10000))
celsius = kelvin - 273.15
return celsius
def check_temp(temp_timer):
temperature = get_temp()
print(f"Temperature: {temperature:.2f} ℃")
# Test if temp is over 65 Degrees Celcius, if so turn on alert
if temperature >= 65.0 or temperature <= -2:
LED.on()
# If the temp is lower than 65 all is good
else:
LED.off()
# This time works just like the LED flasher buy it is being used to trigger
# the function that checks if the switch is currently on or off
temp_timer = Timer(mode=Timer.PERIODIC, period=1000, callback=check_temp)