Demo of Thermistor to Temperature conversion using Steinhart-Hart equation on the FRDM-K64F baord

Dependencies:   mbed

Committer:
unix_guru
Date:
Fri Jan 22 17:21:52 2016 +0000
Revision:
0:eaea12966d34
First version of FRDM-Thermistor-Demo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
unix_guru 0:eaea12966d34 1 /* FRDM-Thermistor-Demo
unix_guru 0:eaea12966d34 2 This is a Thermistor to Temerature conversion demo
unix_guru 0:eaea12966d34 3 for the @NXP (@freescale) FRDM-K64F demo board
unix_guru 0:eaea12966d34 4 Much thanks to @Adafruit for this tutorial:
unix_guru 0:eaea12966d34 5 https://learn.adafruit.com/thermistor/using-a-thermistor
unix_guru 0:eaea12966d34 6
unix_guru 0:eaea12966d34 7 The 100K Thermistor is configured with a 4.7k series resistor
unix_guru 0:eaea12966d34 8 tied to vcc (3.3v) like this:
unix_guru 0:eaea12966d34 9
unix_guru 0:eaea12966d34 10 +3.3v
unix_guru 0:eaea12966d34 11 |
unix_guru 0:eaea12966d34 12 \
unix_guru 0:eaea12966d34 13 / 4.7k series resistor
unix_guru 0:eaea12966d34 14 \
unix_guru 0:eaea12966d34 15 /
unix_guru 0:eaea12966d34 16 |
unix_guru 0:eaea12966d34 17 .-----------O To Anlog pin on FRDM board
unix_guru 0:eaea12966d34 18 |
unix_guru 0:eaea12966d34 19 \
unix_guru 0:eaea12966d34 20 /
unix_guru 0:eaea12966d34 21 Thermistor 100k Nominal
unix_guru 0:eaea12966d34 22 \
unix_guru 0:eaea12966d34 23 /
unix_guru 0:eaea12966d34 24 |
unix_guru 0:eaea12966d34 25 ---
unix_guru 0:eaea12966d34 26 GND
unix_guru 0:eaea12966d34 27
unix_guru 0:eaea12966d34 28
unix_guru 0:eaea12966d34 29 */
unix_guru 0:eaea12966d34 30
unix_guru 0:eaea12966d34 31
unix_guru 0:eaea12966d34 32 #include "mbed.h"
unix_guru 0:eaea12966d34 33
unix_guru 0:eaea12966d34 34 #define THERMISTORNOMINAL 100000 // 100k
unix_guru 0:eaea12966d34 35 // temp. for nominal resistance (almost always 25 C)
unix_guru 0:eaea12966d34 36 #define TEMPERATURENOMINAL 25
unix_guru 0:eaea12966d34 37 // The beta coefficient of the thermistor (usually 3000-4000)
unix_guru 0:eaea12966d34 38 #define BCOEFFICIENT 3950
unix_guru 0:eaea12966d34 39 // the value of the 'other' resistor
unix_guru 0:eaea12966d34 40 #define SERIESRESISTOR 4700
unix_guru 0:eaea12966d34 41
unix_guru 0:eaea12966d34 42 AnalogIn Thermistor(A3);
unix_guru 0:eaea12966d34 43
unix_guru 0:eaea12966d34 44 Serial pc(USBTX, USBRX);
unix_guru 0:eaea12966d34 45
unix_guru 0:eaea12966d34 46
unix_guru 0:eaea12966d34 47 // This is the workhorse routine that calculates the temperature
unix_guru 0:eaea12966d34 48 // using the Steinhart-Hart equation for thermistors
unix_guru 0:eaea12966d34 49 // https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
unix_guru 0:eaea12966d34 50 float get_temperature()
unix_guru 0:eaea12966d34 51 {
unix_guru 0:eaea12966d34 52 float temperature, resistance;
unix_guru 0:eaea12966d34 53 float steinhart;
unix_guru 0:eaea12966d34 54 int a;
unix_guru 0:eaea12966d34 55
unix_guru 0:eaea12966d34 56 a = Thermistor.read_u16(); // Read 16bit Analog value
unix_guru 0:eaea12966d34 57 pc.printf("Raw Analog Value for Thermistor = %d\r\n",a);
unix_guru 0:eaea12966d34 58
unix_guru 0:eaea12966d34 59 /* Calculate the resistance of the thermistor from analog votage read. */
unix_guru 0:eaea12966d34 60 resistance = (float) SERIESRESISTOR / ((65536.0 / a) - 1);
unix_guru 0:eaea12966d34 61 pc.printf("Resistance for Thermistor = %f\r\n",resistance);
unix_guru 0:eaea12966d34 62
unix_guru 0:eaea12966d34 63 steinhart = resistance / THERMISTORNOMINAL; // (R/Ro)
unix_guru 0:eaea12966d34 64 steinhart = log(steinhart); // ln(R/Ro)
unix_guru 0:eaea12966d34 65 steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
unix_guru 0:eaea12966d34 66 steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
unix_guru 0:eaea12966d34 67 steinhart = 1.0 / steinhart; // Invert
unix_guru 0:eaea12966d34 68 temperature = steinhart - 273.15; // convert to C
unix_guru 0:eaea12966d34 69
unix_guru 0:eaea12966d34 70 return temperature;
unix_guru 0:eaea12966d34 71 }
unix_guru 0:eaea12966d34 72
unix_guru 0:eaea12966d34 73
unix_guru 0:eaea12966d34 74
unix_guru 0:eaea12966d34 75
unix_guru 0:eaea12966d34 76 int main()
unix_guru 0:eaea12966d34 77 {
unix_guru 0:eaea12966d34 78 pc.baud(115200);
unix_guru 0:eaea12966d34 79 pc.printf("\r\nThermistor Test - Build " __DATE__ " " __TIME__ "\r\n");
unix_guru 0:eaea12966d34 80
unix_guru 0:eaea12966d34 81 while(1) {
unix_guru 0:eaea12966d34 82 pc.printf("Temperature %f *C\r\n",get_temperature());
unix_guru 0:eaea12966d34 83
unix_guru 0:eaea12966d34 84 wait(.5);
unix_guru 0:eaea12966d34 85 }
unix_guru 0:eaea12966d34 86 }