from machine import Pin
from dht import DHT22
from neopixel import Neopixel
import time
# Configure the number of LEDs and the pins for NeoPixel and DHT22
NUM_LEDS = 16
PIN_NUM = 0
TEMP_PIN = 15
# Initialize the NeoPixel strip and the DHT22 sensor
np = Neopixel(NUM_LEDS, 0, PIN_NUM, mode="RGB")
dht = DHT22(Pin(TEMP_PIN))
# Function to update LED colors based on temperature
def update_lights_based_on_temperature():
while True:
dht.measure() # Measure temperature and humidity
temp = dht.temperature() # Get the temperature in Celsius
print(f"Temperature: {temp}°C")
# Set LED color based on temperature range
if temp < 20:
color = (0, 0, 255) # Blue for cold temperatures
elif 20 <= temp <= 35:
color = (255, 0, 0) # Red for normal temperatures
else:
color = (0, 255, 0) # Green for hot temperatures
np.fill(color) # Set all LEDs to the selected color
np.show() # Update the LED strip
time.sleep(1) # Wait for 1 second before the next reading
# Call the function to start updating lights based on temperature
update_lights_based_on_temperature()