Inaccurate TMP readings

Hi.

I am new to Arduino and is working through the project book that comes with the starter kit.

I am currently working on the Love o Meter but cant understand the readings i get.

At first it shows a temperature around 22C, which seems reasonable. But when i touch the sensor it skyrocks in a matter of a few seconds to around 60C. Even if i remove my finger from the sensor, the temperature still linger around 60C without going down again.

I have looked over the wiring and the code multiple times, but i cant figure it out.

So i would really appreciate some help with this.

Here is the code for the voltage and temperature:

float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);

float temperature = (voltage - .5) * 100;
Serial.print(temperature);
Serial.println(" degrees C");

Thank you in advance!

That is not the entire code. What is sensorVal and where does it come from? Schematics missing. And my chrystal ball is not up and running.

Railroader:
That is not the entire code.

Here is all of it:

const int sensorPin = A0;
const float baselineTemp = 24.22;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600); // open a serial port

for(int pinNumber = 2; pinNumber<5; pinNumber++){
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);

}
}

void loop() {
// put your main code here, to run repeatedly:

int sensorVal = analogRead(sensorPin);

Serial.print("Sensor Value: ");
Serial.print(sensorVal);

// converts the ADC readin to voltage
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);

Serial.print(", degrees C: “);
// convert the voltage to temperature in degrees
float temperature = (voltage - .5) * 100;
Serial.print(temperature);
Serial.println(” degrees C");

if(temperature < baselineTemp){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);

}else if(temperature >= baselineTemp+2 &&
temperature < baselineTemp+4){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);

}else if(temperature >=baselineTemp+4 &&
temperature < baselineTemp+6){
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4, LOW);

}else if(temperature >= baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);

}
delay(5);

60 degrees looks like a sensorVal of some 225. Can You confirm that? Is the sensor mounted in a breadboard? Touching it will in that case move sensor and possibly make affekt the electrical contact with the sensor. Toching a temperature sensor makes the value raise quickly but the recovery, back to room temperature goes rather slowly. However 60 degrees must be wrong some way. Is the formula correct, transforming sensor value to degrees? I don't see any obvious error in the rest of the code.
Attach the electrical diagram. I don't have the starter kit.

Yes, you are correct regarding the sensorval being in the area of 225. The sensor is mounted directly on the breadboard.

Today when i tried it with the same code, it was around 20C until touched, then it raised in about 3 second up to 26C before jumping to around 70C. After letting go of the sensor it went down to 55-60C and remained there again. So that is a bit different from what i experienced yesterday without doing any changes. Maybe could be the sensor itself that has some issues?

I have attached the diagram, taken from the book.

Thanks

@OP

1. You are using TMP36 Temperature Sensor with 10 mV/0C scale factor.

2. The response equation is (with default ananlogReference of 5V) is:

tempC (in [sup]0[/sup]C) = 100*(5/1024)*analogRead(A0); //assume sensor output is connected with A0.

3. In your post, you have shown the response as:

  const int sensorPin = A0;
  int sensorVal = analogRead(sensorPin);

  float voltage = (sensorVal/1024.0) * 5.0;
  float temperature = (voltage - .5) * 100;
  ==> float temperature = ((sensorVal/1024.0) * 5.0 - 0.5)*100
  ==> float temperature = 100*((5.0/1024)*sensorVal) - 0.5)  
  ==> float temperature = 100*((5.0/1024)*analog(A0)) - 0.5)

4. The response Eqn. of Step-3 does not agree with response Eqn. of Step-2. There is an extra factor (-0.5) in the Eqn. of Step-3. From where has it come?

Have You checked and rechecked the advice given in the picture at point (3) regarding the sensor? Sensor not turned 180 degrees? A desperate suggestion from me.

Railroader:
Have You checked and rechecked the advice given in the picture at point (3) regarding the sensor? Sensor not turned 180 degrees? A desperate suggestion from me.

Why are you not mentioning that the response equation that he is using is doubtful?

You already did that.