TMP36 temperature sensor giving incorrect readings

Hi all,

I'm currently attempting to get a TMP36 analog temperature sensor working with my Mega 2560, but running into some trouble. I've tried using a couple of guides to help with my code, but they're all pretty similar, and I get the same results on the few slightly different code layouts I've used. This is the code I have currently:

int sensePin = A8; //This is the Arduino Pin that will read the sensor output
int sensorInput; //The variable we will use to store the sensor input
double temp; //The variable we will use to store temperature in degrees.

void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Start the Serial Port at 9600 baud (default)

}
void loop() {
// put your main code here, to run repeatedly:
sensorInput = analogRead(A8); //read the analog sensor and store it
temp = (double)sensorInput / 1024; //find percentage of input reading
temp = temp * 5; //multiply by 5V to get voltage
temp = temp - 0.5; //Subtract the offset
temp = temp * 100; //Convert to degrees

Serial.print("Current Temperature: ");
Serial.println(temp);
delay(500);
}

When I run the code, the serial monitor gives me wildly oscillating readouts - I'm aware the TMP36 is not an especially accurate sensor, but I'm not sure if its supposed to be quite this bad. On top of this is the fact that half the values are negative values, which clearly is not correct when I'm currently in a heated house. When I add code to print out the voltage, rather than the temperature, the voltage appears to be relatively stable, but I presume the small oscillations are being magnified by the conversion calculations.

On top of this, I have tried reading the voltage on the middle sensor pin, and this gives and even more believable readout (as it should). However, the voltage reading from both methods usually reads at between 0.3mV and 0.7mV, although it does not seem to oscillate between the two too rapidly, and I can notice a voltage increase/decrease when I manually warm the sensor. Clearly the 0.5V offset subtracted from the <0.5V sensor output is leading to the negative values.

I have the sensor connected to the A8 analog pin, with the two power pins connected to the GND and 5V supply on the double-width GPIO header of the Mega. I have tried the same calculations but with the 3.3V supply instead, but this has more or less the same result (the "temperature" values are fairly different to the ones using the 5V supply however, despite altering the code to account for it).

Basically, what I'm asking is, am I likely to have a faulty sensor? It certainly seems like it to me, but if possible I'd like to make sure that I haven't just made some silly mistake with my setup, before I go and order a new temperature sensor.

1 Like

Post a picture of your setup. I suspect intermittent connections.

You should be using a breadboard, and jumpers, considering the thin leads of the sensor. They won't make good contacts with the headers on the Arduino board. Or, solder extensions to the sensor leads.

1 Like

You might try a 0.1µF ceramic capacitor from A8 to GND to help with jitter.

void loop() {
// put your main code here, to run repeatedly:
sensorInput = analogRead(A8); //read the analog sensor and store it
temp = sensorInput * 500.0 / 1024 - 50; // adjust the "500.0" up or down to
                                        // calibrate with accurate thermometer
Serial.print("Current Temperature: ");
Serial.println(temp,1);
delay(500);
}

NOTE: I've had better results using the more stable 1.1 or 2.56 volt internal reference than the default Vcc (5.? volt).

I don't believe the Arduino ide supports doubles. You should change this to

temp = float sensorInput / 1024;

The Arduino IDE "supports" double. On 8 bit processors it defaults to be the same as float (32 bit). On 32 bit processors you may get an actual double (64 bit) implemented in software. Some 32 bit processors even have hardware support.


Been running on a breadboard the whole time, but I can try soldering the pins if you reckon that'll be better. The blue jumper lead is connected to the 5V pin.

I'll give this a go as well, but I'm not quite sure how I would wire this up sorry, I'm still quite new to Arduino. Would the TMP36 stay connected to the 5Vout pin or no?

Nothing wrong with the code you posted, nothing which would cause the symptoms you described. I loaded the code on a Mega 2560 just to make sure. Your wiring scheme also looks to be correct. My 2560 5.0 volts is actually 4.91. Externally simulating your sensor I get stable readings within 1.0 degree C.

Your symptoms would be inline with a loose or poor connection or a faulty sensor. Connect A8 to Ground omitting the sensor. You should see a stable -50.00 degrees C. If you do neither the Mega or your code is a problem.

Ron

Try moving the sensor to a different set of breadboard contacts, and replace all three wires.

Thanks for the replies all. Upon further testing I've found that that when A8 is plugged into GND it sits at mostly -50.00 C, with occasional dips to -49.90~. I found that this seems to be caused by the TFT display I have plugged into the main 5V power pin and GND, as when I unplugged this the A8 began giving a fully constant -50.00.

On top of this however, I noticed that when I unplug the TFT display the displayed temperature is settled at around 12-13 degrees, which is still far too low. When I plug it into the primary 5V pin however (the one usually used by the TFT) rather than one of double-header 5V pins the temperature is a relatively constant 21-22 (ambient) which seems correct. I presume that the double header voltage pins are not working correctly then and are instead running at a lower voltage?

So now I can make the sensor work by itself, but I need to use the TFT display as part of this project so I cannot really avoid the issue as such. Therefore, do you have any suggestions on how to circumvent these two issues (GND affected by TFT, and low voltage power pins) without disconnecting the TFT?

Sounds like the TFT display is overloading the 5V output and causing a voltage drop. Power it separately.

Apologies if I'm wrong, but I'm not so sure that's the issue, as when the TFT is unplugged the double header 5V output still seems to be low voltage, whereas if I use the primary 5V pin for the sensor it gives the correct voltage. When I switch the jumper leads around (TFT to double header, TMP36 to main 5V) the sensor outputs the temperature as roughly correct, while the TFT is also plugged in. I'm realising now that this works fine for my purposes, as the display appears to be able to run off the double header just fine.

However, there is still the matter of the GND seeming affected by the TFT being plugged in which in turn leads to the jittering in the reported temperature. Is there some other way I can ground the TFT to avoid this, or would using a capacitor (like JCA34F suggested) help just as well?

I have no idea what you mean by your last post. For example, what does this mean?

there is still the matter of the GND seeming affected

Please post a complete wiring diagram (hand drawn is best), with links to the components, and report voltages measured with respect to GND, using a decent multimeter.

To test the sensor, remove everything but the sensor, and use the simplest possible program to print the data on the serial monitor.

I disagree. Supply variations, like from blinking LEDs, do show up in the temp readout
You shouldn't read an TMP36 with default Aref (what most examples tell you to do).
Especially not when you have other users (display, etc.) connected to the 5volt supply.
If you do it right (switching to 1.1volt Aref), then you can have a stable readout within 0.2 C.

Try this simple sketch, with everything else removed
Leo..

const byte tempPin = A0; // connect TPM36 to 3.3volt A0 and (not-shared) ground
float calibration = 0.1039; // calibrate temp by changing the last digit(s) of "0.1039"
float tempC;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL1V1); // use the Mega's internal 1.1volt Aref
}

void loop() {
  tempC = (analogRead(tempPin) * calibration) - 50.0; // A/D to temp
  
  Serial.print("Temperature:  ");
  Serial.print(tempC, 1); // one decimal place is all you get
  Serial.print(" C");
  
  delay(1000); // use a non-blocking delay when combined with other code
}

Would you like a diagram of the entire Arduino (TFT included?) or just the sensor?

I've verified that the sensor definitely works fully as expected, as well as the code, when running independently.

The 5Vout pins on the double header appear to run at a lower voltage regardless of whether or not the TFT is plugged in, as even when the TFT is not connected the temperature reported by the sensor is 10 or so degrees too cold. Upon measurement they both read as 4.94V. All of the 5V pins drop to 4.78V when the TFT is powered on. Despite this however, editing the voltage value in the code to account for this does not solve the issue. Therefore I am left to use the primary 5Vout pin for the sensor, but this is not really an issue.

However, when both the TFT and sensor are connected to the Arduino, via any combination of power pins, I receive quite a lot of jitter in the temperatures printed in the serial monitor (up to -3 deg C with spikes up to -5 deg C) as opposed to the practically non-existent fluctuation that the sensor gives when connected on its own. I can't detect any sort of voltage jitter in any of the Vout pins with my multimeter while the TFT is powered on.

Weirdly enough though, if I power the TFT with the 3.3V header, then the sensor temperature increases by around 5 deg C from the correct temperature of 21-22, to 26-27 (without any alterations to the sensor). The jitter still remains however, as long as the TFT is still powered on.

I had assumed this jitter is related to the GND connection somehow, as when I connected A8 directly to GND and ran the code, when running independent it gives the constant -50.00 result as it should. However, when I carried out the same test with the TFT plugged in at the same time, there are some slight fluctuations from that exact -50.00, which I assume should not be happening. Ultimately though, I don't really have a clue. I have also tested using other analog pins (to no avail), so I assume that is also not the cause.

If you want to fix the problem caused by the TFT and/or the rest of the wiring, then it would make sense to post one, don't you think?

Plus links to the other gizmos attached.

I have experienced a very similar effect. I have a TMP36 connected to +5v, Gnd and A0 pins on a UNO, and am using analogRef (internal) and outputting to the serial monitor. With the monitor OFF, my DVM reads a steady 700mV +- 2mV measured A0 to Gnd, but if I open the serial monitor, the readings fluctuate by up to 50mV and are unstable. I apologise if this is not the correct place to post this, but it's the closest thread I can find.

1 Like

You can power a TMP36 with 3.3volt,
so you should power it from the much 'cleaner' unused regulated 3.3volt pin of the Uno.
As commented in the sketch I posted.

You must connect TMP36 ground directly to Arduino ground.
No breadboard ground rail, no shared ground with other sensors.
Sharing TMP36 ground with other devices can give ground-lift and thus inaccurate temps.
Leo..

Thanks for your reply and I now realise that whilst the TMP36 was the only thing connected to the UNO apart from the DVM, the leads were about 8" long and there could well have been some pick-up in the leads, it is in a very electrically noisy area.

Look here:

Don't blame the sensors.
Almost all tutorials have poorly written code, unshielded wires and breadboards.
And almost all chips we buy cheaply are Chinese counterfeit.
Leo..