import time
from machine import Pin, ADC
from utime import sleep
import math
#Definition of pin numbers
b0 = machine.Pin(0, machine.Pin.IN)
l0 = machine.Pin(1, machine.Pin.OUT)
temp_sensor = machine.Pin(26, machine.Pin.IN)
adc = ADC(0)
BETA = 3950
temp = 0
pressed_pin = 0
#Interrupt function
button_state = 0
def interrupt_callback(pin):
global button_state, pressed_pin
button_state = button_state << 1 | pin.value()
button_state = button_state & 0xFF
if button_state == 0x80:
pressed_pin = 1
#Main function
def main():
global pressed_pin, b0, l0, temp_sensor, adc, BETA, temp
b0.irq(trigger = machine.Pin.IRQ_FALLING, handler = interrupt_callback)
while True:
analogValue = adc.read_u16()
temp = 1 / (math.log(1 / (65535. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15
if pressed_pin:
for x in range(5):
print(f"{temp:.2f} °C")
sleep(0.5)
pressed_pin = 0
if __name__ == "__main__":
main()