import machine
from machine import Pin, ADC
import time
# Define the pin for the buzzer
buzzer_pin = Pin(15, Pin.OUT)
# Define the pin for the LED
led_pin = Pin(2, Pin.OUT)
# Define the pin for the analog temperature sensor
temp_sensor = ADC(Pin(32)) # Analog pin
# Calibration and conversion factors
SENSOR_MAX_VALUE = 4095 # Max value for 12-bit ADC
REFERENCE_VOLTAGE = 3.3 # Reference voltage in volts
TEMP_THRESHOLD = 60 # Temperature threshold for alarm in degrees Celsius
def read_temperature():
# Read the analog value from the temperature sensor
adc_value = temp_sensor.read()
# Convert the ADC value to voltage
voltage = (adc_value / SENSOR_MAX_VALUE) * REFERENCE_VOLTAGE
# Convert voltage to temperature (example conversion, adjust based on sensor)
temperature = (voltage - 0.5) * 100 # Example conversion for LM35 sensor
return temperature
def buzz(frequency, duration):
period = 1 / frequency
cycles = int(frequency * duration)
for _ in range(cycles):
buzzer_pin.on()
time.sleep(period / 2)
buzzer_pin.off()
time.sleep(period / 2)
while True:
# Read the temperature value
temp = read_temperature()
print(f"Temperature: {temp:.2f}°C") # Print temperature with 2 decimal places
# Fire alarm condition based on temperature
if temp >= TEMP_THRESHOLD:
# High-frequency alarm
frequency = 1000 # Hz
duration = 0.5 # seconds
print("Alarm Triggered!") # Debugging message
buzz(frequency, duration)
for _ in range(10): # LED blinking effect
led_pin.value(1)
time.sleep(0.2) # LED on duration
led_pin.value(0)
time.sleep(0.2) # LED off duration
print("RUNNN SAVE YOUR LIFE!!") # Debugging message
else:
buzzer_pin.value(0) # Turn off the buzzer
led_pin.value(0) # Turn off the LED
# Delay before the next reading
time.sleep(1)