Skip to content

Instantly share code, notes, and snippets.

@boverby
Last active February 4, 2018 17:16
Show Gist options
  • Save boverby/6ebd512a1f77edb76e266a5d38299261 to your computer and use it in GitHub Desktop.
Save boverby/6ebd512a1f77edb76e266a5d38299261 to your computer and use it in GitHub Desktop.
// experiment using inexpensive NTC thermistor on esp32
// 20180204 - original test
// small wemos board from FACE-TO-FACE [ esp32 Rev1 ]
// compares well to reference thermocouple in simple test
// built from https://learn.adafruit.com/thermistor/using-a-thermistor
// and https://esp32.com/viewtopic.php?f=12&t=1045
// and https://github.com/espressif/arduino-esp32/issues/92
// which analog pin to connect (using svp or gpio36)
#define THERMISTORPIN 36
// resistance at 25 degrees C
#define THERMISTORNOMINAL 100000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 50
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor (measured)
#define SERIESRESISTOR 125000
//
// circuit : Vcc -> SERIESRESISTOR -> (adc measure point) -> thermistor -> gnd
//
uint16_t samples[NUMSAMPLES];
void setup(void) {
Serial.begin(115200);
//analogReference(EXTERNAL);
// set adc to 6db
analogSetAttenuation(ADC_6db); // 11db=0-3.3v, 6db=0-2.2v, 2.5db=0-1.5v, 0db=0-1v
analogReadResolution(11); // 12=0->4095, 11=0->2047, 10=0->1024, 9=0->511
}
void loop(void) {
uint8_t i;
float average;
float resistance;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(25);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
Serial.print("Average analog ");
Serial.print(average);
// convert the value to resistance
// 4095 for 12-bits, 2047 for 11-bits, 1023 for 10-bits, 511 for 9 bits.
resistance = 2047 / average - 1;
resistance = SERIESRESISTOR / resistance;
Serial.print(" resistance ");
Serial.print( resistance );
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 to C
float farenheit = steinhart * 9 / 5 + 32;
Serial.print(" ");
Serial.print(steinhart);
Serial.print(" *C");
Serial.print(" ");
Serial.print(farenheit);
Serial.println(" *F");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment