from machine import Pin, PWM, ADC
import utime
import math
Buzzer = PWM(Pin(6))
thermistor_pin = Pin(26)
adc = ADC(thermistor_pin)
led = Pin(5, Pin.OUT)
def read_temperature():
beta = 3950
series_resistor = 10000
adc_value = adc.read_u16()
resistance = series_resistor / (65535 / adc_value - 1)
# using Steinhart-Hart equation we can get the desired values from NTC(Temperature Sensor)
steinhart = resistance / 10000 # (R/Ro)
steinhart = math.log(steinhart) # ln(R/Ro)
steinhart /= beta # 1/B * ln(R/Ro)
steinhart += 1.0 / (25 + 273.15) # + (1/To)
steinhart = 1.0 / steinhart # Invert
steinhart -= 273.15 # convert to C
return steinhart
while True:
temperature = read_temperature()
print("Temperature:", temperature, "°C")
if temperature > 45:
led.on()
# Activate buzzer for a short duration
Buzzer.duty_u16(3000)
Buzzer.freq(3000) # SET FREQUENCY as per your Choice
utime.sleep(0.001)
Buzzer.duty_u16(0)
utime.sleep(temperature / 1000)
utime.sleep(1)