Getting different readings after migrating sketch from Arduino to Photon [SOLVED]

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;
}

5V vs 3V3 ?

Nope. That is not the problem. I used 3V3 on the Arduino.

I closed your other thread since you started up over here.

What kind of thermistor is it? There are two common values, a 10k and 100k indicating their approximate resistance at room temp. The 10k is going to work fine on a Photon, but you will have trouble with the 100k due to the lower input impedance of the analog inputs on Photons.

If you connect just the thermistor to an ohm meter with no other connections, what does it read?

Unfortunately 130K. Are there any solutions?

Hi @Boye

Sure! Fix is easy–you just need to add one more component, an op-amp to buffer the signal. Check out this web page I found that has a nice picture of the op-amp follower that will allow high impedance sensors like yours.

http://www.electro-labs.com/temperature-sensors-types-and-applications/

Thank you @bko

How do I mark the question as “solved”?

Just click on the topic title, click edit (pencil icon) and add [SOLVED] at the end.
I’ll just do this for you.

1 Like