#include <WiFi.h>
#define THERMISTORNOMINAL 10000
#define TEMPERATURENOMINAL 25
#define BCOEFFICIENT 3950
#define SERIESRESISTOR 10000
#define ANALOG_PIN 35
const float TEMPERATURENOMINAL_DOUBLE = TEMPERATURENOMINAL * 2;
void setup() {
Serial.begin(115200);
analogReadResolution(12); // ESP32 uses 12-bit ADC
wifi_setup();
// Print header for debugging
Serial.println("ESP32 Thermistor Test");
Serial.println("Raw ADC | Voltage | Resistance | Temperature");
}
void loop() {
// Take multiple readings and average
int sum = 0;
for(int i = 0; i < 10; i++) {
sum += analogRead(ANALOG_PIN);
delay(10);
}
int reading = sum / 10;
// Convert ADC to voltage
float voltage = (float)reading * 3.3 / 4095.0;
// Calculate resistance of thermistor
float resistance;
if(voltage != 0) {
resistance = SERIESRESISTOR * ((3.3 / voltage) - 1.0);
} else {
resistance = 999999;
}
// Convert to temperature
float steinhart;
steinhart = resistance / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // Convert absolute temp to C
steinhart *= -1;
steinhart += TEMPERATURENOMINAL_DOUBLE;
// Print all values for debugging
Serial.print(reading);
Serial.print(" | ");
Serial.print(voltage, 3);
Serial.print("V | ");
Serial.print(resistance);
Serial.print("Ω | ");
Serial.print(steinhart);
Serial.println("°C");
delay(5000);
}
void wifi_setup() {
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}