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.cpp Source File

LinearTemp.cpp

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 #include "LinearTemp.h"
00022 #include "mbed.h"
00023 
00024 // Constructor using two calibration points to define linear multiplier and offset
00025 LinearTemp::LinearTemp(PinName pin, float temp1, unsigned short read1, float temp2, unsigned short read2):_pin(pin) {
00026     _temp = 0.0;                                     // Zero the temperature
00027     _multiplier = (temp2 - temp1) / (read2 - read1); // Calculate multiplier as slope
00028     _offset = temp1 - (_multiplier * read1);         // Calculate offset
00029 }
00030 // Constructor using user defined multiplier and offset
00031 LinearTemp::LinearTemp(PinName pin, float multiplier, float offset):_pin(pin) {
00032     _temp = 0.0;                                     // Zero the temperature
00033     _multiplier = multiplier;                        // Set multiplier
00034     _offset = offset;                                // Set offset
00035 }
00036 
00037 // Populates an array with temperature readings, sorts, and returns median
00038 float LinearTemp::readTemp() {
00039     _tempArr[0] = _pin.read_u16();
00040     for (int i = 1; i < 9; i++) { 
00041         _tempArr[i] = _pin.read_u16();
00042         unsigned short tmp = _tempArr[i];
00043         int j;            
00044         for (j = i - 1; j >= 0; j--) {
00045             if (tmp>=_tempArr[j]) break;
00046             _tempArr[j + 1] = _tempArr[j];
00047         }
00048         _tempArr[j + 1] = tmp;
00049     }
00050     _temp = (_tempArr[4] * _multiplier) + _offset;
00051     return _temp;
00052 }
00053 // Returns last calculated temperature value
00054 float LinearTemp::getTemp() {
00055     return _temp;
00056 }