TMP36, odd readings...

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
KarlInNewmill
 
Posts: 5
Joined: Tue Jan 08, 2013 2:47 pm

TMP36, odd readings...

Post by KarlInNewmill »

Hi everyone,

I've got an TMP36 attached to an arduino Mega 2560 on Analog pin 0, getting the ground and 5v from the card.
When reading, I get the readings 177 from the board.

Nomatter how I try to convert this to Celicius, I get really odd values since it indicates +33 degress celcius.
In the room, I've got top 22 degrees.
I've tried numerous different sensors (all Tmp36's) but still the same readings.

What am I doing wrong?????

Thanks in advance!

/Karl

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: TMP36, odd readings...

Post by zener »

Need more info. What is the raw reading? Post your code in a code box. How do you display or determine the final temperature value?

KarlInNewmill
 
Posts: 5
Joined: Tue Jan 08, 2013 2:47 pm

Re: TMP36, odd readings...

Post by KarlInNewmill »

Hi,

Below is some other code I found while googling. It's closer to reality but still off quite a bit.
I've checked and the current temp in the room (right next to the TMP36) is 21degrees.

The output from the code below is:
175 reading, 3.61 V power supply, 0.62 volts, 11.66 degrees C
174 reading, 3.79 V power supply, 0.64 volts, 14.33 degrees C
175 reading, 3.76 V power supply, 0.64 volts, 14.25 degrees C
175 reading, 3.76 V power supply, 0.64 volts, 14.25 degrees

Code: Select all

  
  #define BANDGAPREF 14
  int refReading = analogRead(BANDGAPREF);

  // now calculate our power supply voltage from the known 1.05 volt reading
  float supplyvoltage = (1.05 * 1024) / refReading;
  
  //getting the voltage reading from the temperature sensor
  int reading = analogRead(sensorPin);  

  // converting that reading to voltage
  float voltage = reading * supplyvoltage / 1024; 
 
  // now print out the temperature
  float temperatureC = (voltage - 0.5) * 100 ;   //converting from 10 mv per degree wit 500 mV offset
                                                                     //to degrees ((volatge - 500mV) times 100)
  

  if (temperatureC!=maxi)
  {
      maxi=temperatureC;
      Serial.print(reading); Serial.print(" reading, ");
      Serial.print(supplyvoltage); Serial.print(" V power supply, ");
      Serial.print(voltage); Serial.print(" volts, ");
      Serial.print(temperatureC); Serial.println(" degrees C");
  }

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: TMP36, odd readings...

Post by adafruit_support_bill »

What is the value of "supplyVoltage"? And how do you have everything wired?

Post a photo and the complete code.

KarlInNewmill
 
Posts: 5
Joined: Tue Jan 08, 2013 2:47 pm

Re: TMP36, odd readings...

Post by KarlInNewmill »

Hi,

Supplyvoltage is calculated, look in the code.

Wiring is really dead-simple.
Just 3 wires, Gnd, 5v and A0 to their respective connector on the Tmp36.

The complete code:

Code: Select all

/*
An open-source LM35DZ Temperature Sensor for Arduino. This project will be enhanced on a regular basis
(cc) by Daniel Spillere Andrade , http://www.danielandrade.net
http://creativecommons.org/license/cc-gpl
*/

int sensorPin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature

#define BANDGAPREF 14   // special indicator that we want to measure the bandgap

void setup()
{
  Serial.begin(9600); // start serial communication
}

void loop()
{
int refReading = analogRead(BANDGAPREF);  
  
  
  // now calculate our power supply voltage from the known 1.05 volt reading
  float supplyvoltage = (1.05 * 1024) / refReading;
  
  
  //getting the voltage reading from the temperature sensor
  int reading = analogRead(sensorPin);  

  // converting that reading to voltage
  float voltage = reading * supplyvoltage / 1024; 
 
  // print out the voltage
  
 
  // now print out the temperature
  float temperatureC = (voltage - 0.5) * 100 ;   //converting from 10 mv per degree wit 500 mV offset
                                               //to degrees ((volatge - 500mV) times 100)
  
//  Serial.println(refReading);
  //Serial.print(temperatureF); Serial.println(" degress F");
  /*
  Serial.print(supplyvoltage); //Serial.println("V power supply");
  Serial.print(voltage); Serial.print(" volts ");
  Serial.print(temperatureC); Serial.println(" degrees C");*/
  if (temperatureC!=maxi)
  {
    maxi=temperatureC;
    Serial.print(reading); Serial.print(" reading, ");
      Serial.print(supplyvoltage); Serial.print(" V power supply, ");
  Serial.print(voltage); Serial.print(" volts, ");
  Serial.print(temperatureC); Serial.println(" degrees C");
  }
 
  delay(1000);                                     //waiting a second
}

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: TMP36, odd readings...

Post by adafruit_support_bill »

Supplyvoltage is calculated, look in the code.
So is the temperature. I guess they must both be correct then. :wink:

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: TMP36, odd readings...

Post by zener »

I would do this:

1) Add "refReading" to your print out info.
2) Measure your supply voltage with a volt meter
3) Measure the sensor voltage with a voltmeter (* edit, added this step *)

Then see if all the numbers are as they should be. Often times you need to take an anaolg read multiple times in succession to get a stable reading. Often the first one is bad, since the sampling cap has to charge.
Last edited by zener on Tue Jan 08, 2013 7:56 pm, edited 1 time in total.

JD3
 
Posts: 101
Joined: Sat Jul 28, 2012 8:01 am

Re: TMP36, odd readings...

Post by JD3 »

Your biggest problem is you are using a TMP36 and using a program for the LM35.

The TMP35 is functionally the same as the LM35 - but you are using the TMP36.

LM35 - 250mv @ 25 degrees celcius
TMP36 - 750mv @ 25 degrees celcius.

Look at the data sheets :
http://www.analog.com/static/imported-f ... _36_37.pdf

You need to calculate the voltage for the sensor being used.
Here is the tutorial from LadyAda:
http://learn.adafruit.com/tmp36-temperature-sensor

If you are using standard 5v reference voltage try using this basic sketch.
it displays the temperature in degrees Fahrenheit.

Code: Select all

/*  Sketch written by JD3 2012
  Uses TMP36 temperature senser from Adafruit

 Pin 1 (left)    +5v (or 3.3v)
 Pin 2 (middle)  Analog 0
 Pin 3 (right)   Ground
 
 Adafruit Protoshield w/ TMP36 IC uses Analog pin 3
 */

int volt = 0;
int Ftemp = 0;  // use float for decimal, int for whole 

void setup() {
  Serial.begin(9600); 
}


void loop() {
  volt = analogRead(3);
  Ftemp = (((volt * 4.8828125-500)/10)*9)/5 +32;  // 5v reference
  Serial.print(Ftemp);
  Serial.println(" degrees");
  delay(1000);
}


User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: TMP36, odd readings...

Post by zener »

I don't think he is using the wrong formula. Look here: http://learn.adafruit.com/tmp36-temperature-sensor

He is using the correct formula for the TMP36, although he has modified it to use volts instead of millivolts.

JD3
 
Posts: 101
Joined: Sat Jul 28, 2012 8:01 am

Re: TMP36, odd readings...

Post by JD3 »

Code: Select all

void loop()
{
int refReading = analogRead(BANDGAPREF); 
 
 
  // now calculate our power supply voltage from the known 1.05 volt reading
  float supplyvoltage = (1.05 * 1024) / refReading;
What is refReading doing?

BANDGAPREF is set to 14, so refReading is a reading from analog pin 14?
What's connected to pin 14?
Last edited by JD3 on Fri Jan 11, 2013 2:10 pm, edited 1 time in total.

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: TMP36, odd readings...

Post by zener »

JD3 wrote:What is refReading doing?

BANDGAPREF is set to 14, so refReading is a reading from analog pin 14?
What's connected to pin 14?

It is that portion of the code that is causing this problem.
That part of the code self calibrates the AD converter, since the supply voltage is used as Vref HI but it is not known exactly what it might be. The internal band gap ref is known, so that is used to discover what the Vref HI is. That part of the code seems fine. If the OP tries what I suggested then we might get somewhere.

JD3
 
Posts: 101
Joined: Sat Jul 28, 2012 8:01 am

Re: TMP36, odd readings...

Post by JD3 »

I did not know about the band gap reference. So I looked up some info on that...
Thanks for explaining that Zener. Something new to play with :)

Several places checked show that analogRead() no longer supports the band gap function?
http://jeelabs.org/2012/05/04/measuring ... e-bandgap/

Also this post in Adafruit forums:
http://forums.adafruit.com/viewtopic.php?f=22&t=29946

KarlInNewmill
 
Posts: 5
Joined: Tue Jan 08, 2013 2:47 pm

Re: TMP36, odd readings...

Post by KarlInNewmill »

Hi guys,

Sorry for the late reply but here it comes.
It was my fumbling that caused this sh#t :-)

I took the sensor from the wrong bag... after reading the actual text on the bag, I realized it wasnt what I thought it was.
It was a MCP9701 giving the output 19,5mv / degree C... so now the readings correct... hmmm....sorry :-)

Thanks again though for the rapid and creative responses!

/Karl

User avatar
rowshay
 
Posts: 5
Joined: Thu Mar 06, 2014 1:35 pm

Re: TMP36, odd readings...

Post by rowshay »

I have found similar problems using the TMP36 temperature sensor on the Uno vs Mega. The readings being reported by the Mega 2560 were up to 4-7 degrees higher than what the exact same script (for the temperature reading/reporting portion) and jumpers/components were on the Uno. Where the Uno was reporting in the range of 25.20 to 26.66, the Mega was reporting 29.59 to 32.52.

The only difference between the two boards is that the Mega has the CC3000 WiFi shield on it, which is pushing the temperature value up to a web server on my network every 60 seconds (which stopped suddenly after 6 hours of posting, but that's another issue).

When I pulled the WiFi shield off and just connected everything to the Mega, and uploaded the same script, the numbers being reported between boards is only about 1-2 degree difference. (The TMP36 seems to jump around 1-3 degrees between readings of 30 seconds or less, but the average is fairly constant)

Note that I am using the 0.56" 7-segment LED HT16K33 backpack to display the results for on both boards.


So, it appears that the CC3000 shield introduces some variances in the analoge pin A0's voltage.

User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: TMP36, odd readings...

Post by adafruit_support_mike »

Check the voltages on the 5v pins of each Arduino.

The Arduino's ADC defaults to the 5v EXTERNAL voltage reference, which is taken from the 5v supply rail. That comes out of the 5v regulator, and the LT1117 series of regulators are only spec'd to be within 50mV of the nominal voltage. ADCs don't read voltage per se, they just measure fractions of the reference voltage, so there's a strong garbage-in/garbage-out factor.

To characterize the error, connect the Arduino's ADC to a voltage that remains stable, like an LED running from a wall-wart power supply. Collect a few thousand readings at whatever sampling rate you want to use, then graph them.

If you get nice, flat line, it means the error is just a DC offset. Calibrate the error by measuring the LED voltage with a multimeter, compare that to the millivolt equivalent of the ADC reading, and find the constant that makes the two equal. Then subtract that constant from your TMP36 readings before converting them to a temperature.

If the data samples move around rapidly, put a 10k resistor between the Arduino's 5v rail and its Aref pin, then drop a 10-100uF capacitor from the pin to GND. That will act as a filter to smooth out sudden changes in the reference voltage, which should make the readings more stable. Once the line is smooth enough for your purposes, figure the DC offset again.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”