Very simple library for reading temperature from NTC thermistors

Dependents:   lightweight-weather-station

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thermistor.cpp Source File

thermistor.cpp

00001 #include "mbed.h"
00002 #include "thermistor.h"
00003  
00004 Thermistor::Thermistor (PinName ThermistorPin, int NominalRes, int Beta, int SeriesResistor) : thermistorpin(ThermistorPin), nominalres(NominalRes), beta(Beta), seriesresistor(SeriesResistor) {
00005     init();
00006 }
00007  
00008 void Thermistor::init() {
00009     //defaults:
00010     //beta = 3950 // The beta coefficient of the thermistor (usually 3000-4000)
00011     //seriesresistor = 4700 //value of the second resistor - seriesresistor
00012 }
00013  
00014 void Thermistor::get_temperature() {
00015     int temperaturenominal = 25; //temperature when the resistance is measured
00016     a = thermistorpin.read_u16(); // Read 16bit Analog value
00017     res = (float) seriesresistor / ((65536.0 / a) - 1); // get resistance of the thermistor
00018     steinhart = res / nominalres;  // (R/Ro)
00019     steinhart = log(steinhart);                 // ln(R/Ro)
00020     steinhart /= beta;                  // 1/B * ln(R/Ro)
00021     steinhart += 1.0 / (temperaturenominal + 273.15);   // + (1/To)
00022     steinhart = 1.0 / steinhart; //invert
00023     temp = steinhart - 273.15;           // to celsius
00024 }
00025  
00026 float Thermistor::temperature () {
00027     get_temperature();
00028     return temp;
00029 }
00030 
00031 float Thermistor::resistance () {
00032     get_temperature();
00033     return res;
00034 }