from machine import Pin, ADC, PWM
import dht
import time
################ Temperature Monitoring Configuration ##############
# Define the GPIO pin where the DHT22 data pin is connected
dht_pin = Pin(4) # Use GPIO 4 (can be changed to any other GPIO pin)
# Create a DHT22 object
sensor = dht.DHT22(dht_pin)
# User Defined Threshold
user_threshold = 50
################## Alert System Configuration #######################
# Set up potentiometer
potentiometer_pin = 34
# Potentiometer is connected to GPIO 34 (ADC1)
potentiometer = ADC(Pin(potentiometer_pin))
potentiometer.width(10) # Setting ADC Resolution to 10-bits (0-1023)
# Set up buzzer
buzzer_pin = Pin(15) # Buzzer is connected to GPIO 15
buzzer = PWM(buzzer_pin)
# Set up LED
Led = Pin(18,Pin.OUT)
Led.value(0)
# Set buzzer PWM frequency and duty cycle
buzzer.freq(1000) # Initial frequency of 1000 Hz
buzzer.duty(0) # Start with the buzzer on
while True:
################ Temperature Monitoring ##################
# Trigger a measurement
sensor.measure()
# Read temperature and humidity
temperature = sensor.temperature()
# Print the results
print('Temperature: {:.1f}°C'.format(temperature))
#################### Alert System #######################
# Read potentiometer value (0-1023)
pot_value = potentiometer.read()
if temperature > user_threshold:
# Set duty cycle to a moderate value (e.g., 50%)
buzzer.duty(pot_value)
# LED turn on
Led.value(1)
# Display alert system status
print("Alert System is On !!")
else:
# Set duty cycle to 0 (Turn off buzzer)
buzzer.duty(0)
# LED turn off
Led.value(0)
# Display alert system status
print("Alert System is Off !!")
# Delay for stability
time.sleep(1)