from machine import Pin, ADC, Timer
import time
import math
button = Pin(14, Pin.IN, Pin.PULL_UP)
led = Pin(15, Pin.OUT)
thermistor = ADC(26)
debounce_time = 200
last_interrupt_time = 0
# Constants for NTC Thermistor
BETA = 3950 # Beta parameter
T0 = 298.15 # Reference temperature (25°C in Kelvin)
R0 = 10000 # Reference resistance at T0 (10k ohms)
R_DIVIDER = 10000 # Known resistor in the voltage divider (10k ohms)
VCC = 3.3 # Supply voltage
def read_temperature():
# Read the ADC value from the thermistor
adc_value = thermistor.read_u16()
# Calculate the voltage across the thermistor
voltage = adc_value * (VCC / 65535)
# Calculate the resistance of the thermistor
R_thermistor = R_DIVIDER * (VCC / voltage - 1)
# Calculate the temperature in Kelvin using the Beta parameter equation
temperature_k = 1 / (1/T0 + math.log(R_thermistor/R0) / BETA)
# Convert temperature to Celsius
temperature_c = temperature_k - 273.15
return temperature_c
def button_isr(pin):
global last_interrupt_time
current_time = time.ticks_ms()
if time.ticks_diff(current_time, last_interrupt_time) > debounce_time:
temperature = read_temperature()
print("Temperature: {:.2f} °C".format(temperature))
last_interrupt_time = current_time
# Setup button interrupt
button.irq(trigger=Pin.IRQ_FALLING, handler=button_isr)
# Timer callback for blinking LED
def blink_led(timer):
led.toggle()
# Setup timer to blink LED every second
timer = Timer()
timer.init(period=1000, mode=Timer.PERIODIC, callback=blink_led)
# Main loop (empty, as we rely on interrupts and timer)
while True:
pass