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

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* FRDM-Thermistor-Demo
00002 This is a Thermistor to Temerature conversion demo 
00003 for the @NXP (@freescale) FRDM-K64F demo board
00004 Much thanks to @Adafruit for this tutorial:
00005 https://learn.adafruit.com/thermistor/using-a-thermistor
00006 
00007 The 100K Thermistor is configured with a 4.7k series resistor 
00008 tied to vcc (3.3v)  like this:
00009 
00010     +3.3v
00011       |
00012       \
00013       /  4.7k series resistor
00014       \
00015       /
00016       |
00017       .-----------O To Anlog pin on FRDM board
00018       |
00019       \
00020       /
00021   Thermistor  100k Nominal
00022       \
00023       /
00024       |
00025      ---
00026      GND
00027            
00028 
00029 */
00030  
00031 
00032 #include "mbed.h"
00033 
00034 #define THERMISTORNOMINAL 100000      // 100k 
00035 // temp. for nominal resistance (almost always 25 C)
00036 #define TEMPERATURENOMINAL 25   
00037 // The beta coefficient of the thermistor (usually 3000-4000)
00038 #define BCOEFFICIENT 3950
00039 // the value of the 'other' resistor
00040 #define SERIESRESISTOR 4700    
00041   
00042 AnalogIn Thermistor(A3);
00043 
00044 Serial pc(USBTX, USBRX);
00045 
00046 
00047 // This is the workhorse routine that calculates the temperature
00048 // using the Steinhart-Hart equation for thermistors
00049 // https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
00050 float get_temperature()
00051 {
00052     float temperature, resistance;
00053     float steinhart;
00054     int a;
00055     
00056     a = Thermistor.read_u16();       // Read 16bit Analog value
00057     pc.printf("Raw Analog Value for Thermistor = %d\r\n",a);
00058   
00059     /* Calculate the resistance of the thermistor from analog votage read. */
00060     resistance = (float) SERIESRESISTOR / ((65536.0 / a) - 1);
00061     pc.printf("Resistance for Thermistor = %f\r\n",resistance);
00062    
00063     steinhart = resistance / THERMISTORNOMINAL;         // (R/Ro)
00064     steinhart = log(steinhart);                         // ln(R/Ro)
00065     steinhart /= BCOEFFICIENT;                          // 1/B * ln(R/Ro)
00066     steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15);   // + (1/To)
00067     steinhart = 1.0 / steinhart;                        // Invert
00068     temperature = steinhart - 273.15;                              // convert to C
00069 
00070     return temperature;
00071 }
00072    
00073  
00074 
00075 
00076 int main()
00077 {
00078     pc.baud(115200);    
00079     pc.printf("\r\nThermistor Test - Build " __DATE__ " " __TIME__ "\r\n");
00080   
00081     while(1) {  
00082        pc.printf("Temperature %f *C\r\n",get_temperature()); 
00083 
00084        wait(.5); 
00085     }
00086 }