Calculate temperature based on resistance from thermistor with lookup chart

Committer:
joeata2wh
Date:
Wed Mar 30 13:32:40 2016 +0000
Revision:
5:88bf5a42812e
Parent:
1:cc492e6ba333
Child:
6:3c9cb2bd08c6
Fixed odd bug when readings where for very high resistance.  Also renamed some constants to provide less potential for conflict with other modules.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeata2wh 0:39f659087759 1 #include "mbed.h"
joeata2wh 1:cc492e6ba333 2 #include "ohms.h" // from library ohms_law
joeata2wh 5:88bf5a42812e 3 #include "thermister_ert-j0eg1.h"
joeata2wh 0:39f659087759 4 // Set of resistance values from data sheet used to lookup temperature to find
joeata2wh 0:39f659087759 5 // location in a 5V band.
joeata2wh 5:88bf5a42812e 6 const long thermisterResistance[] = {205200,154800,117900,90690,70370,50570,43440,34530,27640,22270,18060,14740,12110,
joeata2wh 0:39f659087759 7 10000,8309,6941,5828,4916,4165,3542,3027,2595,2233,1929,1672,1451,1261,1097,956,836,732,642,565,499};
joeata2wh 5:88bf5a42812e 8 const int thermisterBase = -40.0f;
joeata2wh 5:88bf5a42812e 9 const int thermisterStep = 5.0f;
joeata2wh 5:88bf5a42812e 10 const int thermisterMaxTemp = 125.0f;
joeata2wh 0:39f659087759 11
joeata2wh 0:39f659087759 12 float conv_c_to_f(float tempC) {
joeata2wh 5:88bf5a42812e 13 return (tempC * 1.8f) + 32.0f;
joeata2wh 5:88bf5a42812e 14 }
joeata2wh 5:88bf5a42812e 15
joeata2wh 5:88bf5a42812e 16 float conv_f_to_c(float tempF) {
joeata2wh 5:88bf5a42812e 17 return (tempF - 32.0f) * 0.55555555555f;
joeata2wh 5:88bf5a42812e 18 }
joeata2wh 5:88bf5a42812e 19
joeata2wh 5:88bf5a42812e 20 float ERTThermisterRead(PinName apin) {
joeata2wh 5:88bf5a42812e 21 #ifdef DEBUG3
joeata2wh 5:88bf5a42812e 22 printf("ERTThermisterRead start\r\n");
joeata2wh 5:88bf5a42812e 23 #endif
joeata2wh 5:88bf5a42812e 24 AnalogIn adcPin(apin);
joeata2wh 5:88bf5a42812e 25 float tmp = ERTThermisterReadTemp(adcPin);
joeata2wh 5:88bf5a42812e 26 #ifdef DEBUG3
joeata2wh 5:88bf5a42812e 27 printf("ERTThermisterRead temp=%f done\r\n", tmp);
joeata2wh 5:88bf5a42812e 28 #endif
joeata2wh 5:88bf5a42812e 29 return tmp;
joeata2wh 5:88bf5a42812e 30 }
joeata2wh 5:88bf5a42812e 31
joeata2wh 5:88bf5a42812e 32 // read the temp from thermister and print out
joeata2wh 5:88bf5a42812e 33 // both the reading and intermediate values.
joeata2wh 5:88bf5a42812e 34 float ERTThermisterReadPrint(Serial log, char *label, PinName apin) {
joeata2wh 5:88bf5a42812e 35 AnalogIn adcpin(apin);
joeata2wh 5:88bf5a42812e 36 float sensorValue = adcpin.read() * ERTThermisterAdcRef;
joeata2wh 5:88bf5a42812e 37 long sensorResist = calcResistV(22000, ERTThermisterAdcRef, sensorValue);
joeata2wh 5:88bf5a42812e 38 float t1C = ERTThermisterReadTemp(adcpin);
joeata2wh 5:88bf5a42812e 39 float t1F = conv_c_to_f(t1C);
joeata2wh 5:88bf5a42812e 40 log.printf("%s\tvin=%4.2f\tresist=%ld\tT1C=%4.1f\tT1F=%4.2f\r\n",
joeata2wh 5:88bf5a42812e 41 label, sensorValue, sensorResist, t1C, t1F);
joeata2wh 5:88bf5a42812e 42 return t1C;
joeata2wh 0:39f659087759 43 }
joeata2wh 0:39f659087759 44
joeata2wh 0:39f659087759 45
joeata2wh 0:39f659087759 46 /* Read Thermister Temperature using ADC and voltage dividor
joeata2wh 0:39f659087759 47 Lookup the range of resistance by finding the first resistance
joeata2wh 0:39f659087759 48 lower than our specified value which gives us temp with 5C spread
joeata2wh 0:39f659087759 49 then adjust by how far our sensor reading is above the lower
joeata2wh 0:39f659087759 50 bound of the range.*/
joeata2wh 5:88bf5a42812e 51 float ERTThermisterReadTemp(AnalogIn adcpin) {
joeata2wh 0:39f659087759 52 //sensorValue = avgRead(adcpin,3,2);
joeata2wh 5:88bf5a42812e 53 float sensorValue = adcpin.read() * ERTThermisterAdcRef;
joeata2wh 5:88bf5a42812e 54 long sensorResist = calcResistV(22000, ERTThermisterAdcRef, sensorValue);
joeata2wh 0:39f659087759 55 int tempCnt = thermisterBase;
joeata2wh 0:39f659087759 56 int tempndx = 0;
joeata2wh 5:88bf5a42812e 57 #ifdef DEBUG3
joeata2wh 5:88bf5a42812e 58 printf("start ERTThermisterReadTemp adc=%4.2f resist=%ld\r\n", sensorValue, sensorResist);
joeata2wh 5:88bf5a42812e 59 #endif
joeata2wh 5:88bf5a42812e 60 if (sensorResist >= thermisterResistance[0]) {
joeata2wh 5:88bf5a42812e 61 #ifdef DEBUG3
joeata2wh 5:88bf5a42812e 62 printf("resist too high\r\n");
joeata2wh 5:88bf5a42812e 63 #endif
joeata2wh 5:88bf5a42812e 64 return ERTThermisterErrorUnder;
joeata2wh 0:39f659087759 65 }
joeata2wh 0:39f659087759 66
joeata2wh 5:88bf5a42812e 67 #ifdef DEBUG3
joeata2wh 5:88bf5a42812e 68 printf("entering while loop sensorResist=%ld thermisterResistance[0]=%ld\r\n",sensorResist, thermisterResistance[0]);
joeata2wh 5:88bf5a42812e 69 #endif
joeata2wh 0:39f659087759 70 while (sensorResist < thermisterResistance[tempndx]) {
joeata2wh 0:39f659087759 71 tempndx++;
joeata2wh 0:39f659087759 72 tempCnt += thermisterStep;
joeata2wh 0:39f659087759 73 if (tempCnt >= thermisterMaxTemp) {
joeata2wh 5:88bf5a42812e 74 return ERTThermisterErrorOver;
joeata2wh 0:39f659087759 75 }
joeata2wh 0:39f659087759 76 }
joeata2wh 5:88bf5a42812e 77 tempCnt -= thermisterStep; // backup to begin of range
joeata2wh 5:88bf5a42812e 78 #ifdef DEBUG3
joeata2wh 5:88bf5a42812e 79 printf("tempCnt=%d\r\n", tempCnt);
joeata2wh 5:88bf5a42812e 80 #endif
joeata2wh 0:39f659087759 81 int lowTempResist = thermisterResistance[tempndx -1];
joeata2wh 0:39f659087759 82 int highTempResist = thermisterResistance[tempndx];
joeata2wh 0:39f659087759 83 int resistRangeDelta = lowTempResist - highTempResist;
joeata2wh 0:39f659087759 84 float degreesPerUnit = (float) thermisterStep / (float) (resistRangeDelta);
joeata2wh 0:39f659087759 85 int unitOverlow = lowTempResist - sensorResist;
joeata2wh 0:39f659087759 86 float degreeAdj = (float) unitOverlow * degreesPerUnit;
joeata2wh 5:88bf5a42812e 87 #ifdef DEBUG3
joeata2wh 5:88bf5a42812e 88 printf("degreeAdj=%f\r\n", degreeAdj);
joeata2wh 5:88bf5a42812e 89 #endif
joeata2wh 0:39f659087759 90 return (float) tempCnt + degreeAdj;
joeata2wh 0:39f659087759 91 }
joeata2wh 0:39f659087759 92
joeata2wh 5:88bf5a42812e 93 float ERTThermisterAvgReadTemp(AnalogIn adcpin, int numRead) {
joeata2wh 0:39f659087759 94 float sum = 0;
joeata2wh 0:39f659087759 95 int ndx;
joeata2wh 0:39f659087759 96 for (ndx = 0; ndx < numRead; ndx++) {
joeata2wh 0:39f659087759 97 wait(0.005);
joeata2wh 5:88bf5a42812e 98 sum += ERTThermisterReadTemp(adcpin);
joeata2wh 0:39f659087759 99 }
joeata2wh 0:39f659087759 100 return (float) ((float) sum / (float) ndx);
joeata2wh 0:39f659087759 101 }