Thermistor Code


i found code using 10k thermistor in playground (link here).  it contains 2 sketches:  one short sketch runs calculations , outputs temperature serial monitor, , 1 that's longer, , think print more information serial monitor along more (mostly useless far can tell) calculations.

also on page, author mentions 5v rail 4.86 volts, has adjusted code work him.  my question cannot see anywhere in first (small) code used 4.86 volts;  in fact, can't see used 5v...

ps:  is possible voltage needed (assumed) pointless calculations?

i think see in longer script, if point out that'd dandy.  here 2 scripts:

short script
code: [select]
#include <math.h>

double thermister(int rawadc) {
double temp;
temp = log(((10240000/rawadc) - 10000));
temp = 1 / (0.001129148 + (0.000234125 * temp) + (0.0000000876741 * temp * temp * temp));
temp = temp - 273.15;            // convert kelvin celcius
temp = (temp * 9.0)/ 5.0 + 32.0; // convert celcius fahrenheit
return temp;
}

void setup() {
serial.begin(115200);
}

void loop() {
serial.println(int(thermister(analogread(0))));  // display fahrenheit
delay(100);
}


long script
code: [select]
#include <math.h>
//schematic:
// [ground] ---- [10k-resister] -------|------- [thermistor] ---- [+5v]
//                                     |
//                                analog pin 0

double thermistor(int rawadc) {
// inputs adc value thermistor , outputs temperature in celsius
//  requires: include <math.h>
// utilizes steinhart-hart thermistor equation:
//    temperature in kelvin = 1 / {a + b[ln(r)] + c[ln(r)]^3}
//    where = 0.001129148, b = 0.000234125 , c = 8.76741e-08
long resistance;  double temp;  // dual-purpose variable save space.
resistance=((10240000/rawadc) - 10000);  // assuming 10k thermistor.  calculation actually: resistance = (1024/adc)
temp = log(resistance); // saving log(resistance) not calculate 4 times later. // "temp" means "temporary" on line.
temp = 1 / (0.001129148 + (0.000234125 * temp) + (0.0000000876741 * temp * temp * temp));   // means both "temporary" , "temperature"
temp = temp - 273.15;  // convert kelvin celsius                                         // means "temperature"

// begin- remove these lines function not display anything
 serial.print("adc: "); serial.print(rawadc); serial.print("/1024");  // print out raw adc number
 serial.print(", volts: "); printdouble((([glow]rawadc*4.860[/glow])/1024.0),3);   // 4.860 volts usb port outputs.
 serial.print(", resistance: "); serial.print(resistance); serial.print("ohms");
// end- remove these lines function not display anything

// uncomment line function return fahrenheit instead.
//temp = (temp * 9.0)/ 5.0 + 32.0; // convert fahrenheit
return temp;  // return temperature
}

void printdouble(double val, byte precision) {
 // prints val number of decimal places determine precision
 // precision number 0 6 indicating desired decimal places
 // example: printdouble(3.1415, 2); // prints 3.14 (two decimal places)
 serial.print (int(val));  //prints int part
 if( precision > 0) {
   serial.print("."); // print 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) ;
 }
}

void setup() {
serial.begin(115200);
}

#define thermistorpin 0   // analog pin 0
double temp;
void loop() {
temp=thermistor(analogread(thermistorpin));           // read adc , convert celsius
serial.print(", celsius: "); printdouble(temp,3);     // display celsius
temp = (temp * 9.0)/ 5.0 + 32.0;                      // converts fahrenheit
serial.print(", fahrenheit: "); printdouble(temp,3);  // display fahrenheit
serial.println("");                                   // end of line
delay(100);                                           // delay bit... fun, , not serial.print faster serial connection can output
}

i'm starting think maybe wasn't best section of forum post in :p.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Thermistor Code


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums