From 338492d64200280f97e9c286f3ef6b7c29b82758 Mon Sep 17 00:00:00 2001 From: Juyoung Date: Sun, 24 Sep 2023 11:46:00 -0500 Subject: [PATCH] Thermistor_Driver --- devices/include/thermistor.h | 61 ++++++++++++++++++++++++++++++++++++ devices/src/thermistor.cpp | 22 +++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 devices/include/thermistor.h create mode 100644 devices/src/thermistor.cpp diff --git a/devices/include/thermistor.h b/devices/include/thermistor.h new file mode 100644 index 0000000..89434cf --- /dev/null +++ b/devices/include/thermistor.h @@ -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 \ No newline at end of file diff --git a/devices/src/thermistor.cpp b/devices/src/thermistor.cpp new file mode 100644 index 0000000..27bb22a --- /dev/null +++ b/devices/src/thermistor.cpp @@ -0,0 +1,22 @@ +#include "thermistor.h" +#include + +/* + 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; +}