Temperature Sensor LED Strip Gauge


Step 1 - Build the Project

Using an LED Strip and a temperature sensor, create a temperature gauge that lights up more pixels when it's warm and fewer pixels when it's cool. 

Step 2 - Upload the Code

/* * An LED strip gauge for the temperature sensor. The warmer it is, * the more pixels are illuminated. */ //Include the LEDStrip library to use the commands #include "LEDStrip.h" const byte numPixels = 15; //number of pixels on a strip //Make an LED strip object: LEDStrip(numPixels,dataPin(DO),clockPin(CO)) LEDStrip strip = LEDStrip(numPixels, 13, 12); //Take a reading to seed the average value float tempAverage = analogRead(A4); void setup() { //Check the temp sensor pinout, getting it backwards ruins it pinMode(A4,INPUT); //Center pin of temp sensor //One of the outside pins is in 5V, the other is in GND row } void loop() { //Take a new temperature reading each loop and add it to the average //Each new reading contributes 10% to the average tempAverage = 0.9*tempAverage + 0.1*float(analogRead(A4)); //tempRead is cast to a float so it can be multiplied by decimals //Converting reading to F with conversion equation const int tempF = int(0.88*float(tempAverage) - 58.0); strip.clear(); //Clear the strip //Map the temp values of 60 to 80 onto the LED strip const int pixel = map(tempF,60,80,0,numPixels); //Pixel will have a value between 0 and numPixels //Turn on each pixel up to that number for (int i = 0; i < pixel;i++){ strip.setPixel(i, 200); //colors range from 0-300. 200 is green } strip.draw(); //Draw the pixels } //Remap temperature (60-80) to color (300-0), and display on all // LEDs with the strip.ALL in place of 'pixel' in setPixel //Hint: Replace the for loop and pixel map with one line of setPixel // (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 tempRead and set its value to the analogRead(A3).  Now any time you use the variable tempRead, 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'll create an integer variable called tempAverage. The value of this variable is the result of the equation to the right of the equals sign. The first operation multiplies the previous value of temperature value by 0.9. The second operation has two parts. First, tempRead is cast as a float. This means that for this calculation only, tempRead will be treated as a float variable, not an integer. You need a cast here because multiplying an integer by a decimal will just cut off the decimal portion of the product. After tempRead is cast, it is multiplied by 0.1. In effect, each time you run this line of code, your new temperature reading makes up 10% of your tempAverage.

Up to this point, your tempAverage variable has only held a voltage reading.  Here you create an integer tempF to convert that voltage reading to a temperature reading in degrees Fahrenheit. These values are specific to the component, so don't modify this line. You can see two more casts here, though: One casts tempAverage to a float, the other casts the variable's value back to an int.

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

Now you'll create an integer called pixel. The value of this variable is the result of the map function. The map function takes the temperatures 60-80F and maps those values onto the range 0-numPixels (15 pixels in this program). So a temperature of 60 equals 0 pixels and a temperature of 80 equals 15 pixels. Once this transformation has happened, that new value between 0 and 15 is assigned to the variable pixel.

The next step is to set the correct number of pixels to light up. You'll do this with a for loop. To create the for loop, you create a new integer variable called i and set it equal to 0. Now, as long as i is less than the value of pixel, i will increase by one (++) each time the for loop runs.

Inside the for loop, the .setPixel() method sets the pixel 'i' to the color value of 200. The first time the for loop runs, it will use a value of 0 for i. The second time it runs, the for loop will use a value of 1. This will continue until i is equal to the variable pixel.

Once the for loop has completed, the code moves on. The next method is .draw(), which displays the pixels that are set in the .setPixel() method. Because .draw() comes at the end, all of the pixels will appear to show up at once.