Calculate temperature based on resistance from thermistor with lookup chart
thermister_ert-j0eg1.cpp
- Committer:
- joeata2wh
- Date:
- 2016-03-30
- Revision:
- 5:88bf5a42812e
- Parent:
- 1:cc492e6ba333
- Child:
- 6:3c9cb2bd08c6
File content as of revision 5:88bf5a42812e:
#include "mbed.h" #include "ohms.h" // from library ohms_law #include "thermister_ert-j0eg1.h" // Set of resistance values from data sheet used to lookup temperature to find // location in a 5V band. const long thermisterResistance[] = {205200,154800,117900,90690,70370,50570,43440,34530,27640,22270,18060,14740,12110, 10000,8309,6941,5828,4916,4165,3542,3027,2595,2233,1929,1672,1451,1261,1097,956,836,732,642,565,499}; const int thermisterBase = -40.0f; const int thermisterStep = 5.0f; const int thermisterMaxTemp = 125.0f; float conv_c_to_f(float tempC) { return (tempC * 1.8f) + 32.0f; } float conv_f_to_c(float tempF) { return (tempF - 32.0f) * 0.55555555555f; } float ERTThermisterRead(PinName apin) { #ifdef DEBUG3 printf("ERTThermisterRead start\r\n"); #endif AnalogIn adcPin(apin); float tmp = ERTThermisterReadTemp(adcPin); #ifdef DEBUG3 printf("ERTThermisterRead temp=%f done\r\n", tmp); #endif return tmp; } // read the temp from thermister and print out // both the reading and intermediate values. float ERTThermisterReadPrint(Serial log, char *label, PinName apin) { AnalogIn adcpin(apin); float sensorValue = adcpin.read() * ERTThermisterAdcRef; long sensorResist = calcResistV(22000, ERTThermisterAdcRef, sensorValue); float t1C = ERTThermisterReadTemp(adcpin); float t1F = conv_c_to_f(t1C); log.printf("%s\tvin=%4.2f\tresist=%ld\tT1C=%4.1f\tT1F=%4.2f\r\n", label, sensorValue, sensorResist, t1C, t1F); return t1C; } /* Read Thermister Temperature using ADC and voltage dividor Lookup the range of resistance by finding the first resistance lower than our specified value which gives us temp with 5C spread then adjust by how far our sensor reading is above the lower bound of the range.*/ float ERTThermisterReadTemp(AnalogIn adcpin) { //sensorValue = avgRead(adcpin,3,2); float sensorValue = adcpin.read() * ERTThermisterAdcRef; long sensorResist = calcResistV(22000, ERTThermisterAdcRef, sensorValue); int tempCnt = thermisterBase; int tempndx = 0; #ifdef DEBUG3 printf("start ERTThermisterReadTemp adc=%4.2f resist=%ld\r\n", sensorValue, sensorResist); #endif if (sensorResist >= thermisterResistance[0]) { #ifdef DEBUG3 printf("resist too high\r\n"); #endif return ERTThermisterErrorUnder; } #ifdef DEBUG3 printf("entering while loop sensorResist=%ld thermisterResistance[0]=%ld\r\n",sensorResist, thermisterResistance[0]); #endif while (sensorResist < thermisterResistance[tempndx]) { tempndx++; tempCnt += thermisterStep; if (tempCnt >= thermisterMaxTemp) { return ERTThermisterErrorOver; } } tempCnt -= thermisterStep; // backup to begin of range #ifdef DEBUG3 printf("tempCnt=%d\r\n", tempCnt); #endif int lowTempResist = thermisterResistance[tempndx -1]; int highTempResist = thermisterResistance[tempndx]; int resistRangeDelta = lowTempResist - highTempResist; float degreesPerUnit = (float) thermisterStep / (float) (resistRangeDelta); int unitOverlow = lowTempResist - sensorResist; float degreeAdj = (float) unitOverlow * degreesPerUnit; #ifdef DEBUG3 printf("degreeAdj=%f\r\n", degreeAdj); #endif return (float) tempCnt + degreeAdj; } float ERTThermisterAvgReadTemp(AnalogIn adcpin, int numRead) { float sum = 0; int ndx; for (ndx = 0; ndx < numRead; ndx++) { wait(0.005); sum += ERTThermisterReadTemp(adcpin); } return (float) ((float) sum / (float) ndx); }