Last active
January 2, 2016 12:09
-
-
Save wgbartley/8301096 to your computer and use it in GitHub Desktop.
Basic class for reading thermistor values on a Spark Core
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
#include <math.h> | |
class Thermistor { | |
private: | |
int _pin; | |
int _resistor; | |
int _temp_raw; | |
float _temp_k; | |
float _temp_c; | |
float _temp_f; | |
public: | |
Thermistor(int pin, int resistor); | |
void begin(void); | |
float getTemp(void); | |
float getTempF(void); | |
float getTempC(void); | |
float getTempK(void); | |
int getTempRaw(void); | |
}; | |
Thermistor::Thermistor(int pin, int resistor) { | |
_pin = pin; | |
_resistor = resistor; | |
} | |
void Thermistor::begin(void) { | |
pinMode(_pin, INPUT); | |
} | |
int Thermistor::getTempRaw(void) { | |
delay(1); | |
return analogRead(_pin); | |
} | |
float Thermistor::getTempK() { | |
getTempRaw(); | |
_temp_k = log(((40960000/_temp_raw) - _resistor)); | |
_temp_k = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * _temp_k * _temp_k ))* _temp_k); | |
return _temp_k; | |
} | |
float Thermistor::getTempC() { | |
getTempK(); | |
_temp_c = _temp_k - 273.15; | |
return _temp_c; | |
} | |
float Thermistor::getTempF() { | |
getTempC(); | |
_temp_f = (_temp_c * 9.0)/ 5.0 + 32.0; | |
return _temp_f; | |
} | |
float Thermistor::getTemp() { | |
return getTempC(); | |
} | |
#define THERMPIN A0 // Spark Core analog input pin | |
#define THERMRES 10000 // Value of voltage divider resistor | |
Thermistor Thermistor(THERMPIN, THERMRES); | |
float t; | |
char str_t[7]; | |
void setup() { | |
Thermistor.begin(); | |
Spark.variable("t", &str_t, STRING); | |
} | |
void loop() { | |
t = Thermistor.getTempF(); | |
sprintf(str_t, "%.2f", t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment