Calculate temperature based on resistance from thermistor with lookup chart

Committer:
joeata2wh
Date:
Mon Mar 07 23:17:16 2016 +0000
Revision:
0:39f659087759
Child:
1:cc492e6ba333
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeata2wh 0:39f659087759 1 #include "mbed.h"
joeata2wh 0:39f659087759 2 #include "ohms.h"
joeata2wh 0:39f659087759 3
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 0:39f659087759 6 static 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 0:39f659087759 8 static int thermisterBase = -40;
joeata2wh 0:39f659087759 9 static int thermisterStep = 5;
joeata2wh 0:39f659087759 10 static int thermisterMaxTemp = 125;
joeata2wh 0:39f659087759 11 const float adc_ref = 3.30;
joeata2wh 0:39f659087759 12
joeata2wh 0:39f659087759 13
joeata2wh 0:39f659087759 14
joeata2wh 0:39f659087759 15 float conv_c_to_f(float tempC) {
joeata2wh 0:39f659087759 16 return (tempC * 1.8) + 32.0;
joeata2wh 0:39f659087759 17 }
joeata2wh 0:39f659087759 18
joeata2wh 0:39f659087759 19
joeata2wh 0:39f659087759 20 /* Read Thermister Temperature using ADC and voltage dividor
joeata2wh 0:39f659087759 21 Lookup the range of resistance by finding the first resistance
joeata2wh 0:39f659087759 22 lower than our specified value which gives us temp with 5C spread
joeata2wh 0:39f659087759 23 then adjust by how far our sensor reading is above the lower
joeata2wh 0:39f659087759 24 bound of the range.*/
joeata2wh 0:39f659087759 25 float readTemp(AnalogIn adcpin) {
joeata2wh 0:39f659087759 26 //sensorValue = avgRead(adcpin,3,2);
joeata2wh 0:39f659087759 27 float sensorValue = adcpin.read() * adc_ref;
joeata2wh 0:39f659087759 28 long sensorResist = calcResistV(22000, adc_ref, sensorValue);
joeata2wh 0:39f659087759 29 int tempCnt = thermisterBase;
joeata2wh 0:39f659087759 30 int tempndx = 0;
joeata2wh 0:39f659087759 31
joeata2wh 0:39f659087759 32 if (sensorValue >= thermisterResistance[0]) {
joeata2wh 0:39f659087759 33 return -255;
joeata2wh 0:39f659087759 34 }
joeata2wh 0:39f659087759 35
joeata2wh 0:39f659087759 36 while (sensorResist < thermisterResistance[tempndx]) {
joeata2wh 0:39f659087759 37 tempndx++;
joeata2wh 0:39f659087759 38 tempCnt += thermisterStep;
joeata2wh 0:39f659087759 39 if (tempCnt >= thermisterMaxTemp) {
joeata2wh 0:39f659087759 40 return 255;
joeata2wh 0:39f659087759 41 }
joeata2wh 0:39f659087759 42 }
joeata2wh 0:39f659087759 43 tempCnt -= thermisterStep; // backup to begin of range
joeata2wh 0:39f659087759 44 int lowTempResist = thermisterResistance[tempndx -1];
joeata2wh 0:39f659087759 45 int highTempResist = thermisterResistance[tempndx];
joeata2wh 0:39f659087759 46 int resistRangeDelta = lowTempResist - highTempResist;
joeata2wh 0:39f659087759 47 float degreesPerUnit = (float) thermisterStep / (float) (resistRangeDelta);
joeata2wh 0:39f659087759 48 int unitOverlow = lowTempResist - sensorResist;
joeata2wh 0:39f659087759 49 float degreeAdj = (float) unitOverlow * degreesPerUnit;
joeata2wh 0:39f659087759 50 return (float) tempCnt + degreeAdj;
joeata2wh 0:39f659087759 51 }
joeata2wh 0:39f659087759 52
joeata2wh 0:39f659087759 53 float avgReadTemp(AnalogIn adcpin, int numRead) {
joeata2wh 0:39f659087759 54 float sum = 0;
joeata2wh 0:39f659087759 55 int ndx;
joeata2wh 0:39f659087759 56 for (ndx = 0; ndx < numRead; ndx++) {
joeata2wh 0:39f659087759 57 wait(0.005);
joeata2wh 0:39f659087759 58 sum += readTemp(adcpin);
joeata2wh 0:39f659087759 59 }
joeata2wh 0:39f659087759 60 return (float) ((float) sum / (float) ndx);
joeata2wh 0:39f659087759 61 }