I have this code I migrated from Arduino to Photon. The code is working, but I’m getting some weird temperatures readings. My assumption is that the error lies in the conversion of resistance.
On my arduino I’m getting temperature around 25 celsius, and a resistance reading af 14700 after the conversion.
Arduino code:
double readResistance() {
float reading = 0;
for (int i = 0; i < ADC_SAMPLES; ++i) {
reading += analogRead(thermsistor_pin);
}
reading /= (float)ADC_SAMPLES;
reading = (1023 / reading) - 1;
return series_resistor / reading;
}
On the photon I’m getting a temperature around -178 celsius and a resistance of 107000 after conversion.
Photon code:
double readResistance() {
float reading = 0;
for (int i = 0; i < ADC_SAMPLES; ++i) {
reading += analogRead(thermsistor_pin);
}
reading /= (float)ADC_SAMPLES;
reading = (4096 / reading) - 1;
return series_resistor / reading;
}
Here’s the whole sketch. I have calculated my A, B and C coefficient using my arduino. If its not the conversion then perhaps its this that causes the temperature error?
#include <math.h>
int thermsistor_pin = A0;
int series_resistor = 10000;
int ADC_SAMPLES = 5;
float A = 0.064779992103;
float B = -0.010165034532;
float C = 0.000040883345;
// Temperature unit conversion functions and state.
typedef float (*TempConversion)(float);
TempConversion ToKelvin;
TempConversion FromKelvin;
char* TempUnit;
void setup() {
Serial.begin(115200);
ToKelvin = &celsiusToKelvin;
FromKelvin = &kelvinToCelsius;
TempUnit = "Celsius";
}
void loop() {
float temp = FromKelvin(readTemp());
Serial.print("Temperature: "); Serial.print(temp); Serial.print(" in "); Serial.println(TempUnit);
delay(1000);
}
double readResistance() {
float reading = 0;
for (int i = 0; i < ADC_SAMPLES; ++i) {
reading += analogRead(thermsistor_pin);
}
reading /= (float)ADC_SAMPLES;
reading = (4096 / reading) - 1;
return series_resistor / reading;
}
float celsiusToKelvin(float celsius) {
return celsius + 273.15;
}
float kelvinToCelsius(float kelvin) {
return kelvin - 273.15;
}
float readTemp() {
float R = readResistance();
float kelvin = 1.0/(A + B*log(R) + C*pow(log(R), 3.0));
return kelvin;
}