Tuesday, September 27, 2016

Esp8266 NTC (Temperature) Thermistor Analog Reading with Arduino IDE

Hello Everyone,

In this lesson, i will show you reading temperature with an esp8266 and a ntc (thermister) sensor.

Note : 
You need an ESP board having ADC pin. Only ESP8266 - 07 and ESP8266 - 12 boards have ADC pin.

I used :
-ESP8266 - 07
-10k NTC
-150k ohm resistor.

We will use very simple and basic resistor-ntc circuit. Circuit is in figure1.


Figure-1

Normally, if you search on internet you will see circuits that uses 10k ntc with a 10k resistor. On my circuit i use 150k resistor. Now i will explain why i am doing it.

In figure-2, there is a graph that explaint resistance and temperature for a ntc sensor.
This sensor says NTC resistor high value is about 18-20k and 3-4k for (room temp).

Figure-2
If we use 10k resistor with 10k NTC,

Assume :
NTC max resistance = 18kohm
NTC min resistance = 3k ohm
VCC = 3.3V

 
Here is the problem. ESP8266 ADC pin Reference is 1V.
What is that mean?
That mean is your analog voltage highest value must be 1V. So, for that reason, i will use 150k resistor and wtih 150k resistor maximum and minumum analog values are below.


Theese values are good for our project. You can use a lower resistor for higher temperature resolution. In my project temperature resoulution is not very important.

ESP8266 Analog Resolution per Volts :

1V = 1024
1 / 1024 = 0.00097 V  resolution.

0.3535 V / 0.00097 V= 364
0.0647 V / 0.00097 V = 66

That means, we have 298 different temperature values. As i sad this is enough for my project.

Now we have solved hardware problem. But with this solution we have a problem with software. Functions that you can find on internet is for 10k ntc and 10k resistor and there is not enough explaniton about it.

Firstly,

We need to find instantenious resistance of NTC. You can use ESP8266 ADC pin as A0 on Arduino IDE.

Simple analog read code : 

int RawData = analogRead(A0);

Filtered analog read code : 


int AnalogRead() {
  int val = 0;
  for(int i = 0; i < 20; i++) {
    val += analogRead(A0);
    delay(1);
  }

  val = val / 20;
  return val;
}


In my project i use filtered analog read code. This is very effective and more stable. We have read analog voltage on NTC. Now we will calculate instantenious resistance of NTC.

void loop() {
  double Vcc = 3.3V;
  unsigned int Rs = 150000;
  double V_NTC = (double)AnalogRead()
  double R_NTC = (Rs * V_NTC) / (Vcc - V_NTC);  

  Serial.println(R_NTC);
}

We have successfuly completed first step. We have calculated resistance of NTC. You can check your paramters with your multimeter but you need to now that when you touch your ntc sensor, ntc resistance will change instantly.

Second step is about calculating temperature with using instantenious resistance of ntc .

The Steinhart–Hart equation is a model of the resistance of a semiconductor at different temperatures. The equation is:

1T=A+Bln(R)+C(ln(R))3
where:
  • Tis the temperature (in kelvins)
  • Ris the resistance at T(in ohms)
  • A, B, and C are the Steinhart–Hart coefficients which vary depending on the type and model of thermistor and the temperature range of interest. (The most general form of the applied equation contains a (ln(R))2
    • term, but this is frequently neglected because it is typically much smaller than the other coefficients, and is therefore not shown above.)
    Steinhart-Hart equation - Wikipedia, The Free Encyclopedia

    A = 0.001129148
    B = 0.000234125
    C = 0.0000000876741

    Now we are ready for calculate temperature.

    Where:
    Rs = 150000;  //150k ohm resistor
    Vcc = 3.3;

    double Thermister(int val) {

      double V_NTC = (double)val / 1024;
      double R_NTC = (Rs * V_NTC) / (Vcc - V_NTC);
      R_NTC = log(R_NTC);
      double Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * R_NTC * R_NTC ))* R_NTC );
      Temp = Temp - 273.15;        
      return Temp;


    }
Here is the complete project code .

#include <math.h>

unsigned int Rs = 150000;
double Vcc = 3.3;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:

  Serial.println(Thermister(AnalogRead()));
  delay(1000);

}

int AnalogRead() {
  int val = 0;
  for(int i = 0; i < 20; i++) {
    val += analogRead(A0);
    delay(1);
  }

  val = val / 20;
  return val;
}

double Thermister(int val) {
  double V_NTC = (double)val / 1024;
  double R_NTC = (Rs * V_NTC) / (Vcc - V_NTC);
  R_NTC = log(R_NTC);
  double Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * R_NTC * R_NTC ))* R_NTC );
  Temp = Temp - 273.15;         
  return Temp;

}






You can change Rs and Vcc paramters with your own values.


Note (Important !) : If you want to use different Rs, make sure your instantenious analog input voltage value is lower than 1V.

You can download project on that link.
Download here


Hope it will help you.
See you soon.






16 comments:

  1. You can read that article for analog readings filtering

    http://esp8266tutorials.blogspot.com.tr/2016/10/arduino-esp8266-digital-smoothing.html

    ReplyDelete
  2. I checked my esp8266-12 (NodeMCU) and it has correct reference voltage - 3.3V

    ReplyDelete
    Replies
    1. This tutorial not for NodeMCU or any other development boards. And thank you for interest

      Delete
  3. Hello,

    Your post help me a lot. The thermistor I'm using is MF52 b=3435 10k at 25°. Any idea how to find the 3 Steinhart-Hart A,B,C values for this thermistor? I'm not sure what information I need to get these. Thank you.

    ReplyDelete
  4. Hello,

    You can use this :

    http://www.thinksrs.com/downloads/programs/Therm%20Calc/NTCCalibrator/NTCcalculator.htm

    ReplyDelete
  5. hi my friend i try to use this code but have a problem.
    the termistore at -17,0 C have a value 73.84 C and resistor 1538

    ReplyDelete
  6. Hi,

    I am using EPCOS B57861S103F40 Thermistor 10kΩ, 2.41 (Dia.) x 6.5mm with the adafruit feather huzzah esp8266, will it work with that program?

    Thank you.

    anon

    ReplyDelete
    Replies
    1. Hello,

      Yes it should work with that. You can write here if you are having problems.

      Delete
    2. Thanks for your prompt reply. After uploading your code, the serial monitor output -13.00. No fluctuation in value too. Please help thank you

      Delete
    3. I solved the problem. awesome blog thank you!!

      Delete
  7. Hi,
    Your program works. However, I want telegram to notify me when the temperature is too high. Also, i can request for the temperature. How do i go about doing this? Hope to hear from you ASAP. Thank you.

    ReplyDelete
    Replies
    1. Hello, i am sorry because i havent work with Telegram before.

      Delete
  8. Anonymous, how did you solve your problem? I'm having the same issue using the Huzzah.

    ReplyDelete
  9. I have just gone through your blog......your information is so valuable. Im Aarudhra from Chennai Thermocouple Sensor

    ReplyDelete

ESP8266 SoftwareSerial Library & Level Shifter for ESP8266 - Arduino

Hello everyone, I have found a very usefull software library for esp8266. Library works well with Arduino IDE. In this tutorial, I will s...