from machine import ADC, Pin
from time import sleep
import math
import functions
# Pin and constant definitions
ADC_Pin = ADC(26)
Led_red = Pin(28, Pin.OUT)
Led_Blue = Pin(27, Pin.OUT)
interrupt_btn = Pin(16, Pin.IN, Pin.PULL_UP)
# Const For NTC
NTC_BETA = 3950 # Beta value of the thermistor
NTC_R0 = 10000 # Resistance at 25°C
NTC_T0 = 298.15 # Temperature at 25°C in Kelvin
# Functions defnitions
btn_pressed = False
def read_analogue_voltage(pin):
global btn_pressed
btn_pressed = True
interrupt_btn.irq(trigger=Pin.IRQ_FALLING, handler=read_analogue_voltage)
def read_ntc():
# Read the analog value from the NTC pin
adc_value = ADC_Pin.read_u16()
# Convert the analog value to a resistance
resistance = (adc_value / 65535.0) * 10000
# Calculate the temperature using the Steinhart-Hart equation
t = 1 / (1 / NTC_T0 + math.log(resistance / NTC_R0) / NTC_BETA)
t_celsius = t - 273 # Convert to Celsius
return t_celsius
adc_value = ADC_Pin.read_u16()
def main():
freq = functions.convert_to_frequency(adc_value) * -1
filterd_freq = functions.low_pass_filter(freq)
while True:
global btn_pressed
if btn_pressed is True:
btn_pressed = False
temperature = read_ntc()
print("Temperature: {:.1f}°C".format(temperature))
if temperature >= 55:
Led_red.on()
Led_Blue.off()
functions.buzz_frequency(filterd_freq)
else:
Led_Blue.on()
Led_red.off()
functions.buzz_frequency(0)
sleep(0.2)
if __name__ == '__main__':
main()