The TMP36 temperature IC is a very cheap IC that can be used as a digital thermometer for all kinds of applications from measuring the temperature of your room to measuring the inner temperature your meat, and it costs under $2! This project uses the TMP36 IC as a simple, accurate, easy-to-build and cheap thermometer, that you can make in less than 5 minutes for only $7!!!
Using the TMP36 temperature IC with an ESP32 is subtly different than an Arduino, because but the formula in the datasheet does not apply to the ESP32, due to the way the ESP32 processes the digital signal. This difference is noted under the code, and how the code changes as a result of the ESP32's quirk!
There are very subtle differences in when coding a temperature sensor on the ESP32 board as opposed to the Arduino. For the ESP32 board, we do not need to multiply the raw sensor reading by the voltage, but simply needs to be normalized by the 2^10 bits.
const int tempPin = 2; //analog input pin constant<br>
int tempVal; // temperature sensor raw readings
float volts; // variable for storing voltage
float temp; // actual temperature variable
void setup() { // start the serial port at 9600 baud Serial.begin(9600); }
void loop() { //read the temp sensor and store it in tempVal tempVal = analogRead(tempPin);
volts = tempVal/1023.0; // normalize by the maximum temperature raw reading range
temp = (volts - 0.5) * 100 ; //calculate temperature celsius from voltage as per the equation found on the sensor spec sheet.
Serial.print(" Temperature is: "); // print out the following string to the serial monitor Serial.print(temp); // in the same line print the temperature Serial.println (" degrees C"); // still in the same line print degrees C, then go to next line.
delay(1000); // wait for 1 second or 1000 milliseconds before taking the next reading. }