I an using a Seeed Studio ESP32-C3 for a thermistor

I an using a Seeed Studio ESP32-C3 for a thermistor to monitor the temperature of a battery pack. The code runs however it just displays constant temps in the 130s F. Can someone out here help me figure out how to get this code to work. Thank you for any and all assistance!


/*
 * FINAL CALIBRATED CODE FOR SEEED ESP32-C3
 * Wiring: 3.3V -> 10k Resistor -> Pin A0 -> Thermistor -> GND
 */

const int sensorPin = 0;          // A0 (GPIO 0)
const float R_FIXED = 10000.0;    // 10k fixed resistor
const float R_NOMINAL = 10000.0;  // 10k thermistor
const float T_NOMINAL = 25.0;     // 25C
const float B_COEF = 3950.0;      // Beta coefficient

void setup() {
  Serial.begin(115200);
  analogReadResolution(12);
  analogSetAttenuation(ADC_11db); 
}

void loop() {
  // Take 10 readings and average them for stability
  long readingSum = 0;
  for (int i = 0; i < 10; i++) {
    readingSum += analogRead(sensorPin);
    delay(10);
  }
  float rawADC = readingSum / 10.0;

  if (rawADC > 0 && rawADC < 4095) {
    // THIS IS THE CORRECT FORMULA FOR RAW 930 -> ROOM TEMP
    // Resistance = R_fixed / ( (4095 / ADC) - 1 )
    float resistance = R_FIXED / ((4095.0 / rawADC) - 1.0);

    // Steinhart-Hart Equation
    float steinhart;
    steinhart = resistance / R_NOMINAL;     
    steinhart = log(steinhart);             
    steinhart /= B_COEF;                   
    steinhart += 1.0 / (T_NOMINAL + 273.15); 
    steinhart = 1.0 / steinhart;            
    
    float tempC = steinhart - 273.15;
    float tempF = (tempC * 9.0 / 5.0) + 32.0;

    Serial.print("Raw ADC: "); Serial.print(rawADC);
    Serial.print(" | Resistance: "); Serial.print(resistance);
    Serial.print(" | Temp: "); Serial.print(tempF);
    Serial.println(" F");
  } else {
    Serial.println("Check Wiring: ADC out of range");
  }

  delay(1000);
}```

If I'm not mistaken D0 is a strapping pin. Could be an issue or not in this case.
2. If you leave the thermistor constantly powered it will read a higher than normal temperature. It needs to be pulsed on and off at intervals.
3. I did quickly put your code into Gemini and it recommended changing a few things. Didn't really look into that much but I do know you need to pulse that thermistor for an accurate reading

Thats interesting. Ive used a couple different pins with the same result. Im not sure how to not have the thermistor constantly powered.

Hook it up to a digital output pin should give you what you need. It's how some power tool batteries measure internal temps

Do you mean a XIAO ESP32-C3 like this?

If yes, then use pin D1 and in your code set sensor pin to 3 or A1

1 Like

Yes that is the board I will try your changes

You seem to have some other board selected?

And what should it display? What it displays if you place a freezer pack on top of it?
Are you sure about the beta coefficient?

The ESP32 has the wrong A/D for that. Should work, but don't expect stable readings.

Thermistor code has two parts.

  1. calculating the resistor of the thermistor.
  2. converting the found resistance to temperature.

You could test the first part with a serial.print just below the "float resistance" line.
See if the resistor makes sense (10,000 Ohm at 25C).

long readingSum Just use int here, which is already 4 bytes on this platform.
Leo..

You were correct it is now working thank you.

You are welcome
Have a nice day