Temperature Sensor LED Strip Color Changer

Step 1 - Build the Project

Using an LED Strip and a temperature sensor, create a temperature display that changes color based on the temperature. 

Step 2 - Upload the Code

/* *An LED strip that changes color depending on the temperature */ #include "LEDStrip.h" const byte numPixels = 15; //number of pixels on a strip /* *Make the LED strip object LEDStrip strip * = LEDStrip(numPixels, dataPin, clockPin); */ LEDStrip strip = LEDStrip(numPixels, 13, 12); void setup() { pinMode(A4,INPUT); //Temperature sensor pin } void loop() { const int minTemp= 60; //set the minimum expected temp as a variable const int maxTemp= 80; //Use an equation to convert the analogRead of the temp sensor to a //value in degrees Fahrenheit float tempF = int(0.88*float(analogRead(A4)) - 58.0); //limit the possible values of tempF to minTemp and maxTemp tempF = constrain(tempF,minTemp,maxTemp); //Set the pixels to the color corresponding to temperature strip.setPixel(strip.ALL,map(tempF,minTemp,maxTemp,0,100)); strip.draw(); //Draw the pixels that you set in .setPixel delay(250); } //Change the color range in the map (0,100) for a different range based on temp //(c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense

Step 3 - Read the Walkthrough

Above the setup, create an integer variable named numPixels. This will stand in for the number of LED pixels on your LED strip. Any time you use the integer numPixels, it will act as if you typed 15 in that place. 

When you're using an LED strip, you need to tell the library about that strip. In the line LEDStrip strip = LEDStrip(numPixels, 12, 11), you are telling the library "I'm using an LEDStrip and I want to name it 'strip'. The strip has 'numPixels' number of pixels and its data pin is connected on pin 12. Its clock pin is connected on pin 11. That is called creating an object and it links the hardware to the library.

In the setup() section of the code, set the pinMode of the temperature sensor to INPUT, so it sends a signal in to the Maker Board instead of waiting for commands from Maker Board.

In the loop of the code, first create a variable called tempValue and set its value to the analogRead(A3).  Now any time you use the variable soundValue, you're using the analog reading of pin A3. Keep in mind that the variable only updates its value on this line, not each time you use it. You're using the same single analogRead for an entire loop.

Next, you have to convert the analog reading to an actual temperature in Fahrenheit. These numbers are specific to the temperature sensor, so don't change those. After the conversion, the value of the variable tempF will be equal or very near the actual Fahrenheit temperature.

The .clear() method clears out the pixels and colors that were on the strip so you can write new values to the strip.

This program lights up all of the pixels on the strip at once, so you can use the strip.ALL command within the strip.setPixel() method. This means instead of specifying that you want pixel 2 to light up, you're instead lighting up every pixel at once. The next number you have to enter into the .setPixel() method is a color value. Here, you're replacing a single number with a mapped value.

The temperature range this program will cover is 60-80F. However, you have 100 potential color values to choose from. The map() function allows you to map a temperature of 60 to a color value of 0 and a temperature of 80 to a color value of 100. When the map function has run, tempF will be assigned a new value between 0 and 100. Finally, you use the strip.draw() method to display the values from the .setPixel() method. 

You can tweak this code to achieve a different set of colors. Remember that the LED strip has 300 color values available. Here, you're using 0-100 because it's a nice blue-to-red fade with temperature.