// Pin configuration
const int analogPin = 33; // ESP32 pin where thermistor is connected

// Thermistor parameters
const float nominalResistance = 10000; // Resistance of the thermistor at 25°C (in ohms)
const float nominalTemperature = 25;  // Temperature for nominal resistance (in °C)
const float bCoefficient = 3950;      // Beta coefficient (provided by the thermistor's datasheet)
const float seriesResistor = 10000;   // Value of the series resistor (in ohms)

// ADC parameters
float vref = 3.3;                     // ESP32 reference voltage
int adcMaxValue = 4095;               // 12-bit ADC (values from 0 to 4095)

void setup() {
  Serial.begin(115200);               // Initialize Serial Monitor at 115200 baud
}

void loop() {
  // Read the analog input
  int analogValue = analogRead(analogPin);
  
  // Convert the ADC value to a voltage
  float voltage = analogValue * (vref / adcMaxValue);
  
  // Calculate the thermistor resistance
  float thermistorResistance = (seriesResistor * (vref / voltage)) - seriesResistor;
// Apply the Steinhart-Hart equation to calculate the temperature
  float steinhart;
  steinhart = thermistorResistance / nominalResistance;     // R/Ro
  steinhart = log(steinhart);                               // ln(R/Ro)
  steinhart /= bCoefficient;                                // 1/B * ln(R/Ro)
  steinhart += 1.0 / (nominalTemperature + 273.15);         // + (1/To)
  steinhart = 1.0 / steinhart;                              // Invert the result
  steinhart -= 273.15;                                      // Convert from Kelvin to Celsius

  
  // Print the temperature to the Serial Monitor
  Serial.print("Temperature: ");
  Serial.println(steinhart);
  Serial.println(" °C");

  delay(1000); // Wait 1 second before the next reading
}
Loading
esp32-devkit-c-v4
ntc1:GND
ntc1:VCC
ntc1:OUT
r1:1
r1:2