from machine import Pin, ADC
import time
import dht
# Pin assignments
ldr_pin = ADC(Pin(34)) # LDR connected to GPIO 34
sensor_pin = Pin(15) # DHT sensor connected to GPIO 15
pines = [Pin(12, Pin.OUT), Pin(13, Pin.OUT), Pin(14, Pin.OUT)]
colores = [[1, 0, 0], [1, 1, 0], [0, 1, 0]] # RGB colors
# Initialize DHT sensor
sensor = dht.DHT22(sensor_pin)
# Function to read LDR value
def read_ldr():
return ldr_pin.read() # Read analog value
# Setup function
def setup():
print("Initializing...")
for pin in pines:
pin.off() # Ensure all pins are off
# Main loop
def loop():
while True:
# Read LDR value
lux = read_ldr()
print("LDR Value:", lux)
# Read temperature and humidity
sensor.measure()
temperatura = sensor.temperature()
humedad = sensor.humidity()
print("Temperatura: {:.1f} °C".format(temperatura))
print("Humedad: {:.1f} %".format(humedad))
# Cycle through LED colors
for color in colores:
for i in range(3):
pines[i].value(color[i]) # Set the LED state
time.sleep(1) # Wait for 1 second
time.sleep(2) # Additional delay before the next loop
# Run the setup and loop
setup()
loop()