This library is designed to work with devices like the LM335 temperature sensor. There are only two requirements for compatibility: 1) The device must be a sensor device that has an analog voltage output. 2) The physical quantity measured must vary linearly with the analog voltage. The LM335 creates an analog voltage proportional to temperature. It can work very well with this library using the nominal multiplier of 0.0050354 and offset of -273.15 or by providing two user defined calibration points to the constructor. It uses a median filter to remove unwanted noise from ADC readings.

Dependents:   mbed_measuring_temperature

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LinearTemp.h Source File

LinearTemp.h

00001 /* Copyright (c) <2012> <P. Patel>, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018  
00019  // --------------------- Median Filtered Linear Temperature Sensor Reader ------------------------
00020  
00021 #ifndef LINEAR_TEMP
00022 #define LINEAR_TEMP
00023 
00024 #include "mbed.h"
00025 /** This library is designed to work with devices like the LM335 temperature sensor.  There are only
00026  * two requirements for compatibility of a device with this library: 1) It must be a sensor device
00027  * that has an analog voltage output. 2) The physical quantity measured must vary linearly with 
00028  * the analog voltage.  The LM335 which creates an analog voltage proportional to temperature
00029  * can work very well with this library using the nominal multiplier of 0.0050354 and offset of 
00030  * -273.15 or by providing two calibration points to the constuctor to generate a linear response.  It
00031  * samples the sensor 9 times and uses the median to minimize the effect of poor ADC readings and sudden
00032  * changes. 
00033  * 
00034  * Example:
00035  * @code
00036  * LinearTemp mysensor(p20, 0.0050354, -273.15);        // Setup a LM335 temp sensor on pin 20
00037  * DigitalOut myled(LED1);
00038  * 
00039  * int main() {
00040  *     while(1) {
00041  *         // Light LED if filtered temperature is greater than 45 degrees Celsius
00042  *         if (mysensor > 45) myled = 1;
00043  *         else myled = 0;
00044  *     }
00045  * }
00046  * @endcode
00047  */
00048 class LinearTemp {
00049 public:
00050     /** Create a calibrated temperature sensor object connected to an Analog input pin
00051      * @param pin Analog input pin to which an LM335 or comparable linear temp sensor is connected
00052      * @param temp1 Temperature (float in degrees C or F) of first calibration point
00053      * @param read1 Mbed ADC reading (unsigned short) of first calibration point
00054      * @param temp2 Temperature (float in degrees C or F) of second calibration point
00055      * @param read2 Mbed ADC reading (unsigned short) of second calibration point
00056      */
00057     LinearTemp(PinName pin, float temp1, unsigned short read1, float temp2, unsigned short read2);
00058     /** Create a calibrated temperature sensor object connected to an Analog input pin
00059      * @param pin Analog input pin to which an LM335 or comparable linear temp sensor is connected
00060      * @param multiplier Conversion multiplier to go from ADC reading as unsigned short to temperature (change in degrees / change in unsigned short)
00061      * @param offset Conversion offset (positive or negative)
00062      */
00063     LinearTemp(PinName pin, float multiplier,  float offset);
00064     /** Returns a new median-filtered, converted reading from the sensor
00065       * @returns New temperature (float) in degrees C or F as designated by the calibration points
00066       */
00067     float readTemp();
00068     /** Return the last calculated and stored temperature
00069       * @returns Last stored temperature (float) in degrees C or F as designated by the calibration points
00070       */
00071     float getTemp();
00072     #ifdef MBED_OPERATORS
00073     /** An operator shorthand for readTemp() to calculate filtered temperature
00074      */
00075     operator float() {
00076         return readTemp();
00077     }
00078     #endif
00079 private:
00080     AnalogIn _pin;
00081     unsigned short _tempArr[9];
00082     float _temp;
00083     float _multiplier;
00084     float _offset;
00085 };
00086 #endif