Temperature Display
Use a temperature sensor together with a NeoPixel stick, giving you visual feedback on the current temperature.
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.
Required Hardware
You will need the following to build this project:
Circuit
Assemble the components according to the circuit diagram below:
Code
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 dht5
6PIXEL_NUMBER = 107np = neopixel.NeoPixel(Pin(10), PIXEL_NUMBER) # Pin D78
9SENSOR_PIN = 5 # Pin D210TEMP_SENSOR = dht.DHT11(Pin(SENSOR_PIN))11sleep(1)12
13red = (255, 0, 0) # set to red14green = (0, 128, 0) # set to green15blue = (0, 0, 64) # set to blue16
17def hotLED():18 for i in range(0, PIXEL_NUMBER):19 np[i] = red20 np.write()21
22 23def coldLED():24 for i in range(0, PIXEL_NUMBER):25 np[i] = blue26 np.write()27 28def neutralLED():29 for i in range(0, PIXEL_NUMBER):30 np[i] = green31 np.write()32
33while(1):34 TEMP_SENSOR.measure()35 print(TEMP_SENSOR.temperature())36 temp = TEMP_SENSOR.temperature()37
38 if(temp >= 28): #Threshold for when the LEDs indicate a hot temperature39 hotLED()40 41 if(temp <= 22): #Threshold for when the LEDs indicate a cold temperature42 coldLED()43 44 if(temp > 22 and temp < 28): #Threshold for when the LEDs indicate a neutral temperature45 neutralLED()46
47 sleep(1)
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.