Please complete the basic installation-chapters before starting a project.
This project will take the temperature reading from a DHT11 sensor and change the color of the LEDs on the NeoPixel accordingly.
You will need the following to build this project:
Assemble the components according to the circuit diagram below:
Feel free to change the temperature thresholds to fit your environment more accurately, the script will print the temperature reading in the terminal so you can easily determine what thresholds to use. Then upload this code to your board.
1from machine import Pin2from time import sleep3import neopixel4import dht56PIXEL_NUMBER = 107np = neopixel.NeoPixel(Pin(10), PIXEL_NUMBER) # Pin D789SENSOR_PIN = 5 # Pin D210TEMP_SENSOR = dht.DHT11(Pin(SENSOR_PIN))11sleep(1)1213red = (255, 0, 0) # set to red14green = (0, 128, 0) # set to green15blue = (0, 0, 64) # set to blue1617def hotLED():18 for i in range(0, PIXEL_NUMBER):19 np[i] = red20 np.write()212223def coldLED():24 for i in range(0, PIXEL_NUMBER):25 np[i] = blue26 np.write()2728def neutralLED():29 for i in range(0, PIXEL_NUMBER):30 np[i] = green31 np.write()3233while(1):34 TEMP_SENSOR.measure()35 print(TEMP_SENSOR.temperature())36 temp = TEMP_SENSOR.temperature()3738 if(temp >= 28): #Threshold for when the LEDs indicate a hot temperature39 hotLED()4041 if(temp <= 22): #Threshold for when the LEDs indicate a cold temperature42 coldLED()4344 if(temp > 22 and temp < 28): #Threshold for when the LEDs indicate a neutral temperature45 neutralLED()4647 sleep(1)