Thermistor Interfacing with NodeMCU

The thermistor is a type of resistor whose resistance changes in accordance with the change in temperature. It is used to measure the temperature over a small range typically -100 °C to 300 °C.

49k views4 min readUpdated Apr 18, 2023
Thermistor Interfacing with NodeMCU

Overview of Thermistor

Thermistor
Thermistor

 

A thermistor is a variable resistance element; whose resistance varies with change in temperature. The change in resistance value is a measure of the temperature. Thermistors are classified as PTC (Positive Temperature Coefficient) or NTC (Negative Temperature Coefficient).

They can be used as current limiters, temperature sensors, overcurrent protectors, etc. For more information about the thermistor and how to use it, refer to the topic NTC Thermistor in the sensors and modules section.

 

Connection Diagram of NTC Thermistor with NodeMCU

Thermistor interface with NodeMCU
Interfacing Thermistor with NodeMCU

 

 

Measure Temperature using NTC Thermistor and NodeMCU

Measuring temperature using a thermistor.

 

Here, an NTC type thermistor of 10kΩ (thermistor resistance) is used. NTC of 10kΩ means that this thermistor has a resistance of 10kΩ at 25°C. The voltage across the 10kΩ resistor is given to the ADC of NodeMCU.

 

We can calculate the voltage across a series of 10kΩ resistor (R2 in the above figure) as,

Vout = VCC * ADC_Value / ADC_Resolution

Where Vout is the voltage measured by the ADC of NodeMCU

The temperature can be found out from thermistor resistance using the Steinhart-Hart equation.

Temperature(in kelvin) = 1 / (A + B[ln(Rth)] + C[ln(Rth)]^3)

where, A = 0.001129148,

B = 0.000234125,

C = 8.76741*10^-8 and

Rth is the thermistor resistance.

The thermistor resistance (Rth) can be found out using a simple voltage divider network formula.

Rth + 10k = VCC * 10k / Vout

Where Rth is the thermal resistance

 

NodeMCU ADC can be used to measure the analog voltage across the 10kΩ resistor. To know about ADC of NodeMCU refer to NodeMCU ADC with ESPlorer IDE and NodeMCU ADC with Arduino IDE.

 

We can write codes for NodeMCU Dev Kit in either Lua Script or C/C++ language. We are using ESPlorer IDE for writing Lua scripts and Arduino IDE for writing code in C/C++. To know more refer to Getting started with NodeMCU using ESPlorer IDE (which uses Lua scripting for NodeMCU) and Getting started with NodeMCU using Arduino IDE (which uses C language-based Arduino sketches for NodeMCU).

 

Let’s write Lua script for NodeMCU to measure temperature using a thermistor.

Lua Script for thermistor

VCC = 3.3   -- NodeMCU on board 3.3v vcc
R2 = 10000  -- 10k ohm series resistor
adc_resolution = 1023 -- 10-bit adc

-- thermistor equation parameters
A = 0.001129148 
B = 0.000234125
C = 8.76741*10^-8 

function ln(x)      --natural logarithm function for x>0 real values
    local y = (x-1)/(x+1)
    local sum = 1 
    local val = 1
    if(x == nil) then
        return 0
    end
-- we are using limited iterations to acquire reliable accuracy.
-- here its upto 10000 and increased by 2
    for i = 3, 10000, 2 do
        val = val*(y*y)
        sum = sum + (val/i)
    end
    return 2*y*sum
end

while true do
    local Vout, Rth, temperature
    local adc_value = adc.read(0)

    Vout = (adc_value * VCC) / adc_resolution
    Rth = (VCC * R2 / Vout) - R2
    temperature = (1 / (A + (B * ln(Rth)) + (C * (ln(Rth))^3)))   -- Temperature in kelvin
    temperature = temperature - 273.15  -- Temperature in degree celsius
    print(string.format("Temperature = %0.3g °C",temperature))
    tmr.delay(100000)
end

 

ESPlorer Serial Output Window

ESPlorer Serial monitor output window for temperature using thermistor

ESPlorer Serial Output Window

 

Now let’s write a program of the thermistor for NodeMCU using Arduino IDE

NTC thermistor Code for NodeMCU using Arduino IDE

const double VCC = 3.3;             // NodeMCU on board 3.3v vcc
const double R2 = 10000;            // 10k ohm series resistor
const double adc_resolution = 1023; // 10-bit adc

const double A = 0.001129148;   // thermistor equation parameters
const double B = 0.000234125;
const double C = 0.0000000876741; 

void setup() {
  Serial.begin(9600);  /* Define baud rate for serial communication */
}

void loop() {
  double Vout, Rth, temperature, adc_value; 

  adc_value = analogRead(A0);
  Vout = (adc_value * VCC) / adc_resolution;
  Rth = (VCC * R2 / Vout) - R2;

/*  Steinhart-Hart Thermistor Equation:
 *  Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)
 *  where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8  */
  temperature = (1 / (A + (B * log(Rth)) + (C * pow((log(Rth)),3))));   // Temperature in kelvin

  temperature = temperature - 273.15;  // Temperature in degree celsius
  Serial.print("Temperature = ");
  Serial.print(temperature);
  Serial.println(" degree celsius");
  delay(500);
}

 

Arduino Serial Output Window

Arduino Serial monitor output window for temperature using thermistor

Arduino Serial Output Window

 

Video of Temperature Measurement using NTC Thermistor and NodemCU

Components Used

Powered byMouser Electronics
  • Thermistor

    Thermistor is a type of resistor whose resistance changes in accordance with change in temperature. It is used to measure the temperature over small range typically -100 °C to 300 °C.

    474-SEN-00250

  • NodeMCU

    NodeMCU

    × 1

    NodeMCUNodeMCU

    713-113990105

  • ESP12F

    ESP12F

    × 1

    ESP12E

    485-2491

Downloads

Comments3

Join the discussion — share a question or tip.

  • BR
    BrunoDuarte

    On the NodeMCU pcb there are some resistors already, are you considering that ? https://circuits4you.com/wp-content/uploads/2017/12/nodemcu-circuit-diagram.png

  • LU
    LuberthDijkman

    do more 200 measurements and divide by 200 for more stable temp output https://youtu.be/lny7J7KXk9w //NTC Thermistor Interfacing with NodeMCU => WIFI remote temperature sensor //like to use easy to program usb devices nodemcu 12E ESP8266 development board //my ART thermostat is placed above my wood fire place //verry bad place for temperature measuring //solution: wifi remote temperature sensor //https://www.electronicwings.com/nodemcu/thermistor-interfacing-with-nodemcu // pinout image //https://www.electronicwings.com/public/images/user_images/images/NodeMCU/NodeMCU%20Interfaces/NodeMCU%20Thermistor/NodeMCU_Thermistor_Interfacing_Diagram.png //put this code in a webserver example? //ky-013 NTC thermistor //signal on A0 of nodemcu 12E development pcb // 3v3 3.3volt on -min/ground ky-013 //use 3.3V inputs nodemcu +3.3v tollerant "NOT +5v" //0 -min ground on +plus of ky013 // //text on ky-013 ntc thermistor is wrong +=- -=+ (resistor is placed pullup should be pulldown) // //https://www.google.com/search?q=ky-013 //https://www.google.com/search?q=nodemcu+12E&oq=nodemcu+12E //luberth tested and works on nodemcu temperature output in terminal //nodemcu development board arduino code //https://www.electronicwings.com/nodemcu/thermistor-interfacing-with-nodemcu //https://www.electronicwings.com/public/images/user_images/images/NodeMCU/NodeMCU%20Interfaces/NodeMCU%20Thermistor/NodeMCU_Thermistor_Interfacing_Diagram.png const double VCC = 3.3; // NodeMCU on board 3.3v vcc const double R2 = 10000; // 10k ohm series resistor const double adc_resolution = 1023; // 10-bit adc double ntc_analog_value; const double A = 0.001129148; // thermistor equation parameters const double B = 0.000234125; const double C = 0.0000000876741; void setup() { Serial.begin(9600); /* Define baud rate for serial communication */ } void loop() { double Vout, Rth, temperature, adc_value; ntc_analog_value = 0; int numberoffmeasurements = 200; // do 100 readings for more stable measurement for (int i = 0; i < numberoffmeasurements; i++) { // < = smaller i++ = i=i+1 ntc_analog_value = ntc_analog_value + analogRead(A0); delay(1); } adc_value= ntc_analog_value / numberoffmeasurements; // divide by 100 for more stable measurement //temp not jumping up and down Vout = (adc_value * VCC) / adc_resolution; Rth = (VCC * R2 / Vout) - R2; /* Steinhart-Hart Thermistor Equation: * Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3) * where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8 */ temperature = (1 / (A + (B * log(Rth)) + (C * pow((log(Rth)),3)))); // Temperature in kelvin temperature = temperature - 273.15; // Temperature in degree celsius Serial.print("Temperature = "); Serial.print(temperature,1); //1 digit behind dot Serial.println(" degree celsius"); delay(500); }

  • LU
    LuberthDijkman· edited

    Nice wireless temp for my ART Thermostat https://youtu.be/lny7J7KXk9w