from machine import Pin, PWM
import dht
import time
DHT_PIN = 4
LED_PIN = 5
sensor = dht.DHT22(Pin(DHT_PIN))
led = PWM(Pin(LED_PIN), freq=1000)
TEMP_MIN = 20
TEMP_MAX = 30
def map_value(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
def read_sensor():
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print("Temp: {:.1f}°C Hum: {:.1f}%".format(temp, hum))
return temp
except OSError as e:
print("Error al leer el sensor:", e)
return None
while True:
temp = read_sensor()
if temp is not None:
intensity = map_value(temp, TEMP_MIN, TEMP_MAX, 0, 1023)
intensity = max(0, min(1023, intensity))
led.duty(int(intensity))
time.sleep(0.5)