Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
thermister_ert-j0eg1.cpp
00001 /* thermister_ert-j0eg1.cpp 00002 By Joseph Ellsworth CTO of A2WH 00003 Take a look at A2WH.com Producing Water from Air using Solar Energy 00004 March-2016 License: https://developer.mbed.org/handbook/MIT-Licence 00005 */ 00006 00007 #include "mbed.h" 00008 #include "ohms.h" // from library ohms_law 00009 #include "thermister_ert-j0eg1.h" 00010 // Set of resistance values from data sheet used to lookup temperature to find 00011 // location in a 5V band. 00012 const long thermisterResistance[] = {205200,154800,117900,90690,70370,50570,43440,34530,27640,22270,18060,14740,12110, 00013 10000,8309,6941,5828,4916,4165,3542,3027,2595,2233,1929,1672,1451,1261,1097,956,836,732,642,565,499}; 00014 const int thermisterBase = -40.0f; 00015 const int thermisterStep = 5.0f; 00016 const int thermisterMaxTemp = 125.0f; 00017 00018 float conv_c_to_f(float tempC) { 00019 return (tempC * 1.8f) + 32.0f; 00020 } 00021 00022 float conv_f_to_c(float tempF) { 00023 return (tempF - 32.0f) * 0.55555555555f; 00024 } 00025 00026 float ERTThermisterRead(PinName apin) { 00027 #ifdef DEBUG3 00028 printf("ERTThermisterRead start\r\n"); 00029 #endif 00030 AnalogIn adcPin(apin); 00031 float tmp = ERTThermisterReadTemp(adcPin); 00032 #ifdef DEBUG3 00033 printf("ERTThermisterRead temp=%f done\r\n", tmp); 00034 #endif 00035 return tmp; 00036 } 00037 00038 // read the temp from thermister and print out 00039 // both the reading and intermediate values. 00040 float ERTThermisterReadPrint(Serial log, char *label, PinName apin) { 00041 AnalogIn adcpin(apin); 00042 float sensorValue = adcpin.read() * ERTThermisterAdcRef; 00043 long sensorResist = calcResistV(22000, ERTThermisterAdcRef, sensorValue); 00044 float t1C = ERTThermisterReadTemp(adcpin); 00045 float t1F = conv_c_to_f(t1C); 00046 log.printf("%s\tvin=%4.2f\tresist=%ld\tT1C=%4.1f\tT1F=%4.2f\r\n", 00047 label, sensorValue, sensorResist, t1C, t1F); 00048 return t1C; 00049 } 00050 00051 00052 /* Read Thermister Temperature using ADC and voltage dividor 00053 Lookup the range of resistance by finding the first resistance 00054 lower than our specified value which gives us temp with 5C spread 00055 then adjust by how far our sensor reading is above the lower 00056 bound of the range.*/ 00057 float ERTThermisterReadTemp(AnalogIn adcpin) { 00058 //sensorValue = avgRead(adcpin,3,2); 00059 float sensorValue = adcpin.read() * ERTThermisterAdcRef; 00060 long sensorResist = calcResistV(22000, ERTThermisterAdcRef, sensorValue); 00061 int tempCnt = thermisterBase; 00062 int tempndx = 0; 00063 #ifdef DEBUG3 00064 printf("start ERTThermisterReadTemp adc=%4.2f resist=%ld\r\n", sensorValue, sensorResist); 00065 #endif 00066 if (sensorResist >= thermisterResistance[0]) { 00067 #ifdef DEBUG3 00068 printf("resist too high\r\n"); 00069 #endif 00070 return ERTThermisterErrorUnder; 00071 } 00072 00073 #ifdef DEBUG3 00074 printf("entering while loop sensorResist=%ld thermisterResistance[0]=%ld\r\n",sensorResist, thermisterResistance[0]); 00075 #endif 00076 while (sensorResist < thermisterResistance[tempndx]) { 00077 tempndx++; 00078 tempCnt += thermisterStep; 00079 if (tempCnt >= thermisterMaxTemp) { 00080 return ERTThermisterErrorOver; 00081 } 00082 } 00083 tempCnt -= thermisterStep; // backup to begin of range 00084 #ifdef DEBUG3 00085 printf("tempCnt=%d\r\n", tempCnt); 00086 #endif 00087 int lowTempResist = thermisterResistance[tempndx -1]; 00088 int highTempResist = thermisterResistance[tempndx]; 00089 int resistRangeDelta = lowTempResist - highTempResist; 00090 float degreesPerUnit = (float) thermisterStep / (float) (resistRangeDelta); 00091 int unitOverlow = lowTempResist - sensorResist; 00092 float degreeAdj = (float) unitOverlow * degreesPerUnit; 00093 #ifdef DEBUG3 00094 printf("degreeAdj=%f\r\n", degreeAdj); 00095 #endif 00096 return (float) tempCnt + degreeAdj; 00097 } 00098 00099 float ERTThermisterAvgReadTemp(AnalogIn adcpin, int numRead) { 00100 float sum = 0; 00101 int ndx; 00102 for (ndx = 0; ndx < numRead; ndx++) { 00103 wait(0.005); 00104 sum += ERTThermisterReadTemp(adcpin); 00105 } 00106 return (float) ((float) sum / (float) ndx); 00107 }
Generated on Sat Jul 16 2022 23:26:08 by
1.7.2