Red error blinking with thermsistor sketch [SOLVED]

So I adapted this meat-thermometer sketch from https://learn.adafruit.com/cloud-thermometer/hardware
But it won’t work. It says it compiles fine, but when I flash it, my photon keeps blinking red.

Any ideas?

Here’s the sketch:

#include <math.h>

int thermsistor_pin = A0;
int series_resistor = 10000;
int ADC_samples = 5;
int temperature = 0;
int analogvalue = 0;

float A = 0.000327262830;
float B = 0.000306674003;
float C = -0.000000246318;

typedef float (*TempConversion)(float);
TempConversion FromKelvin;
char* TempUnit;


void setup() {
    Serial.begin(9600);
}

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 = (1023 / reading) - 1;
    return series_resistor / reading;
}

float celsiusToKelvin(float celsius) {
    return celsius + 273.15; 
}

float readTemp() {
    float R = readResistance();
    float kelvin = 1.0/(A + B*log(R) + C*pow(log(R), 3.0));
    return kelvin;
}

/Christoffer

Hi @Boye

Is it possible that A0 is undriven or connected to GND? That would cause the ADC readings to be all zero and then in readResistance you would divide by zero, causing an error on a Photon or Core (but not on an Arduino ATMEL processor). Try changing this:

   float reading = 0.000001;  // was just 0

This code has some other problems like the 1023 (10-bit ADC) should be 4095 (12-bit ADC) for Particle devices. I don’t think you need to average 5 reading on a Particle device either–there is already an averaging that happens in analogRead().

1 Like

Hi @bko.

I fixed the what you mentioned, but it still blinked red, so I tried some troubleshooting by commenting out code.

It seems to be some sort of error with the temp conversion, as it worked after i commented out float temp = FromKelvin(readTemp()); and replaced it with float temp = readTemp();
Any ideas?

#include <math.h>

int thermsistor_pin = A0;
int series_resistor = 10000;
int temperature = 0;
int analogvalue = 0;

float A = 0.000327262830;
float B = 0.000306674003;
float C = -0.000000246318;

typedef float (*TempConversion)(float);
TempConversion FromKelvin;
char* TempUnit;

void setup() {
    Serial.begin(115200);
}

void loop() {
    //float temp = FromKelvin(readTemp());
    float temp = readTemp();
    Serial.print("Temperature: "); Serial.print(temp); Serial.print(" in "); Serial.println(TempUnit);
    delay(1000);
}


double readResistance() {
    float reading = 0.000001;
    reading = analogRead(thermsistor_pin);
    reading = (4095 / reading) - 1; //maybe comment out this line.
    return series_resistor / reading;
}


float celsiusToKelvin(float celsius) {
    return celsius + 273.15; 
}

float readTemp() {
    float R = readResistance();
    float kelvin = 1.0/(A + B*log(R) + C*pow(log(R), 3.0));
    return kelvin;
}

Where is the implementation and setup for your function FromKelvin()?

You’d need something like this


float yourFromKelvin(float kelvin)
{
  float someFactor;
  // calculate someFactor
  return kelvin * someFactor;
}

TempConversion FromKelvin = &yourFromKelvin;

to use the original code, but if you had the implementation already, why use a function pointer type in the first place?

And you are using an uninitialized char* TempUnit!

1 Like

Boy I have to learn to read more carefully! As @ScruffR said, these three lines are not helping you.

1 Like

@bko and @ScruffR, thanks for the help.

The code is now 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;
}

Does the Arduino system use +5V instead of +3.3V? That could account for the difference.

The Arduino has both 5V and 3.3V, but I remembered to use the right output.

This thread solved and the OP opened a new one, so closing this.