hero

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:

Circuit for the temperature display
Circuit for the temperature display

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 Pin
2from time import sleep
3import neopixel
4import dht
5
6PIXEL_NUMBER = 10
7np = neopixel.NeoPixel(Pin(10), PIXEL_NUMBER) # Pin D7
8
9SENSOR_PIN = 5 # Pin D2
10TEMP_SENSOR = dht.DHT11(Pin(SENSOR_PIN))
11sleep(1)
12
13red = (255, 0, 0) # set to red
14green = (0, 128, 0) # set to green
15blue = (0, 0, 64) # set to blue
16
17def hotLED():
18 for i in range(0, PIXEL_NUMBER):
19 np[i] = red
20 np.write()
21
22
23def coldLED():
24 for i in range(0, PIXEL_NUMBER):
25 np[i] = blue
26 np.write()
27
28def neutralLED():
29 for i in range(0, PIXEL_NUMBER):
30 np[i] = green
31 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 temperature
39 hotLED()
40
41 if(temp <= 22): #Threshold for when the LEDs indicate a cold temperature
42 coldLED()
43
44 if(temp > 22 and temp < 28): #Threshold for when the LEDs indicate a neutral temperature
45 neutralLED()
46
47 sleep(1)

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.