Engduino v2.1
|
00001 /** 00002 * \addtogroup EngduinoThermistor 00003 * 00004 * This is the driver code for reading temperature. This can 00005 * equally well be done in a sketch file, but it is also here 00006 * for completeness 00007 * 00008 * The Engduino has a Murata NCP18WF104J03RB 00009 * thermistor attached to analogue input A5. 00010 * This thermistor has a resistance of 100K at 25C. 00011 * 00012 * @{ 00013 */ 00014 00015 /** 00016 * \file 00017 * Engduino Thermistor driver 00018 * \author 00019 * Engduino team: support@engduino.org 00020 */ 00021 00022 #include "pins_arduino.h" 00023 #include "EngduinoThermistor.h" 00024 00025 /* 00026 * Initialise Class Variables 00027 */ 00028 00029 /*---------------------------------------------------------------------------*/ 00030 /** 00031 * \brief Constructor 00032 * 00033 * C++ constructor for this class. Empty. 00034 */ 00035 EngduinoThermistorClass::EngduinoThermistorClass() 00036 { 00037 } 00038 00039 /*---------------------------------------------------------------------------*/ 00040 /** 00041 * \brief begin function - must be called before using other functions 00042 * 00043 * Nothing to be done. 00044 */ 00045 void EngduinoThermistorClass::begin() 00046 { 00047 } 00048 00049 00050 /*---------------------------------------------------------------------------*/ 00051 /** 00052 * \brief end function - switch off the LEDs 00053 * 00054 * Nothing to be done. 00055 */ 00056 void EngduinoThermistorClass::end() 00057 { 00058 } 00059 00060 00061 /*---------------------------------------------------------------------------*/ 00062 /** 00063 * \brief Get the temperature. 00064 * \param units A choice between CELSIUS (default), KELVIN, or FAHRENHEIT 00065 * \return The temperature on the given scale. 00066 * 00067 * This function makes a call to the function that actually works out what 00068 * a particular resistance means. 00069 * 00070 * For the Murata NCP18WF104J03RB, the thermistor has a resistance of 100K at 00071 * 25C and a B value of 4250 and a 100K balance resistor. 00072 */ 00073 float EngduinoThermistorClass::temperature(temperatureUnits units) 00074 { 00075 // Parameters for the Murata NCP18WF104J03RB thermistor 00076 // 00077 return (temperature(units, 4250.0, 298.15, 100000.0, 100000.0)); 00078 } 00079 00080 /*---------------------------------------------------------------------------*/ 00081 /** 00082 * \brief Get the temperature. 00083 * \param units A choice between CELSIUS (default), KELVIN, or FAHRENHEIT 00084 * \param B Parameter in the B equation - see data sheet 00085 * \param T0 Temperature in Kelvin at which resistance is R0 - see data sheet 00086 * \param R0 Resistance at temperature T0 - see data sheet 00087 * \param R_Balance Value of the balance resistor in the potential divider (100K on Engduino v1.0) 00088 * \return The temperature on the given scale. 00089 * 00090 * This function measures the voltage and, as a consequence, calculates the 00091 * resistance of the thermistor. From this, we can figure out the actual 00092 * temperature 00093 * 00094 * NTC thermistors can be characterised with the B (or beta) parameter equation, 00095 * 00096 * 1/T = 1/T0 + 1/B*ln(R/R0) 00097 * 00098 * Where the temperatures are in Kelvin and R0 is the resistance at 00099 * temperature T0 (25 °C = 298.15 K). 00100 * 00101 * So far as the resistance is concerned, we have a potential divider with a balance 00102 * resistor of value R_Balance. Assuming that the resistance of the NTC is R and the 00103 * voltage across it is V, then Ohms law gives: 00104 * V/Vcc = R/(R + R_Balance) 00105 * 00106 * Given that V/Vcc is just (analog in)/1024 for a 10 bit ADC, we can work out that: 00107 * R = (R_Balance * analogIn) / (1024 - analogIn) 00108 * 00109 */ 00110 float EngduinoThermistorClass::temperature(temperatureUnits units, float B, float T0, float R0, float R_Balance) 00111 { 00112 float R, T; 00113 float analogIn = analogRead(A5); // Measure the voltage across the NTC. 00114 00115 R = (R_Balance * analogIn) / (1024.0 - analogIn); // Resistance calculated from potential divider 00116 T = 1.0 / (1.0/T0 + (1.0/B)*log(R/R0)); // Temperature in Kelvin given by B equation 00117 00118 00119 switch (units) { 00120 case CELSIUS: 00121 T -= 273.15; 00122 break; 00123 case FAHRENHEIT : 00124 T = (T-273.15)*1.8 + 32.0; 00125 break; 00126 default: 00127 break; 00128 }; 00129 00130 return T; 00131 } 00132 00133 00134 /** 00135 * \brief Get the raw potential divider reading. 00136 * \return Raw potential divider reading. 00137 * 00138 * Raw voltage reading from NTC potential divider circuit 00139 * 00140 */ 00141 uint16_t EngduinoThermistorClass::temperatureRaw() 00142 { 00143 return(analogRead(A5)); 00144 } 00145 00146 00147 /*---------------------------------------------------------------------------*/ 00148 /* 00149 * Preinstantiate Objects 00150 */ 00151 EngduinoThermistorClass EngduinoThermistor = EngduinoThermistorClass(); 00152 00153 /** @} */