TMP36 Temperature Sensor keeps getting inconstant readings

I have an Arduino UNO WiFi REV2 with a TMP36 temperature sensor which I have configured to connect to the network, and send an email once the temperature has reached a certain temperature in our IT room. Am able to get on the network and its able to send an email when the temperature reaches what I have set the limit to be. The challenge is that the reading are never constant, I have tried to be in different environments and I keep getting a reading that is not constant. Below is my code and the reading

#include <WiFiNINA.h>
#include <LCDIC2.h>

#define sensorPin A0

char ssid[] = "*******";
char pass[] = "";

int status = WL_IDLE_STATUS;
int pinDHT11 = 2;
LCDIC2 lcd(0x27, 16, 2);
byte Degree[] = {
  B00111,
  B00101,
  B00111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};
char server[] = "******";

String postData;
String postVariable = "Temperature=";

WiFiClient client;
void setup() {
  
Serial.begin(9600);

//analogReference(EXTERNAL);
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    
    delay(10000);
  }

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
if (lcd.begin());
  
}


void loop() {
 int reading = analogRead(sensorPin);  
 double voltage = reading * 0.004882814;
 double temperatureC = (voltage - 0.5) * 100.0;
 double temperatureF = (temperatureC * 1.8) + 32;

  postData = postVariable + temperatureC;

if (temperatureC >= 30){
  if (client.connect(server, 80)) {
    client.println("POST /arduino/emailScript.php/ HTTP/1.1");
    client.println("Host: *******");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(postData.length());
    client.println();
    client.print(postData);
    

    Serial.println("Email Sent");
    Serial.println(postData);
    Serial.println(" C");
  }

  if (client.connected()) {
    client.stop();
  }

  delay(60000);
}

  Serial.println(postData);
  
  lcd.print(postData);
 
  lcd.print("C");
 for (uint8_t i = 0; i < 15; i++) {
    lcd.setCursor(i, 1);
    
    delay(250);
  }
  for (uint8_t i = 0; i < 15; i--) {
    lcd.setCursor(i, 1);
    delay(250);
    
  }

  delay(3000);
  lcd.clear();
}

and the output is as below;
sometimes even in negative

Temperature=6.35
Temperature=34.21
Temperature=0.00
Temperature=37.63
Temperature=0.00
Temperature=38.61
Temperature=0.00
Temperature=34.21
Temperature=7.82
Temperature=31.28
Temperature=0.00
Temperature=35.68

Put
Serial.println(reading);
After analogRead .
That will tell whether the problem is in your sensor/wiring or in your sketch...
Also, when comparing to float you should use >= 30.0
In IDE press ctrl-t. Your indentation does not match your curly braces.
TemperatureC is not a String. Can you concatenate a String and a double this way? Maybe you should first convert the double to a String...

I have put that and its giving me the below

58

Temperature=-21.68

50

Temperature=-25.59

71

Temperature=-15.33

55

Temperature=-23.14

Hi, @MikoTukwi
Welcome to the forum.
Thanks for using code tags... :+1:

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Those analgRead values seem too low... can you send a schematic and a picture of your setup?
I presume you are not outside above 5000 m?
Or near the north or south pole...
...or in a fridge...


this is a more sketch description of what the connection is like

I cannot see it this way, but did you swap vcc and gnd?

I have swapped them to have one go on the other and these are the readings am getting

Attempting to connect to Network named: XXXXX
SSID: XXXXX
IP Address: 000000
96
Temperature=-3.12
58
Temperature=-21.68
8
Temperature=-46.09
64
Temperature=-18.75
47
Temperature=-27.05
10
Temperature=-45.12
0
Temperature=-50.00
9
Temperature=-45.61
1
Temperature=-49.51

Swapping the GND and 5V to the TMP36 is likely to destroy the temperature sensor.
(and don't use the excuse - i only did it for a short while).

1 Like

Hi,
Your code shows you have more than the 36 connected, can we have a COMPLETE schematic please?

Have you just got code that reads the 36 and NOTHING else, WITH NOTHING else connected?

If not, then please forget your current code for the moment and write some simple code to prove you are connected to the 36. Do this with all the other hardware disconnected as well.

This procedure should have been your first bit of code you wrote.

Also check this site out and the code supplied:

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

2 Likes

I have disconnected all hardware and I have used a new 36 not the one used before, below is the new diagram for your reference and the output (I have used the code from the page you gave me)


12:15:43.768 -> Temperature: -18.26°C | -0.87°F
12:15:44.770 -> Temperature: -18.26°C | -0.87°F
12:15:45.755 -> Temperature: -17.77°C | 0.01°F
12:15:46.766 -> Temperature: -17.77°C | 0.01°F
12:15:47.779 -> Temperature: -18.26°C | -0.87°F
12:15:48.780 -> Temperature: -18.75°C | -1.75°F
12:15:49.752 -> Temperature: -18.26°C | -0.87°F

TomGeorge,

Great tutorial - It taught me how to print a degree sign in the serial monitor.

Serial.print("\xC2\xB0");    

I have added the serial.println(reading); and here are the readings; they are constant but always negative

Temperature: -18.75°C | -1.75°F
64
Temperature: -18.75°C | -1.75°F
63
Temperature: -19.24°C | -2.63°F
63
Temperature: -19.24°C | -2.63°F
64
Temperature: -18.75°C | -1.75°F
64
Temperature: -18.75°C | -1.75°F
65
Temperature: -18.26°C | -0.87°F
65
Temperature: -18.26°C | -0.87°F

Just a stab in the dark here, but could the sensor be a TMP35 and not a TMP36?

The TMP35 covers a different temperature range, and does not have the 0.5V offset that the TMP36 has.
See the datasheet for further details.

You could try changing the following line:

 // Convert the voltage into the temperature in Celsius
  float temperatureC = (voltage - 0.5) * 100;

to:

 // Convert the voltage into the temperature in Celsius
  float temperatureC = (voltage) * 100;

On the picture it seems pretty anonymous...

What happens if you heat it by holding the plastic with your fingers?

Not a very big change. where as most of the videos I have watched shows the temperature change after its touched

I have tried that and these are the readings
Temperature: 31.74°C | 89.13°F
64
Temperature: 31.25°C | 88.25°F
64
Temperature: 31.25°C | 88.25°F
63
Temperature: 30.76°C | 87.37°F
65
Temperature: 31.74°C | 89.13°F
64
Temperature: 31.25°C | 88.25°F
64
Temperature: 31.25°C | 88.25°F
64
Temperature: 31.25°C | 88.25°F
64
Temperature: 31.25°C | 88.25°F
63
Temperature: 30.76°C | 87.37°F
65
Temperature: 31.74°C | 89.13°F
64
Temperature: 31.25°C | 88.25°F
64
Temperature: 31.25°C | 88.25°F

Surprisingly too high yet am doing these tests while am in the server room and its pretty cold

What happens if you remove temp sensor and connect 5V to A0?

If I remove the temp sensor and connect the 5v to 10 I get these readings
Temperature: -23.63°C | -10.54°F
390
Temperature: 140.43°C | 284.77°F
419
Temperature: 154.59°C | 310.26°F
378
Temperature: 134.57°C | 274.23°F
733
Temperature: 307.91°C | 586.24°F
657
Temperature: 270.80°C | 519.44°F
560
Temperature: 223.44°C | 434.19°F
514
Temperature: 200.98°C | 393.76°F
494
Temperature: 191.21°C | 376.18°F
374
Temperature: 132.62°C | 270.71°F
433
Temperature: 161.43°C | 322.57°F
419
Temperature: 154.59°C | 310.26°F
525