-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
367f569
commit 338492d
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#ifndef __THERMISTOR_H__ | ||
#define __THERMISTOR_H__ | ||
|
||
#include "mbed.h" | ||
#include "mutexless_analog.h" | ||
|
||
/* | ||
Thermistor voltage reading is converted to temperature in a two step process | ||
1. Thermistor voltage must be converted into thermistor resistance. | ||
This is done by solving the voltage divider equation (VCC and other resistor R are | ||
fixed by hardware) for the thermistor voltage | ||
Measured Voltage = VCC * (Thermistor_Resistance) / (Thermistor_Resistance + R) | ||
2. Thermistor resistance is converted to temperature through a best fit | ||
log curve built using the specific thermistor model's electrical characteristics. | ||
This will involve a pair of magic numbers (mult_const and add_const) generated by | ||
fitting a curve to a series of (resistance, temperature) points provided by manufacturer | ||
Temperature = mult_const * log(Thermistor Resistance) + add_const | ||
*/ | ||
typedef struct Thermistor_Constants { | ||
float mult_const; | ||
float add_const; | ||
} Thermistor_Constants; | ||
|
||
|
||
const Thermistor_Constants NCP21XM472J03RA_Constants = { | ||
.mult_const = -27.06, | ||
.add_const = 253.11 | ||
}; | ||
|
||
|
||
class Thermistor { | ||
private: | ||
float mult_const; | ||
float add_const; | ||
float vcc; | ||
float R; | ||
AnalogInMutexless thermVoltPin; | ||
|
||
public: | ||
/* | ||
Constructor expects at least 2 arguments | ||
constants: Struct of log translation function constants from structs above | ||
therm_pin: Analog pin the thermistor is wired to | ||
Optional arguments | ||
R: Resistance of resistor in series with thermistor | ||
vcc: Total voltage across R and thermistor | ||
*/ | ||
Thermistor(const Thermistor_Constants constants, PinName therm_pin, float R = 4700, float vcc = 3.3); | ||
|
||
/* | ||
Retrives current thermistor reading | ||
*/ | ||
float get_temperature(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "thermistor.h" | ||
#include <math.h> | ||
|
||
/* | ||
Initialize all parameters and pin | ||
*/ | ||
Thermistor::Thermistor(const Thermistor_Constants constants, PinName therm_pin, float R, float vcc) : thermVoltPin(therm_pin) { | ||
this->mult_const = constants.mult_const; | ||
this->add_const = constants.add_const; | ||
this->vcc = vcc; | ||
this->R = R; | ||
} | ||
|
||
/* | ||
Retrive new temperature reading | ||
*/ | ||
float Thermistor::get_temperature() { | ||
float therm_voltage = thermVoltPin.read() * 3.3; | ||
float R_Therm = R / (vcc / therm_voltage - 1); | ||
|
||
return mult_const * std::log(R_Therm) + add_const; | ||
} |