![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Interfaced 9 MF51E103F3950 NTC 10k Thermistors to a Nucleo board for a science project.
main.cpp@3:cea064439566, 2017-01-31 (annotated)
- Committer:
- smartsystemdesign
- Date:
- Tue Jan 31 18:13:04 2017 +0000
- Revision:
- 3:cea064439566
- Parent:
- 2:cda957297041
- Child:
- 4:3ed3e2aac291
Added timestamp in minutes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
smartsystemdesign | 0:453df8530a88 | 1 | // Using MF51E103F3950 NTC 10k Thermistor, with a B value of 3950 |
smartsystemdesign | 0:453df8530a88 | 2 | #include "mbed.h" |
smartsystemdesign | 0:453df8530a88 | 3 | |
smartsystemdesign | 0:453df8530a88 | 4 | // which analog pin to connect |
smartsystemdesign | 0:453df8530a88 | 5 | #define THERMISTORPIN A0 |
smartsystemdesign | 0:453df8530a88 | 6 | // resistance at 25 degrees C |
smartsystemdesign | 0:453df8530a88 | 7 | #define THERMISTORNOMINAL 10000 |
smartsystemdesign | 0:453df8530a88 | 8 | // temp. for nominal resistance (almost always 25 C) |
smartsystemdesign | 0:453df8530a88 | 9 | #define TEMPERATURENOMINAL 25 |
smartsystemdesign | 0:453df8530a88 | 10 | // how many samples to take and average, more takes longer |
smartsystemdesign | 0:453df8530a88 | 11 | // but is more 'smooth' |
smartsystemdesign | 0:453df8530a88 | 12 | #define NUMSAMPLES 5 |
smartsystemdesign | 0:453df8530a88 | 13 | // The beta coefficient of the thermistor (usually 3000-4000) |
smartsystemdesign | 0:453df8530a88 | 14 | #define BCOEFFICIENT 3950 |
smartsystemdesign | 0:453df8530a88 | 15 | // the value of the 'other' resistor |
smartsystemdesign | 0:453df8530a88 | 16 | #define SERIESRESISTOR 10000.0f |
smartsystemdesign | 0:453df8530a88 | 17 | |
smartsystemdesign | 0:453df8530a88 | 18 | #define OFFSET_NUM_AVERAGES 20 |
smartsystemdesign | 0:453df8530a88 | 19 | |
smartsystemdesign | 0:453df8530a88 | 20 | Serial pc(USBTX, USBRX); // tx, rx |
smartsystemdesign | 0:453df8530a88 | 21 | AnalogIn ain0(A0); |
smartsystemdesign | 0:453df8530a88 | 22 | AnalogIn ain1(A1); |
smartsystemdesign | 0:453df8530a88 | 23 | AnalogIn ain2(A2); |
smartsystemdesign | 0:453df8530a88 | 24 | AnalogIn ain3(A3); |
smartsystemdesign | 0:453df8530a88 | 25 | AnalogIn ain4(A4); |
smartsystemdesign | 0:453df8530a88 | 26 | AnalogIn ain5(A5); |
smartsystemdesign | 2:cda957297041 | 27 | AnalogIn ain6(PA_5); // Note: on the Nucleo F401, this is connected to the LED. |
smartsystemdesign | 2:cda957297041 | 28 | AnalogIn ain7(PA_6); |
smartsystemdesign | 0:453df8530a88 | 29 | AnalogIn ain8(PA_7); |
smartsystemdesign | 0:453df8530a88 | 30 | |
smartsystemdesign | 3:cea064439566 | 31 | Timer timer; |
smartsystemdesign | 3:cea064439566 | 32 | |
smartsystemdesign | 0:453df8530a88 | 33 | float offsets[9] = {0}; |
smartsystemdesign | 2:cda957297041 | 34 | float resistors[9] = {9984.270169f, 9999.947582f, 9999.962164f, 9999.960999f, 9999.958757f, 9999.918074f, 9999.969933f, 9999.966487f, 9999.977766f}; |
smartsystemdesign | 0:453df8530a88 | 35 | |
smartsystemdesign | 0:453df8530a88 | 36 | int main() |
smartsystemdesign | 0:453df8530a88 | 37 | { |
smartsystemdesign | 0:453df8530a88 | 38 | pc.baud(115200); |
smartsystemdesign | 0:453df8530a88 | 39 | |
smartsystemdesign | 0:453df8530a88 | 40 | pc.printf("\nThermistor test\n"); |
smartsystemdesign | 0:453df8530a88 | 41 | float rawReading[9] = {0}; |
smartsystemdesign | 0:453df8530a88 | 42 | float reading[9] = {0}; |
smartsystemdesign | 0:453df8530a88 | 43 | |
smartsystemdesign | 0:453df8530a88 | 44 | // Find offset values (take average 20 readings for offset): |
smartsystemdesign | 0:453df8530a88 | 45 | float sum = 0; |
smartsystemdesign | 0:453df8530a88 | 46 | // for(uint8_t i = 0; i < OFFSET_NUM_AVERAGES; i++) { |
smartsystemdesign | 0:453df8530a88 | 47 | // sum += ain0.read(); |
smartsystemdesign | 0:453df8530a88 | 48 | // } |
smartsystemdesign | 0:453df8530a88 | 49 | // offsets[0] = (sum / OFFSET_NUM_AVERAGES); |
smartsystemdesign | 0:453df8530a88 | 50 | // |
smartsystemdesign | 0:453df8530a88 | 51 | // sum = 0; |
smartsystemdesign | 0:453df8530a88 | 52 | // for(uint8_t i = 0; i < OFFSET_NUM_AVERAGES; i++) { |
smartsystemdesign | 0:453df8530a88 | 53 | // sum += ain1.read(); |
smartsystemdesign | 0:453df8530a88 | 54 | // } |
smartsystemdesign | 0:453df8530a88 | 55 | // offsets[1] = (sum / OFFSET_NUM_AVERAGES); |
smartsystemdesign | 0:453df8530a88 | 56 | // |
smartsystemdesign | 0:453df8530a88 | 57 | // sum = 0; |
smartsystemdesign | 0:453df8530a88 | 58 | // for(uint8_t i = 0; i < OFFSET_NUM_AVERAGES; i++) { |
smartsystemdesign | 0:453df8530a88 | 59 | // sum += ain2.read(); |
smartsystemdesign | 0:453df8530a88 | 60 | // } |
smartsystemdesign | 0:453df8530a88 | 61 | // offsets[2] = (sum / OFFSET_NUM_AVERAGES); |
smartsystemdesign | 0:453df8530a88 | 62 | // |
smartsystemdesign | 0:453df8530a88 | 63 | // sum = 0; |
smartsystemdesign | 0:453df8530a88 | 64 | // for(uint8_t i = 0; i < OFFSET_NUM_AVERAGES; i++) { |
smartsystemdesign | 0:453df8530a88 | 65 | // sum += ain3.read(); |
smartsystemdesign | 0:453df8530a88 | 66 | // } |
smartsystemdesign | 0:453df8530a88 | 67 | // offsets[3] = (sum / OFFSET_NUM_AVERAGES); |
smartsystemdesign | 0:453df8530a88 | 68 | // |
smartsystemdesign | 0:453df8530a88 | 69 | // sum = 0; |
smartsystemdesign | 0:453df8530a88 | 70 | // for(uint8_t i = 0; i < OFFSET_NUM_AVERAGES; i++) { |
smartsystemdesign | 0:453df8530a88 | 71 | // sum += ain4.read(); |
smartsystemdesign | 0:453df8530a88 | 72 | // } |
smartsystemdesign | 0:453df8530a88 | 73 | // offsets[4] = (sum / OFFSET_NUM_AVERAGES); |
smartsystemdesign | 0:453df8530a88 | 74 | // |
smartsystemdesign | 0:453df8530a88 | 75 | // sum = 0; |
smartsystemdesign | 0:453df8530a88 | 76 | // for(uint8_t i = 0; i < OFFSET_NUM_AVERAGES; i++) { |
smartsystemdesign | 0:453df8530a88 | 77 | // sum += ain5.read(); |
smartsystemdesign | 0:453df8530a88 | 78 | // } |
smartsystemdesign | 0:453df8530a88 | 79 | // offsets[5] = (sum / OFFSET_NUM_AVERAGES); |
smartsystemdesign | 0:453df8530a88 | 80 | // |
smartsystemdesign | 0:453df8530a88 | 81 | // sum = 0; |
smartsystemdesign | 0:453df8530a88 | 82 | // for(uint8_t i = 0; i < OFFSET_NUM_AVERAGES; i++) { |
smartsystemdesign | 0:453df8530a88 | 83 | // sum += ain5.read(); |
smartsystemdesign | 0:453df8530a88 | 84 | // } |
smartsystemdesign | 0:453df8530a88 | 85 | // offsets[5] = (sum / OFFSET_NUM_AVERAGES); |
smartsystemdesign | 0:453df8530a88 | 86 | // |
smartsystemdesign | 0:453df8530a88 | 87 | // sum = 0; |
smartsystemdesign | 0:453df8530a88 | 88 | // for(uint8_t i = 0; i < OFFSET_NUM_AVERAGES; i++) { |
smartsystemdesign | 0:453df8530a88 | 89 | // sum += ain6.read(); |
smartsystemdesign | 0:453df8530a88 | 90 | // } |
smartsystemdesign | 0:453df8530a88 | 91 | // offsets[6] = (sum / OFFSET_NUM_AVERAGES); |
smartsystemdesign | 0:453df8530a88 | 92 | // |
smartsystemdesign | 0:453df8530a88 | 93 | // sum = 0; |
smartsystemdesign | 0:453df8530a88 | 94 | // for(uint8_t i = 0; i < OFFSET_NUM_AVERAGES; i++) { |
smartsystemdesign | 0:453df8530a88 | 95 | // sum += ain7.read(); |
smartsystemdesign | 0:453df8530a88 | 96 | // } |
smartsystemdesign | 0:453df8530a88 | 97 | // offsets[7] = (sum / OFFSET_NUM_AVERAGES); |
smartsystemdesign | 0:453df8530a88 | 98 | // |
smartsystemdesign | 0:453df8530a88 | 99 | // sum = 0; |
smartsystemdesign | 0:453df8530a88 | 100 | // for(uint8_t i = 0; i < OFFSET_NUM_AVERAGES; i++) { |
smartsystemdesign | 0:453df8530a88 | 101 | // sum += ain8.read(); |
smartsystemdesign | 0:453df8530a88 | 102 | // } |
smartsystemdesign | 0:453df8530a88 | 103 | // offsets[8] = (sum / OFFSET_NUM_AVERAGES); |
smartsystemdesign | 0:453df8530a88 | 104 | |
smartsystemdesign | 0:453df8530a88 | 105 | // Single reading for offset: |
smartsystemdesign | 0:453df8530a88 | 106 | // offsets[0] = ain0.read(); |
smartsystemdesign | 0:453df8530a88 | 107 | // offsets[1] = ain1.read(); |
smartsystemdesign | 0:453df8530a88 | 108 | // offsets[2] = ain2.read(); |
smartsystemdesign | 0:453df8530a88 | 109 | // offsets[3] = ain3.read(); |
smartsystemdesign | 0:453df8530a88 | 110 | // offsets[4] = ain4.read(); |
smartsystemdesign | 0:453df8530a88 | 111 | // offsets[5] = ain5.read(); |
smartsystemdesign | 0:453df8530a88 | 112 | // offsets[6] = ain6.read(); |
smartsystemdesign | 0:453df8530a88 | 113 | // offsets[7] = ain7.read(); |
smartsystemdesign | 0:453df8530a88 | 114 | // offsets[8] = ain8.read(); |
smartsystemdesign | 0:453df8530a88 | 115 | |
smartsystemdesign | 0:453df8530a88 | 116 | // Find average of offsets and store in offsets array: |
smartsystemdesign | 0:453df8530a88 | 117 | sum = 0; |
smartsystemdesign | 0:453df8530a88 | 118 | float average = 0; |
smartsystemdesign | 0:453df8530a88 | 119 | |
smartsystemdesign | 0:453df8530a88 | 120 | for(uint8_t i = 0; i < 9; i++) { |
smartsystemdesign | 0:453df8530a88 | 121 | sum += offsets[i]; |
smartsystemdesign | 0:453df8530a88 | 122 | } |
smartsystemdesign | 0:453df8530a88 | 123 | |
smartsystemdesign | 0:453df8530a88 | 124 | average = sum / 9; |
smartsystemdesign | 0:453df8530a88 | 125 | |
smartsystemdesign | 0:453df8530a88 | 126 | for(uint8_t i = 0; i < 9; i++) { |
smartsystemdesign | 0:453df8530a88 | 127 | offsets[i] = offsets[i] - average; |
smartsystemdesign | 0:453df8530a88 | 128 | } |
smartsystemdesign | 0:453df8530a88 | 129 | |
smartsystemdesign | 3:cea064439566 | 130 | timer.start(); |
smartsystemdesign | 3:cea064439566 | 131 | |
smartsystemdesign | 0:453df8530a88 | 132 | while(1) { |
smartsystemdesign | 0:453df8530a88 | 133 | // Take reading and apply offset, then use voltage divider equation: |
smartsystemdesign | 0:453df8530a88 | 134 | // rawReading[0] = 1 / (ain0.read() - offsets[0]) - 1; |
smartsystemdesign | 0:453df8530a88 | 135 | // rawReading[1] = 1 / (ain1.read() - offsets[1]) - 1; |
smartsystemdesign | 0:453df8530a88 | 136 | // rawReading[2] = 1 / (ain2.read() - offsets[2]) - 1; |
smartsystemdesign | 0:453df8530a88 | 137 | // rawReading[3] = 1 / (ain3.read() - offsets[3]) - 1; |
smartsystemdesign | 0:453df8530a88 | 138 | // rawReading[4] = 1 / (ain4.read() - offsets[4]) - 1; |
smartsystemdesign | 0:453df8530a88 | 139 | // rawReading[5] = 1 / (ain5.read() - offsets[5]) - 1; |
smartsystemdesign | 0:453df8530a88 | 140 | // rawReading[6] = 1 / (ain6.read() - offsets[6]) - 1; |
smartsystemdesign | 0:453df8530a88 | 141 | // rawReading[7] = 1 / (ain7.read() - offsets[7]) - 1; |
smartsystemdesign | 0:453df8530a88 | 142 | // rawReading[8] = 1 / (ain8.read() - offsets[8]) - 1; |
smartsystemdesign | 0:453df8530a88 | 143 | |
smartsystemdesign | 0:453df8530a88 | 144 | rawReading[0] = 1 / ain0.read() - 1; |
smartsystemdesign | 0:453df8530a88 | 145 | rawReading[1] = 1 / ain1.read() - 1; |
smartsystemdesign | 0:453df8530a88 | 146 | rawReading[2] = 1 / ain2.read() - 1; |
smartsystemdesign | 0:453df8530a88 | 147 | rawReading[3] = 1 / ain3.read() - 1; |
smartsystemdesign | 0:453df8530a88 | 148 | rawReading[4] = 1 / ain4.read() - 1; |
smartsystemdesign | 0:453df8530a88 | 149 | rawReading[5] = 1 / ain5.read() - 1; |
smartsystemdesign | 0:453df8530a88 | 150 | rawReading[6] = 1 / ain6.read() - 1; |
smartsystemdesign | 0:453df8530a88 | 151 | rawReading[7] = 1 / ain7.read() - 1; |
smartsystemdesign | 0:453df8530a88 | 152 | rawReading[8] = 1 / ain8.read() - 1; |
smartsystemdesign | 0:453df8530a88 | 153 | |
smartsystemdesign | 0:453df8530a88 | 154 | for(uint8_t i = 0; i < 9; i++) { |
smartsystemdesign | 0:453df8530a88 | 155 | reading[i] = resistors[i] / rawReading[i]; |
smartsystemdesign | 0:453df8530a88 | 156 | } |
smartsystemdesign | 0:453df8530a88 | 157 | |
smartsystemdesign | 0:453df8530a88 | 158 | // pc.printf("Thermistor resistance "); |
smartsystemdesign | 0:453df8530a88 | 159 | // pc.printf("%f\t", reading0); |
smartsystemdesign | 0:453df8530a88 | 160 | |
smartsystemdesign | 0:453df8530a88 | 161 | // Apply Steinhart equation: |
smartsystemdesign | 3:cea064439566 | 162 | |
smartsystemdesign | 0:453df8530a88 | 163 | for(uint8_t i = 0; i < 9; i++) { |
smartsystemdesign | 0:453df8530a88 | 164 | float steinhart; |
smartsystemdesign | 0:453df8530a88 | 165 | steinhart = reading[i] / THERMISTORNOMINAL; // (R/Ro) |
smartsystemdesign | 0:453df8530a88 | 166 | steinhart = log(steinhart); // ln(R/Ro) |
smartsystemdesign | 0:453df8530a88 | 167 | steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro) |
smartsystemdesign | 0:453df8530a88 | 168 | steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To) |
smartsystemdesign | 0:453df8530a88 | 169 | steinhart = 1.0f / steinhart; // Invert |
smartsystemdesign | 0:453df8530a88 | 170 | steinhart -= 273.15f; // convert to C |
smartsystemdesign | 0:453df8530a88 | 171 | // pc.printf("Temperature %d: %f *C\t", i, steinhart); |
smartsystemdesign | 0:453df8530a88 | 172 | |
smartsystemdesign | 0:453df8530a88 | 173 | // Print commas for plotting (except at end): |
smartsystemdesign | 0:453df8530a88 | 174 | if(i == 8) { |
smartsystemdesign | 0:453df8530a88 | 175 | pc.printf("%.4f", steinhart); |
smartsystemdesign | 0:453df8530a88 | 176 | } |
smartsystemdesign | 2:cda957297041 | 177 | else if(i == 6) |
smartsystemdesign | 2:cda957297041 | 178 | continue; // bad analog input (on LED) |
smartsystemdesign | 0:453df8530a88 | 179 | else { |
smartsystemdesign | 0:453df8530a88 | 180 | pc.printf("%.4f,", steinhart); |
smartsystemdesign | 0:453df8530a88 | 181 | } |
smartsystemdesign | 0:453df8530a88 | 182 | } |
smartsystemdesign | 3:cea064439566 | 183 | pc.printf("%.2f, ", timer.read()/60.0f); // print time from boot in minutes |
smartsystemdesign | 3:cea064439566 | 184 | |
smartsystemdesign | 0:453df8530a88 | 185 | pc.printf("\n"); |
smartsystemdesign | 1:010d1ef2f3ab | 186 | wait(1); |
smartsystemdesign | 0:453df8530a88 | 187 | } |
smartsystemdesign | 0:453df8530a88 | 188 | } |