SparkFun Forums 

Where electronics enthusiasts find answers.

Topics pertaining to the Arduino Core & software used with the Artemis module and Artemis development boards.
User avatar
By cdollar
#209168
I have an artemis redboard with a normal thermistor connected to pin A0. I have no problems reading values from the ADC with analogRead(), but when I try to convert that ADC value to a temperature my sketch ultimately hangs when calling the log() function. The sketch is pretty simple (below). I've tried various things in the adcToTemp() method, such as using floats vs. double, etc., but the sketch will always hang when calling log(). You can also see where I added a call to log() in the sketch's setup method - that one works fine. I'm not sure what I'm missing here?
Code: Select all
#define thermistorAdcPin A0

void setup()
{
  Serial.begin(115200);
  delay(1000);

  float test = log(22579.617188);
  Serial.print("\nInitial float test: "); Serial.println(test);
}

double adcToTemp(int RawADC)
{
  long   Resistance;
  double Temp;

  Serial.print("\nConvert RAW: "); Serial.println(RawADC);

  Resistance = ((10240000 / RawADC) - 10000);
  Serial.print("Convert Resistance: "); Serial.println(Resistance);

  Temp = log(Resistance); // <--- this line causes the hang
  Serial.print("Convert Temp: "); Serial.println(Temp);
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;
  Temp = (Temp * 9.0) / 5.0 + 32.0;

  return Temp;
}

void loop()
{
  int adcTempVal = analogRead(thermistorAdcPin);

  Serial.print("Temp ADC Pin:   "); Serial.println(adcTempVal);
  Serial.print("Temp F:         "); Serial.println(adcToTemp(adcTempVal));
}

The output is:
Code: Select all
16:09:20.659 -> ⸮⸮
16:09:21.699 -> Initial float test: 10.02
16:09:21.699 -> Temp ADC Pin:   780
16:09:21.699 -> Temp F:         
16:09:21.699 -> Convert RAW: 780
16:09:21.699 -> Convert Resistance: 3128

Thanks for the help!
Chris
User avatar
By cdollar
#209702
Thanks for the reply.

I've tried both with and without explicitly including math.h with no change in behavior. And if that was the problem wouldn't I see the sketch hang in the setup() method where I seem to have no problem with calling log()?
By Valen
#209708
cdollar wrote: Mon Nov 18, 2019 9:14 am Thanks for the reply.

I've tried both with and without explicitly including math.h with no change in behavior. And if that was the problem wouldn't I see the sketch hang in the setup() method where I seem to have no problem with calling log()?
You've got a point there. Must be something else then. I did notice in Setup() you calculate the log function as:

float test = log(22579.617188);

22579.617188 is a floating point explicit value. So that is calculated without issues.

But in AdcToTemp you calculate it on a variable of type long, and store it as double. There may ba a type casting issue. try:

double Temp = log ( (double) Resistance);

This first converts the Resistance variable to type double, then execute the log function on it, and stores it as double.
 Topic permissions

You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum