View Single Post
Old 01-03-16, 02:32 PM   #27
Acuario
Apprentice EcoRenovator
 
Join Date: May 2011
Location: Tortosa, Spain
Posts: 221
Thanks: 2
Thanked 81 Times in 46 Posts
Default

13. NTCThermistor.ino (still sort of in development/test)

///////////////////////////////////////////////////////////////////////////////////////////////////
// NTC Thermistor
///////////////////////////////////////////////////////////////////////////////////////////////////
#include <math.h>




// enumerating 3 major temperature scales
enum {
T_KELVIN = 0,
T_CELSIUS,
T_FAHRENHEIT
};

// manufacturer data for episco k164 10k thermistor
// simply delete this if you don't need it
// or use this idea to define your own thermistors
#define EPISCO_K164_10k 4300.0f,298.15f,10000.0f // B,T0,R0
#define NTCRES 3380.0f,298.15f,10000.0f

NIL_THREAD(threadReadNTC, arg) {
while (TRUE) {

//while (readAnalog == TRUE) {
// nilThdSleepMilliseconds(100);
//}
for (int s = 2; s < 6; s++) {
ntcSensor[s - 2] = int(Thermistor(analogRead(s)));
nilThdSleepMilliseconds(200);
// DEBUG_PRINTST("Sensor %d: %d\n", s, ntcSensor[s - 2]);
}
//for (int s = 0; s < 4; s++) {
// ntcSensor[s] = int(Thermistor(analogRead(s)));
// //ntcSensor[s] = int(Temperature(s, T_CELSIUS, NTCRES, 9850));
// //ntcSensor[s] = thermNTC(s);
// //ntcSensor[s] = tempTable(s);
// nilThdSleepMilliseconds(200);
//}

// Sleep for 10 seconds.
nilThdSleepSeconds(10);
}
}

// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10500

int samples[NUMSAMPLES];

float thermNTC(int pin) {
uint8_t i;
float average;

// take N samples in a row, with a slight delay
for (i = 0; i < NUMSAMPLES; i++) {
samples[i] = analogRead(pin);
delay(10);
}

// average all the samples out
average = 0;
for (i = 0; i < NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;

float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
return(steinhart);
}


// Temperature function outputs float , the actual
// temperature
// Temperature function inputs
// 1.AnalogInputNumber - analog input to read from
// 2.OuputUnit - output in celsius, kelvin or fahrenheit
// 3.Thermistor B parameter - found in datasheet
// 4.Manufacturer T0 parameter - found in datasheet (kelvin)
// 5. Manufacturer R0 parameter - found in datasheet (ohms)
// 6. Your balance resistor resistance in ohms

float Temperature(int AnalogInputNumber, int OutputUnit, float B, float T0, float R0, float R_Balance)
{
float R, T;

// R=1024.0f*R_Balance/float(analogRead(AnalogInputNumber)))-R_Balance;
R = R_Balance*(1024.0f / float(analogRead(AnalogInputNumber)) - 1);

T = 1.0f / (1.0f / T0 + (1.0f / B)*log(R / R0));

switch (OutputUnit) {
case T_CELSIUS:
T -= 273.15f;
break;
case T_FAHRENHEIT:
T = 9.0f*(T - 273.15f) / 5.0f + 32.0f;
break;
default:
break;
};

return T;
}


///////////////////////////
// Stuff for NTC thermistor
///////////////////////////
// 666 ohms = 100C
//double Thermistor(int RawADC) {
// double Temp;
// Temp = log(9850.0*((1024.0 / RawADC - 1)));
// Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp))* Temp);
// Temp = Temp - 273.15; // Convert Kelvin to Celcius
// return Temp - 18;
//}

double Thermistor(int RawADC) {
double Temp;
Temp = log(10000.0*((1024.0 / RawADC - 1)));
//Temp = log(10000.0 / (1024.0 / RawADC - 1)); // for pull-up configuration
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp))* Temp);
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}
Acuario is offline   Reply With Quote