Skip to content

Commit

Permalink
Thermistor_Driver
Browse files Browse the repository at this point in the history
  • Loading branch information
ohjy1006kenneth authored and Wigu701 committed Nov 19, 2023
1 parent 367f569 commit 338492d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
61 changes: 61 additions & 0 deletions devices/include/thermistor.h
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
22 changes: 22 additions & 0 deletions devices/src/thermistor.cpp
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;
}

0 comments on commit 338492d

Please sign in to comment.