import time
time.sleep(0.1) # Wait for USB to become ready
print("Hello, Pi Pico W!")
from machine import Pin, PWM
from time import sleep
import dht
# DHT22 sensor pin
DHT_PIN = 2 # Replace with your actual pin number
# Buzzer pin
BUZZER_PIN = 15 # Replace with your actual pin number
# Temperature threshold
TEMP_THRESHOLD = 30.0
# Initialize DHT22 sensor
dht_sensor = dht.DHT22(Pin(DHT_PIN))
# Initialize buzzer (PWM for sound control)
buzzer = PWM(Pin(BUZZER_PIN))
buzzer.freq(1000) # Set buzzer frequency (adjust as needed)
buzzer.duty_u16(0) # Turn off buzzer initially
def read_temperature():
try:
dht_sensor.measure()
temperature = dht_sensor.temperature()
return temperature
except OSError as e:
print("Failed to read sensor:", e)
return None
def play_alarm():
for _ in range(3): # Play alarm for a few cycles
buzzer.duty_u16(1000) # Adjust duty cycle for volume
sleep(0.2)
buzzer.duty_u16(0)
sleep(0.2)
# Main loop
while True:
temperature = read_temperature()
if temperature is not None:
print("Temperature: {:.1f} °C".format(temperature))
if temperature > TEMP_THRESHOLD:
print("Temperature exceeding threshold! Triggering alarm...")
play_alarm()
else:
buzzer.duty_u16(0) # Ensure buzzer is off
sleep(2)