// For: https://forum.arduino.cc/t/temperature-sensor-always-giving-127-00-c-and-196-60-f-as-output/1011429/
//
// Version 1, 11 July 2022, by Koepel, Public Domain
//
//
// Based on:
// https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
// For the pins. Pin 34 has no special power-on conditions,
// and it is still avaible when the Wifi is used.
//
// https://en.wikipedia.org/wiki/Thermistor
// For the simplified "Beta" formula.
// NTC
const int ntcPin = 34; // A good pin for analog input
const double Beta = 3950; // I think the Wokwi NTC has this Beta
const double Tnul = 298.15; // 25°C = 298.15K, the reference temperature for the NTC
const double Rnul = 10000; // a 10k NTC
const double Rresistor = 10000; // a 10k resistor to 3.3V
void setup()
{
Serial.begin(115200);
}
void loop()
{
int rawADC = analogRead( ntcPin); // 12 bit
// Warning:
// This calculation uses 3.3 for the full range voltage.
// Do not change the 3.3 of the formula if the ESP32 runs at a other voltage.
double voltage = (double) rawADC * 3.3 / 4096; // 12 bit, range is 3.3V
Serial.print( "Input = ");
Serial.print( voltage);
Serial.print( "V, ");
// Calculate the resistance of the NTC
double R = (voltage * Rresistor) / (3.3 - voltage);
Serial.print( "NTC = ");
Serial.print( R);
Serial.print( "Ω, ");
double x = Rnul * exp( -Beta / Tnul);
double kelvin = Beta / (log(R/x));
double celsius = kelvin - 273.15;
Serial.print( "Temperature = ");
Serial.print( celsius);
Serial.print( "°C");
Serial.println();
delay( 1000);
}