Very simple library for reading temperature from NTC thermistors

Dependents:   lightweight-weather-station

Committer:
programy1
Date:
Sun May 17 18:18:29 2020 +0000
Revision:
1:424e85f62cd8
Parent:
0:5459d9074b70
added comment for function documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
programy1 0:5459d9074b70 1 #include "mbed.h"
programy1 0:5459d9074b70 2 #include "thermistor.h"
programy1 0:5459d9074b70 3
programy1 0:5459d9074b70 4 Thermistor::Thermistor(PinName ThermistorPin, int NominalRes, int Beta, int SeriesResistor) : thermistorpin(ThermistorPin), nominalres(NominalRes), beta(Beta), seriesresistor(SeriesResistor) {
programy1 0:5459d9074b70 5 init();
programy1 0:5459d9074b70 6 }
programy1 0:5459d9074b70 7
programy1 0:5459d9074b70 8 void Thermistor::init() {
programy1 0:5459d9074b70 9 //defaults:
programy1 0:5459d9074b70 10 //beta = 3950 // The beta coefficient of the thermistor (usually 3000-4000)
programy1 0:5459d9074b70 11 //seriesresistor = 4700 //value of the second resistor - seriesresistor
programy1 0:5459d9074b70 12 }
programy1 0:5459d9074b70 13
programy1 0:5459d9074b70 14 void Thermistor::get_temperature() {
programy1 0:5459d9074b70 15 int temperaturenominal = 25; //temperature when the resistance is measured
programy1 0:5459d9074b70 16 a = thermistorpin.read_u16(); // Read 16bit Analog value
programy1 0:5459d9074b70 17 res = (float) seriesresistor / ((65536.0 / a) - 1); // get resistance of the thermistor
programy1 0:5459d9074b70 18 steinhart = res / nominalres; // (R/Ro)
programy1 0:5459d9074b70 19 steinhart = log(steinhart); // ln(R/Ro)
programy1 0:5459d9074b70 20 steinhart /= beta; // 1/B * ln(R/Ro)
programy1 0:5459d9074b70 21 steinhart += 1.0 / (temperaturenominal + 273.15); // + (1/To)
programy1 0:5459d9074b70 22 steinhart = 1.0 / steinhart; //invert
programy1 0:5459d9074b70 23 temp = steinhart - 273.15; // to celsius
programy1 0:5459d9074b70 24 }
programy1 0:5459d9074b70 25
programy1 0:5459d9074b70 26 float Thermistor::temperature() {
programy1 0:5459d9074b70 27 get_temperature();
programy1 0:5459d9074b70 28 return temp;
programy1 0:5459d9074b70 29 }
programy1 0:5459d9074b70 30
programy1 0:5459d9074b70 31 float Thermistor::resistance() {
programy1 0:5459d9074b70 32 get_temperature();
programy1 0:5459d9074b70 33 return res;
programy1 0:5459d9074b70 34 }