from machine import Pin, ADC, PWM
import utime as time
# Define pin for NTC sensor and buzzer
ntc_pin = 27 # GPIO pin connected to the NTC sensor (Analog pin)
buzzer_pin = 22 # GPIO pin connected to the buzzer (Digital pin)
# Initialize ADC for reading analog values
adc = ADC(Pin(ntc_pin))
# Initialize buzzer
buzzer = Pin(buzzer_pin, Pin.OUT)
# Initialize PWM for the buzzer
buzzer_pwm = PWM(buzzer)
# Function to read temperature from NTC sensor
def read_temperature():
# Read analog value from NTC sensor
analog_value = adc.read_u16()
# Convert analog value to voltage
voltage = analog_value * (3.3 / 65535)
# Convert voltage to temperature (assuming linear relationship)
# Replace the conversion formula with the appropriate calibration for your NTC sensor
temperature = (voltage - 0.5) * 100 # Example linear conversion
return temperature
# Function to play tone
def play_tone(frequency, duration):
buzzer_pwm.freq(frequency) # Set frequency
buzzer_pwm.duty_u16(32768) # 50% duty cycle (50% volume)
time.sleep_ms(duration) # Sound for specified duration
buzzer_pwm.duty_u16(0) # Turn off the buzzer
while True:
# Read temperature from NTC sensor
temperature = read_temperature()
# Check if temperature is at or above 69 degrees Celsius
if temperature >= 69.0:
print("Temperature is at or above 69 degrees Celsius:", temperature)
# Sound the buzzer with a tone
play_tone(1000, 1000) # Frequency: 1000 Hz, Duration: 1 second
buzzer.value(1) # Turn on the buzzer (optional)
time.sleep(1) # Sound for 1 second
buzzer.value(0) # Turn off the buzzer (optional)
else:
print("Temperature:", temperature, "degrees Celsius")
time.sleep(5) # Wait for 5 seconds before reading temperature again