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