Temperature and Light

 

Create a circuit that uses the temperature sensor to measure temperature and output different colors of light based on how warm or cold it is.

Use the Red Green and Blue LEDs to achieve a wide range of color mixing. construct the paper box/diffuser from the tutorials.

The sensor we will be using is the TMP36, which you should be able to read on the flat side of the part. This is an analog device and returns a varying voltage from 0-5V depending on the temperature.

The following code will read the voltage output from the sensor:

/*
This simple sketch reads the temperature sensor and prints out the value to the serial monitor
*/


void setup() // runs once at the beginning or at reset
{
Serial.begin(9600); //Start the serial connection with the computer

}

void loop() // run over and over again
{
Serial.println(analogRead(0)); //reads analog pin 0 and prints that value to serial monitor

delay(1000); //waiting a second
}

Determine the sensor's range by holding it between your thumb and forefinger to apply heat. what is the value of the sensor at start, and after holding it for a few seconds.

Use if() statements to set up different conditions where different colors of light are created.

if(analogRead(0) < 150){
digitalWrite(redPin, HIGH);
digitalWrite(bluePin, LOW);
digitalWrite(greenPin, LOW);
}
if(analogRead(0) > 150){
digitalWrite(redPin, LOW);
digitalWrite(bluePin, HIGH);
digitalWrite(greenPin, LOW);
}
if(analogRead(0) > 160){
digitalWrite(redPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(greenPin, HIGH);
}

be careful that your conditions do not overlap. For instance if I say if ( analogRead(0)<150 ) turn on red light and if (analogRead(0)<160) turn on blue light, you will see that there is an overlap here, and that any value less than 150 is going to satisfy both cases.... You can also look into Boolean operators in the Arduino.cc Reference area, as it might be helpful to be able to say:

if ( analogRead(0)<150 and analogRead(0)>140)....which looks like this:
if ( analogRead(0)<150 && analogRead(0)>140)

Modify your conditions so that they don't overlap and each condition is able to be met.

In the first example you should see a distinct change in the light, from red to blue and then green. Now let's use analogWrite to allow the sensor to affect the output in a more fluid way. Take the reading from the temp sensor and plug it in to the analogWrite function so that you have some LEDs getting brighter as others get dimmer while the temperature sensor changes. This will give a fluid change of color across the range of the sensor. In order to do this right you need to scale the reading from the sensor to match the output of analogWrite. So, we need to scale a reading which can be 0-1024 and we want it to become 0-255.

map() allows us to do this and this is what it looks like:

x=map(analogRead(0), fromLow, fromHigh, toLow, toHigh);

which is this:

x=map(analogRead(0), 0, 1024, 0, 255);

 

analogRead(0);