Created
November 5, 2012 14:13
-
-
Save anonymous/4017373 to your computer and use it in GitHub Desktop.
Thermistor linearisation code snippet (Arduino etc)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//***************************************************************************************************** | |
// Thermistor Linearisation and temperature print-out code | |
// Thermistor Linearisation Code | |
double Thermistor(int RawADC) { | |
// Inputs ADC Value from Thermistor and outputs Temperature in Celsius | |
// requires: include <math.h> | |
// Utilizes the Steinhart-Hart Thermistor Equation: | |
// Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3} | |
// where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08 | |
long Resistance; | |
double Temp; // Dual-Purpose variable to save space. | |
Resistance=((10240000/RawADC) - 10000); // Assuming a 10k Thermistor. Calculation is actually: Resistance = (1024 * BalanceResistor/ADC) - BalanceResistor | |
Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later. // "Temp" means "Temporary" on this line. | |
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp)); // Now it means both "Temporary" and "Temperature" | |
Temp = Temp - 273.15; // Convert Kelvin to Celsius // Now it only means "Temperature" | |
// Uncomment this line for the function to return Fahrenheit instead. | |
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert to Fahrenheit | |
return Temp; // Return the Temperature | |
} | |
//***************************************************************************************************** | |
void printDouble(double val, byte precision) { | |
// prints val with number of decimal places determine by precision | |
// precision is a number from 0 to 6 indicating the desired decimal places | |
// example: printDouble(3.1415, 2); // prints 3.14 (two decimal places) | |
Serial.print (int(val)); //prints the int part | |
if( precision > 0) { | |
Serial.print("."); // print the decimal point | |
unsigned long frac, mult = 1; | |
byte padding = precision -1; | |
while(precision--) mult *=10; | |
if(val >= 0) frac = (val - int(val)) * mult; else frac = (int(val) - val) * mult; | |
unsigned long frac1 = frac; | |
while(frac1 /= 10) padding--; | |
while(padding--) Serial.print("0"); | |
Serial.print(frac,DEC) ; | |
} | |
} | |
//***************************************************************************************************** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment