Project 3 - Problem with temperature measurement

Hi everyone, I am a beginner with Arduino and I'm new in the forum.
I am trying to get the correct Celsius Temperature, however I'm getting values over 100.

Sensor Value: 291 , Volts: 1.42 , Temp: 102.09
Sensor Value: 287 , Volts: 1.40 , Temp: 100.14
Sensor Value: 286 , Volts: 1.40 , Temp: 99.65
Sensor Value: 291 , Volts: 1.42 , Temp: 102.09

Below the code. Can someone explain me why?

Thanks,

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

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value

void setup() {
  // put your setup code here, to run once:
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  // calibrate during the first five seconds
  while (millis() < 7000) {
    sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }

  // signal the end of the calibration period
  digitalWrite(4, LOW);

  Serial.begin(9600);
  for(int pinNumber = 2; pinNumber<5; pinNumber++) {
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }

}

void loop() {
  // put your main code here, to run repeatedly:
  int sensorValue = analogRead(sensorPin);
  Serial.print("Sensor Value:  ");
  Serial.print(sensorValue);
  float voltage = (sensorValue/1024.0) * 5.0;
  Serial.print(" , Volts:  ");
  Serial.print(voltage);
  float temperature = (voltage - .4) * 100;
  Serial.print(" , Temp:  ");
  Serial.println(temperature);

}

Hi @andreacalcagni25 ,
These values you are getting are because of your formulas.
voltage = (sensorValue/1024.0) * 5.0;
and
temperature = (voltage - .4) * 100;
Where did you find these formulas?

With the readings 291, 287 and 286, which was the value you expected.

RV mineirin

What sensor?

The sensor is TMP36

HI RV,
thanks for the answer. I found these formulas both on the book as well as online.

By dividing sensor value by 1024 and multiplying by 5 I should get the voltage.
I'm subtracting - .5 and multiplying by 100 to convert from 10 mv per degree with 500 mV offset to degrees.

With those Sensor Values those are the temperature values I obtain.
I was expecting to get Celsius degrees close to 20° with that formula.

Here is the Adafruit tutorial on using the TMP36 sensor.

Hi GroundFungus,
thanks for the link. I tried using that code, but the values obtained are the same.
Maybe there is something wrong with the calibration?

Hi @andreacalcagni25 ,
The TMP36 measures temperatures from -40°C to +125°C.

At -40° the output is 100 mV, at 0° the output is 500 mV, at 25° the output is 750mV and at 125° the output is 1750 mV.

Assuming you are using an arduino UNO, the voltages will generate the following values.
at -40° -> 100 mV =~ 20, at 0° -> 500mV =~102, at 25° -> 750mV =~153 and 125° -> 1750 =~ 358.
The formulas entered in your sketch are correct, (except the value -.4 which for the TMP36 should be -.5, but this difference has very little effect on the temperature result).

These values

actually generate these temperatures.

What seems to me to be wrong are the values read: 291, 287, 286, 291....
Check that the sensor is connected correctly.

RV mineirin