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().
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;
}
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?