#include <Wire.h> 
#include <LiquidCrystal_I2C.h>


#define ntc_pin A0         // Pin, to which the voltage divider is connected
#define vd_power_pin 2        // 5V for the voltage divider
#define nominal_resistance 10000       //Nominal resistance at 25⁰C
#define nominal_temeprature 25   // temperature for nominal resistance (almost always 25⁰ C)
#define samplingrate 5    // Number of samples
#define beta 3950  // The beta coefficient or the B value of the thermistor (usually 3000-4000) check the datasheet for the accurate value.
#define Rref 10000   //Value of  resistor used for the voltage divider
int samples = 0;   //array to store the samples


#define BACKLIGHT_PIN     13

LiquidCrystal_I2C lcd(0x20,16,4);  // Set the LCD I2C address

void setup()
{

  // Switch on the backlight
  pinMode ( BACKLIGHT_PIN, OUTPUT );
  digitalWrite ( BACKLIGHT_PIN, HIGH );
  
  lcd.begin(16,4);               // initialize the lcd 
  lcd.home ();                   // go home
  lcd.print("Hello, ARDUINO ");  
  lcd.setCursor ( 0, 1 );        // go to the next line
  lcd.print (" FORUM - fm   ");
}


void setup(void) {
  // put your setup code here, to run once:
   pinMode(vd_power_pin,OUTPUT);
   Serial.begin(9600);   // initialize serial communication at a baud rate of 9600
}

void loop(void) {
  // put your main code here, to run repeatedly:
  //
  //  Temperature measurements
  uint8_t i;
  float average;
  samples = 0;
  
  // take voltage readings from the voltage divider
  digitalWrite(vd_power_pin,HIGH);
  for (i=0; i< samplingrate; i++) {
     samples += analogRead(ntc_pin);
     delay(10);
  }
  
  digitalWrite(vd_power_pin,LOW);
  average = 0;
  average = samples/ samplingrate;
  Serial.println("\n \n");
  Serial.print("ADC readings ");
  Serial.println(average);
  
  // Calculate NTC resistance
  average = 1023 / average - 1;
  average = Rref/ average;
  Serial.print("Thermistor resistance ");
  Serial.println(average);
  
  float temperature;
  float tempF;
  temperature = average / nominal_resistance;     // (R/Ro)
  temperature = log(temperature);                  // ln(R/Ro)
  temperature /= beta;                   // 1/B * ln(R/Ro)
  temperature += 1.0 / (nominal_temeprature + 273.15); // + (1/To)
  temperature = 1.0 / temperature;                 // Invert
  temperature -= 273.15;                         // convert absolute temp to C
  tempF = (temperature * 1.8) + 32;          // convert to Fahrenheit
  
  Serial.print("Temperature ");
  Serial.print(temperature);
  Serial.print(" *C:: ");
  Serial.print(tempF);
  Serial.println(" *F");

  delay(1000);
}