#define THERMISTOR_PIN 35 // Use the appropriate analog pin on your ESP32
#define SERIES_RESISTOR 10000.0 // Value of the series resistor in ohms
#define THERMISTOR_RESISTANCE_ROOM_TEMP 10000.0 // Resistance of the NTC thermistor at room temperature in ohms
#define TEMPERATURE_ROOM_TEMP 25.0 // Room temperature in °C
#define B_COEFFICIENT 3950.0 // B-coefficient of the NTC thermistor
float adc_in, temp_c;
void setup()
{
Serial.begin(9600); // Set the baud rate
analogReadResolution(10); // Set ADC resolution to 10 bits
}
void loop()
{
// Create a table of 20 readings
for (int i = -24; i <= 80; i += 5) {
// Calculate the ADC value corresponding to the current temperature
int adc_value = calculateADCFromTemperature(i);
// Print the ADC out and temperature in Celsius
Serial.print("ADC Output: ");
Serial.print(adc_value);
Serial.print(" | Temperature (°C): ");
Serial.println(i);
delay(2000); // Delay for 2 seconds between readings
}
// End the program after collecting 20 readings
while (true) {
delay(1000);
}
}
int calculateADCFromTemperature(float temperature)
{
// Calculate the resistance of the NTC thermistor at the given temperature
float thermistor_resistance = THERMISTOR_RESISTANCE_ROOM_TEMP * exp(B_COEFFICIENT * (1.0 / (temperature + 273.15) - 1.0 / (TEMPERATURE_ROOM_TEMP + 273.15)));
// Calculate the ADC value using the voltage divider formula
int adc_value = (int)((SERIES_RESISTOR * 1023.0) / (SERIES_RESISTOR + thermistor_resistance));
Serial.print(" thermistor_resistance ");
Serial.println(thermistor_resistance);
return adc_value;
}