// Measure Temperature using NTC Thermistor by Solarduino
// Note Summary
// Note : Safety is very important when dealing with electricity. We take no responsibilities while you do it at your own risk.
// Note : This temperature measurement code is for NTC Thermistor use.
// Note : The value shown in Serial Monitor / LCD Display is refreshed every second and is the average value of 1000 sample readings.
// Note : No calibration is needed. However, if your sensor is not accurate, you can adjust or add in your callibartion code
// Note : The unit provides reasonable accuracy and may not be comparable with other expensive branded and commercial product.
// Note : All credit shall be given to Solarduino.
// Temperature (ºC) kΩ Ω RAW
// -40 154.3 154300
// -35 111.7 111700
// -30 81.7 81700
// -25 60.4 60400
// -20 45.1 45100
// -15 33.95 33950
// -10 25.8 25800
// -5 19.77 19770
// 0 15.28 15280
// 5 11.9 11900
// 10 9.33 9330
// 15 7.37 7370
// 20 5.87 5870
// 25 4.7 4700 327
// 30 3.79 3790
// 35 3.07 3070
// 40 2.51 2510
// 45 2.055 2055
// 50 1.696 1696
// 55 1.405 1405
// 60 1.17 1170
// 65 0.98 980
// 70 0.824 824
// 75 0.696 696
// 80 0.59 590
// 85 0.503 503
// 90 0.43 430
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/////////////*/
/* 0- General */
int decimalPrecision = 1; // decimal places for all values shown in LED Display & Serial Monitor
/* 1- Temperature Measurement */
int ThermistorPin = A1; // The analog input pin for measuring temperature
float voltageDividerR1 = 10000; // Resistor value in R1 for voltage devider method
float BValue = 3470; // The B Value of the thermistor for the temperature measuring range
float R1 = 4700; // Thermistor resistor rating at based temperature (25 degree celcius)
float T1 = 298.15; /* Base temperature T1 in Kelvin (default should be at 25 degree)*/
float R2 ; /* Resistance of Thermistor (in ohm) at Measuring Temperature*/
float T2 ; /* Measurement temperature T2 in Kelvin */
float a ; /* Use for calculation in Temperature*/
float b ; /* Use for calculation in Temperature*/
float c ; /* Use for calculation in Temperature*/
float d ; /* Use for calculation in Temperature*/
float e = 2.718281828 ; /* the value of e use for calculation in Temperature*/
float tempSampleRead = 0; /* to read the value of a sample including currentOffset1 value*/
float tempLastSample = 0; /* to count time for each sample. Technically 1 milli second 1 sample is taken */
float tempSampleSum = 0; /* accumulation of sample readings */
float tempSampleCount = 0; /* to count number of sample. */
float tempMean ; /* to calculate the average value from all samples, in analog values*/
/* 2 - LCD Display */
#include <LiquidCrystal_I2C.h> /* I2C display */
#define I2C_ADDR 0x27 /* I2C display */
#define LCD_COLUMNS 20 /* I2C display */
#define LCD_LINES 4 /* I2C display */
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES); /* I2C display */
//#include<LiquidCrystal.h> /* Load the liquid Crystal Library (by default already built-it with arduino solftware)*/
//LiquidCrystal LCD(8,9,4,5,6,7); /* Creating the LiquidCrystal object named LCD. The pin may be varies based on LCD module that you use*/
unsigned long startMillisLCD; /* start counting time for LCD Display */
unsigned long currentMillisLCD; /* current counting time for LCD Display */
const unsigned long periodLCD = 500; // refresh every X seconds (in seconds) in LED Display. Default 1000 = 1 second
void setup()
{
/* 0- General */
Serial.begin(9600);
/* 2 - LCD Display */
lcd.init(); /* I2C display */
lcd.backlight(); /* I2C display */
// LCD.begin(16,2); /* Tell Arduino that our LCD has 16 columns and 2 rows*/
// LCD.setCursor(0,0); /* Set LCD to start with upper left corner of display*/
startMillisLCD = millis(); /* Start counting time for LCD display*/
}
void loop()
{
/* 1- Temperature Measurement */
if(millis() >= tempLastSample + 1) /* every 1 milli second taking 1 reading */
{
tempSampleRead = analogRead(ThermistorPin); /* read analog value from sensor */
tempSampleSum = tempSampleSum+tempSampleRead; /* add all analog value for averaging later*/
tempSampleCount = tempSampleCount+1; /* keep counting the sample quantity*/
tempLastSample = millis(); /* reset the time in order to repeat the loop again*/
}
if(tempSampleCount == 500) /* after 1000 sample readings taken*/
{
tempMean = tempSampleSum / tempSampleCount; /* find the average analog value from those data*/
R2 = (voltageDividerR1*tempMean)/(1023-tempMean); /* convert the average analog value to resistance value*/
a = 1/T1; /* use for calculation */
b = log10(R1/R2); /* use for calculation */
c = b / log10(e); /* use for calculation */
d = c / BValue ; /* use for calculation */
T2 = 1 / (a - d); /* the measured temperature value based on calculation (in Kelvin) */
Serial.print(T2 - 273.15,decimalPrecision); /* display in Serial monitor the temperature in Celcius*/
Serial.println(" °C");
tempSampleSum = 0; /* reset all the total analog value back to 0 for the next count */
tempSampleCount = 0; /* reset the total number of samples taken back to 0 for the next count*/
}
/* 2 - LCD Display */
currentMillisLCD = millis(); /* Set counting time for LCD Display*/
if (currentMillisLCD - startMillisLCD >= periodLCD) /* for every x seconds, run the codes below*/
{
lcd.setCursor(0,0); /* Set cursor to first colum 0 and second row 1 */
lcd.print("T=");
lcd.print(T2 - 273.15,decimalPrecision); /* display current value in LCD in first row */
lcd.print(char(223)); /* print the symbol "degree"*/
lcd.print("C ");
lcd.setCursor(0,1);
lcd.print("RAW=");
lcd.print(tempSampleRead,0);
lcd.print(" "); /* display nothing in LCD in second row */
startMillisLCD = currentMillisLCD ; /* Set the starting point again for next counting time */
}
}