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.






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...