LED and Temperature

Hello everyone,

I am new at creating arduino projects. I really want to create a project that uses a temperature sensor to determine body temperature and make an LED display a corresponding color (like a mood ring). I am having trouble finding code and was wondering if anyone knows where I can find working code?

All help is greatly appreciated!

Hello and welcome.

Why do you believe there must be code out there somewhere that does what you need? Why would there be?

Have you already purchased some components like a temperature sensor or an rgb led? If so, give us links to them. Also give links to any code you have found that does parts of what you need.

Paul

Hi, and thanks for the reply. I have a LM35 temperature sensor and and RGB LED. I would really like to get the code from this link to work:

OR

(in the comments section at the bottom)

I want the sensor to read the body temp and display a corresponding color with the LED. I cannot get this code to work when compiled in the arduino program, a bunch or errors appear.

brandon15601:
a bunch or errors appear.

Damn pity I can't see the code or the errors (I'm not going to some other site to hunt it down) or I could probably help you make it work.

This code has no errors but the color will not change with temperature. I connected the sensor but I'm not exactly sure which port it should be inserted into (the lead the sends info to the computer) can you happen to tell via this code?:

float RGB[3];

float tempC;

int ldrPin = 0; // LDR in Analog Input 0 to read the ambient light

int analog_tempPin1 = 1;

int ambientLight; // variable to store the value of the ambient light

int ledPin1 = 11; // red LED in Digital Pin 11 (PWM)

int ledPin2 = 10; // green LED in Digital Pin 10 (PWM)

int ledPin3 = 9; // blue LED in Digital Pin 9 (PWM)

int ledPin4 = 7; // LED connected to digital pin 7

int ledPin5 = 6;

void setup(){

pinMode(ledPin1,OUTPUT); // tell arduino it's an output

pinMode(ledPin2,OUTPUT);// tell arduino it's an output

pinMode(ledPin3,OUTPUT); // tell arduino it's an output

// set all the outputs to low

digitalWrite(ledPin1,LOW);

digitalWrite(ledPin2,LOW);

digitalWrite(ledPin3,LOW);

pinMode(ledPin4, OUTPUT);

pinMode(ledPin5, OUTPUT);

Serial.begin(9600);

}

void loop(){

for (float x=0;x<PI;x=x+0.00001){

RGB[0]=255*abs(sin(x*(180/PI))); // calculate the brightness for the red led

RGB[1]=255*abs(sin((x+PI/3)*(180/PI))); // calculate the brightness for the green led

RGB[2]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the blue led

ambientLight=analogRead(ldrPin); // read an store the ambient light

if(ambientLight>600){ // start only if the ambient light is very low

// write the brightness on the leds

analogWrite(ledPin1,RGB[0]);

analogWrite(ledPin2,RGB[1]);

analogWrite(ledPin3,RGB[2]);

}

else{

digitalWrite(ledPin1,LOW);

digitalWrite(ledPin2,LOW);

digitalWrite(ledPin3,LOW);

}

for(int i=0;i<3;i++){

if(RGB[i]<1){

delay(100);

}

if(RGB[i]<5){

delay(50);

}

if(RGB[i]<10){

delay(10);

}

if(RGB[i]<100){

delay(5);

}

}

delay(1);

tempC = analogRead(analog_tempPin1); //read the value from the sensor

tempC = (5.0 * tempC * 100.0)/1024.0; //convert the analog data to temperature

Serial.print("analog ");

Serial.print(analog_tempPin1);

Serial.print(" ");

Serial.println(tempC); //send the data to the computer

if (tempC < 40){

digitalWrite(ledPin4, HIGH); // set the LED on

digitalWrite(ledPin5, LOW); // set the LED off

delay(1000); // wait for a second

digitalWrite(ledPin4, LOW); // set the LED off

digitalWrite(ledPin5, HIGH); // set the LED on

delay(1000); // wait for a second

}

if (tempC > 40){

digitalWrite(ledPin4, HIGH); // set the LED on

digitalWrite(ledPin5, LOW); // set the LED off

delay(100); // wait for 0.1second

digitalWrite(ledPin4, LOW); // set the LED off

digitalWrite(ledPin5, HIGH); // set the LED on

delay(100); // wait for 0.1second

}

}

}

And this code has a ton of errors which are listed below the code, but the concept is exactly what I want to happen with the LED colors:

/* "The Magic Crystal Mood Ball"
Stupid Pet Trick - ITP 2011
*/

//TMP36 Pin Variables
int temperaturePin = 0; //input: the analog pin the TMP36 is connected

//RGB LED pins
int ledDigitalOne[] = {9, 10, 11}; //output: the three digital pins of the RGB LED
//9 = redPin, 10 = greenPin, 11 = bluePin

const boolean ON = LOW;
const boolean OFF = HIGH;

//Predefined Colors
const boolean RED[] = {ON, OFF, OFF};
const boolean GREEN[] = {OFF, ON, OFF};
const boolean BLUE[] = {OFF, OFF, ON};
const boolean YELLOW[] = {ON, ON, OFF};
const boolean CYAN[] = {OFF, ON, ON};
const boolean MAGENTA[] = {ON, OFF, ON};
const boolean WHITE[] = {ON, ON, ON};
const boolean BLACK[] = {OFF, OFF, OFF};

void setup()
{
for(int i = 0; i < 3; i++)
{
pinMode(ledDigitalOne[i], OUTPUT); //Set the RGB LED pins as outputs
}
Serial.begin(9600); //Start the serial connection with the computer
}
void loop()
{
float temperature = getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor
temperature = (((temperature - .5) * 100)*1.8)+32; //converting from 10 mv per degree wit 500 mV offset
int newTemperature = temperature; //to degrees ((volatge - 500mV) times 100)
Serial.println(newTemperature); //printing the result

delay(7000); //waiting 7 seconds to get a new result

// Set the three LEDs to any predefined color depending of the temperature in ºF
if ((newTemperature>40) && (newTemperaturesetColor(ledDigitalOne, BLACK)
Serial.println("Black");
}
else if ((newTemperature>=72) && (newTemperaturesetColor(ledDigitalOne, WHITE)
{
Serial.println("White");
}
else if ((newTemperature>=74) && (newTemperaturesetColor(ledDigitalOne, GREEN)
{
Serial.println("Green");
}
else if ((newTemperature>=76) && (newTemperaturesetColor(ledDigitalOne, CYAN)
{
Serial.println("Cyan");
}
else if ((newTemperature>=78) && (newTemperaturesetColor(ledDigitalOne, BLUE)
{
Serial.println("Blue");
}
else if ((newTemperature>=80) && (newTemperaturesetColor(ledDigitalOne, YELLOW)
{
Serial.println("Yellow");
}
else if ((newTemperature>=82) && (newTemperaturesetColor(ledDigitalOne, RED)
{
Serial.println("Red");
}
else 
{
setColor(ledDigitalOne, MAGENTA);
Serial.println("Magenta");
}

  float getVoltage(int pin)
    {
      return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
    }
 // Function to set the color
}
void setColor(int* led, boolean* color)
{
for(int i = 0; i < 3; i++)
  {
    digitalWrite(led[i], color[i]);
  }
}
// A version of setColor that allows for using const boolean colors
void setColor(int* led, const boolean* color)
{
boolean tempColor[] = {color[0], color[1], color[2]};
setColor(led, tempColor);
}

ERRORS:

sketch_apr28a.ino: In function 'void loop()':
sketch_apr28a.ino:43:72: error: 'newTemperaturesetColor' was not declared in this scope
sketch_apr28a.ino:44:1: error: expected ')' before 'Serial'
sketch_apr28a.ino:44:24: error: expected ')' before ';' token
sketch_apr28a.ino: At global scope:
sketch_apr28a.ino:46:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:50:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:54:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:58:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:62:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:66:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:70:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:82:1: error: expected declaration before '}' token
Error compiling.

OK, so the forum has struck again and mangled my extensive reply.

It appears that the problem is that you have quoted code from an "instructable".

This summary will therefore have to suffice.

You don't need floats, ints will do; all that matters is the ADC output, the range of those numbers (which isn't wide).

The output of an LM35, 10mV / degC, would vary by 10mV:
from below normal (98 F, 36.7 C, 367mV)
to ill (100 F, 37.7C, 377mV).
[99 F, 37.2 C, 372 mV.]

Even with the INTERNAL ("1.1V") ADC reference, where 1mV is about 1 bit, that's really not workable (IMHO).