Reading a thermistor


#1

Hi,
I am using a thermistor as described in this project.
If I download the code (posted below) into a photon, I get a reading of 24 degrees celsius, which seems pretty close to my home temperature.

Now, I download this code into a Bluz DK and I get a reading of 80 degrees celsius. I’m using the exact same thermistor, resistor and connections as with the photon.
What could I be doing wrong? :astonished:

thanks
Gustavo.
PS: the code:

#include <math.h>
#include "application.h"

bool useFahrenheit = false;

// this is the thermistor used
// https://www.adafruit.com/products/372
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000

int THERMISTOR = A0;
float temperature;

double lastCheck = 0; //last time the sensor was read.  When starting, we assume it's closed
double now;
double checkPeriod = 5000;//polling period

void setup()
{

  pinMode(THERMISTOR, INPUT);

}

void loop()
{

  now = millis();
  if (now < lastCheck) {//handle the rollover
    lastCheck = 0;
  }
  if ((now-lastCheck)>checkPeriod) {
    lastCheck = now;

    if(Particle.connected()){
      if (!RGB.controlled()) {
        RGB.control(true);
        RGB.color(0,0,0);
       }
    }
     
    calculateTemperature();
  }
  
}

/*******************************************************************************
 * Function Name  : calculateTemperature
 * Description    : read the value of the thermistor and convert it to degrees
 * Return         : none
 *******************************************************************************/
void calculateTemperature()
{

  // read and convert the value to resistance
  float average = analogRead(THERMISTOR);
  average = (4095 / average) - 1;
  average = SERIESRESISTOR / average;

  float steinhart;
  steinhart = average / THERMISTORNOMINAL;          // (R/Ro)
  steinhart = log(steinhart);                       // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                        // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                      // Invert
  steinhart -= 273.15;                              // convert to C

  // Convert Celsius to Fahrenheit
  // source: http://playground.arduino.cc/ComponentLib/Thermistor2#TheSimpleCode
  if (useFahrenheit)
  {
    steinhart = (steinhart * 9.0) / 5.0 + 32.0;
  }

  //assign to global variable
  temperature = steinhart;

  Particle.publish("bleDK", "temperature: " + float2string(temperature) + "°C", 60, PRIVATE);
  
}

String float2string( float floatNumber ) {
  String stringNumber = String(floatNumber);

  //return only 2 decimals
  // Example: show 19.00 instead of 19.000000
  stringNumber = stringNumber.substring(0, stringNumber.length()-4);

  return stringNumber;
}

#2

Bluz has an ADC with 10 bits of precision while a Photon has one with 12 bits. So a Photon will give you a value between 0 and 4095 while bluz will give you a value between 0 and 1023.

So you need to change the 4095 to 1023 in this line:

average = (4095 / average ) - 1;


#3

darn, what a “little detail” I would have never looked at…
thanks, works great with that correction.
Gustavo.


#4

One of the fun little differences.

I added this to the docs as well, so there is a list of differences between the Photon/Core and bluz, this wasn’t on there. It is getting pushed to the staging docs server now and will be on the public docs next time we release: https://github.com/bluzDK/bluz-docs/commit/0a6d565567d7249bef0a9abb032f4c923f3bcc88