"CAPETILLO ESPERANZA JOSÉ ÁNGEL 7BK"
from machine import ADC, Pin, PWM
from time import sleep
import dht
# Configuración de pines
sensor = dht.DHT22(Pin(1))
pot_max = ADC(Pin(28))
pot_min = ADC(Pin(27))
alarma = PWM(Pin(26))
led = Pin(16, Pin.OUT)
# Constantes
ADC_MAX_VALUE = 65535
VOLTAGE_REF = 3.3
CONVERSION_FACTOR = 30.3 # Factor de conversión para temperatura
BUZZER_DUTY_CYCLE = 32768
BUZZER_FREQUENCY = 1000
BUZZER_CYCLES = 10
SLEEP_INTERVAL = 1
TEMP_THRESHOLD_ALARMA = 50 # Umbral de temperatura en °C
def rango(adc):
"""
Convierte la lectura de un ADC a una temperatura estimada.
"""
lectura = adc.read_u16()
voltaje = (lectura / ADC_MAX_VALUE) * VOLTAGE_REF
temperatura = voltaje * CONVERSION_FACTOR
return temperatura
def buzzer():
"""
Activa el buzzer en un ciclo de encendido/apagado.
"""
alarma.freq(BUZZER_FREQUENCY)
for _ in range(BUZZER_CYCLES):
alarma.duty_u16(BUZZER_DUTY_CYCLE)
sleep(0.05)
alarma.duty_u16(0)
sleep(0.05)
alarma.duty_u16(0)
while True:
temp_max = rango(pot_max)
pot_min_value = pot_min.read_u16()
sensor.measure()
temp_actual = sensor.temperature()
print(f"Temperatura actual: {temp_actual:.2f}°C")
print(f"Temperatura máxima (pot_max): {temp_max:.2f}°C")
print(f"Valor de pot_min: {pot_min_value}")
# Verificación de condiciones
if temp_max > TEMP_THRESHOLD_ALARMA:
buzzer()
print("Temperatura de pot_max excede los 50°C: ALARMA ACTIVADA")
if pot_min_value < (ADC_MAX_VALUE * 0.05): # Ajuste para detectar un valor cercano a 0
led.value(1)
print("pot_min en posición mínima: LED ACTIVADO")
else:
led.value(0)
sleep(SLEEP_INTERVAL)