TMP36 analog sensor giving inconsistent readings

I’m using a TMP36 analog temperature sensor for a project that will light up certain colours of 5 RGB LEDs according to the readings from the sensor. I’m getting inconsistent readings, they start from the negatives and jump to the positives and they don’t read past 17 degrees C. My LED’s are not lighting up either when I change around the numbers, the only ones that seem to light up are the blue ones and they only light up when it reads numbers less than 5. I honestly do not know what’s going on, I need someone to shed some light on my problem please!

int tempPin= 0; // connected to analog pin 0

int LEDpins= (6,7,8); // the output pins that the three digital pins of the led connects to
// 6= red, 7=green , 8=blue
const int ON= LOW;
const int OFF= HIGH; // 

//setting the colours 
const int RED = (ON, OFF, OFF);
const int YELLOW= (ON, ON, OFF);
const int GREEN= (OFF, ON, OFF);
const int PURPLE= (ON, OFF, ON);
const int BLUE= (OFF, OFF, ON);
const int BLACK= (OFF, OFF, OFF);
const int CYAN= (OFF, ON, ON);




void setup ()
{
  
  Serial.begin(9600);
  
 // pinMode(tempPin,INPUT); //sensor is reading information
  // LEDs are getting information
  pinMode(LEDpins,OUTPUT);

}

void loop()
{
  float Temperature = getVolts(tempPin); // reading the sensor for the voltage
  int Temp2= Temperature; // creating a second temp. reading that has been converted
  Temp2 = (Temperature- .5) * 100; //converting from 10 mV/degree with 500 mV offset to degrees ((volatge - 500mV) times 100)
  
  
 
  Serial.println(Temp2); // print readings
  delay(1000); // wait a second to get the new reading
  
  if ((Temp2 >1) && (Temp2 <=5)) { // if the sensor reads from 1-5 degrees, turn on all the green LEDS
digitalWrite(LEDpins,GREEN);

  }
  
  if ((Temp2 <= 4)) { // if the sensor reads less than or equal to 23 degrees, turn off all leds (black)
    digitalWrite(LEDpins,BLACK);
 
  }
  
  if ((Temp2 > 6) && (Temp2 <= 10)) { // when reading is 21-23 degrees turn on all blue leds
    digitalWrite(LEDpins,BLUE);
  
  }
  
  if ((Temp2 > 11) && ( Temp2 <=12)) { // when reading is between 27-29 degreen turn on blue and green leds to make cyan
    digitalWrite(LEDpins,CYAN);
  }
  
  if ((Temp2 >= 17)) { // when the reading is above 33 degrees turn on red leds
    digitalWrite(LEDpins,RED);

  }
   
  if ((Temp2 >13) && (Temp2 <=16)) { // when the reading is between 24-25 turn on purple leds (red and blue)
    digitalWrite(LEDpins,PURPLE);

  
  }
  
  if (Temp2 == 26) { // when the reading is 26 degrees, turn on yellow (red and green)
    digitalWrite(LEDpins,YELLOW);

  }
  
} 
  
  
  float getVolts(float tempPin){
return (analogRead(tempPin) * .004882814); //converting from a 0 to 1024 digital range
  }

Here are some of the readings I’m getting:
-3
9
-3
9
-3
9
-3
10
-1
11
11
12
0
12
0
12
0
12
0
12
0
13
16
5
16
4
17
17
16
4
17
17
17
17
17
16
5
18
16
5
17
16
5
17
17
17
16
4
16
3
15
3
15
2
14
2
14
2
14
1
13
13
12
0
12
0
12
0
12
0
12
0
12
0
12
0
11
11
11
11
11
12
-1
11
11
11
11
11
10
-1
10
-1
10
-2
10
-2
10

What are you trying to do here? int LEDpins= (6,7,8); //this makes your LEDpins = to 8 ? const int RED = (ON, OFF, OFF); //this makes your RED = to 1 ? etc.

I hooked up 5 of the RGB LEDs to connect to only 3 pins, so the LEDpins= (6,7,8); means that I am using those pins to control all the LEDs. As for the const int RED = (ON, OFF, OFF); this shows that to turn on red, I have to send power to only 7 and 8 (which is why they are off). Pin 6= red leds, pin 7= green leds, and pin 8= blue leds.

Connect the oscilloscope to pin where the analog input goes from the TMP36, and see how stable the voltage is. If fluctuate, add tantalum capacitor directly to TMP36 pins as is recommended in the datasheet.

Hi, can you post a copy of your circuit please, CAD or just a pic of a hand drawn circuit will be great. A picture of your project would also help us to debug.

Tom..... :)

Please do set the TMP36 pin as input, it makes the code better to read. Mixing float and integers can easily go wrong. I'm not sure how the compiler will calculate the 'Temp2' variable. It is okay if you would use float for the temperature and use that float also for the 'if' statements.

The TMP36 is an analog sensor, and the analog value has the 5V as a reference. To get a good reference is important.

I suggest to make a few sketches that do only one task. Start with the TMP36, and try to get stable temperature. Don't use any leds or any extra code in that sketch.

Like LarryD, also I am not used to this: const int RED = (ON, OFF, OFF); Is that valid C++ programming ?

I hooked up 5 of the RGB LEDs to connect to only 3 pins

Start with 1 tri-colored LED, I assume it is similar to the one in the attached image.

int LEDpins= (6,7,8); <<<< NO

Example: should be something like this
int LEDpin1 = 6; //Red
int LEDpin2 = 7; //Green
int LEDpin3 = 8; //Blue

You then have to Turn on/off these o/ps to get the color you need

12-29-2013 8-07-29 PM.jpg

LarryD:

I hooked up 5 of the RGB LEDs to connect to only 3 pins

Start with 1 tri-colored LED, I assume it is similar to the one in the attached image.

Then read about LEDs, their voltage drops, the way to calculate resistor values, and above all, how much current it will take to drive 15 LEDs at full brightness. Then look up the maximum current you can source from an Arduino pin.