# External Reference (AREF) pin in Particle Photon

Hi,

I have a little problem with AREF pin.
As I’m also looked at the previous Topics on this matter, I can’t find any solution to my problem.
[1]

[2]

Below is my code in Particle Web IDE.

``````#include "Particle.h"
#include <math.h>

#define THERMISTORPIN A0
#define LM35 A1
#define NUMSAMPLES 10
#define SERIESRESISTOR 30000
#define A  0.0008265222525
#define B  0.0002221033477
#define C  0.00000009841282328

int samples[NUMSAMPLES];

void setup() {
Particle.publish("Getting Temperature....");

}

void loop() {
int i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
delay(50);
}

// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
average = 1024 / (average - 1);
average = SERIESRESISTOR / average;

float steinhart1, steinhart2, steinhart3, steinhart4, steinhart5;

steinhart1 = log(average);
steinhart2 =  C * steinhart1 * steinhart1 * steinhart1;
steinhart3 = (B * steinhart1) + steinhart2;  // B*(ln R) + C * (ln R)***
steinhart4 = 1.0 / (A + steinhart3); // 1/(A + B*(ln R) + C * (ln R)***)
steinhart5 = (steinhart4 - 273.15);                         // convert to C
Particle.publish("Body Temperature", String(steinhart5));
delay(1000);
}
``````

I’m using Thermistor 503ET-3H. The result when I using the above code without AREF is like below:

The value should be in the range of room temperature, which is (25C - 26C).
I always using below code with AREF when using Arduino and the temperature measurement is good.

``````void setup()
{
Serial.begin(9600);
Serial.println("Getting Temperature....");
analogReference(EXTERNAL);
...
...
...
}
``````

And I know from the other discussion topic on this matter stated that Particle does not need AREF.

Below I also attach the connection I’ve been build.

So, without AREF what can I do to make the thermistor measurement is correct?

I hope for discussion on this matter.
Thank you.

I am curious where you obtained the Steinhart-Hart coefficients etc. I would suspect the math rather than a hardware problem for the moment. You might get a few clues by recording all raw and final values while taping the sensor to a glass of ice water, then after holding it in your finger. That way you can see what all the numbers are doing at the 0-40C boundaries.

1 Like

Not having looked into the code very well, did you consider that the analog ports have a higher resolution on the Photon than they do on most arduinos? 4096 VS 1024? (I’m seeing the latter in your code, hence the question)
The Photon also runs on 3.3V, rather than 5V, should that make any difference.

1 Like

The reference voltage of these devices is exactly the voltage that can be measured on the 3v3 pin.
Which in real life should be close enough to 3.3V. It may vary a few mV due to temperature and other factors which could be incorporated into the conversion, but that would also apply with an AREF pin (but isn’t, so the influence will probably be negligible).

So most likely your greatly wrong readings come from the wrong formula used here - the biggest issue is the division by 1024 (error factor 4 - as it should be 4096) propbably followed by the obscured reference voltage which may well be render the constants in the calculation wrong.

BTW, when you say this

What is the reference voltage you apply to VREF. Just taking the external reference without actually knowing what it is and just banging in some constants of “obscure” origin (which may well contain the reference voltage in some “unknown” way) will prevent an informed decission or successful debugging.

2 Likes

It is actually exits you know. Here I give you the link.

Besides, I also using the datasheet given by the SEMITEC Corporation for the thermistor sensor.
All of my calculation have been approved by my professor. So, the calculation for steinhart is correct.
Plus, the calculation has been test using Arduino Mega.

The equation undoubtedly exists, what we’re wondering is, if those values are correct when you’re using it with a Photon.

That may very well be, but a Mega isn’t a Photon, and they operate differently. If you’ve determined those values by using the Mega, why not recalculate them with a Photon, making sure you take its specs into consideration: pin resolution and voltages used.

1 Like

Actually, as I mentioned about I have looked at other topic regarding the same problem. Yes, I’ve found out about the value of 4096 as the analog to digital converter. Here is the result.

(I’m using the same code above)

Oh…
If that the case I’ve to run back many test just to get the reliable coefficient value of A, B, & C.

Is there any other way?

The steinhart formula for thermistor is correct.
Maybe, you mean the formula of (like below):

``````// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
average = 4096 / (average - 1);
average = SERIESRESISTOR / average;
``````

I’m just using the voltage divider formula. If there is a wrong in that calculation for Particle Photon, please advise me.

Since the part of the calculation you quoted does not take into account the reference voltage in any way, but the voltage intrinsicly is a factor of the result, the voltage must be part of the rest of the calculation.
But as it can’t be seen in itself it inevitable must be somehow mingled into the constants.

And for above reason you need to know what the Vref is to decide whether the constant ~3.3V of the Photon will work with these constants or not.

And following that reasoning I dare to contradict …

… in the way that the steps of the formular probably are correct, but the constants may or may not be.

BTW, you mentioned “voltage divider formula” but in your logic there is no voltage to be seen - that just doesn’t add up.

2 Likes

Yes I’m using the 3.3V pin. Because in the Arduino I also using the 3.3V pin regardless of the AREF pin.

In a simple word, can you suggest to me what I should do to get the correct value for my thermistor measurement?

I also have looked at the Particle library for thermistor code example. In the example, it using a beta steinhart calculation with only one coefficient value. And there is also an example that are using the three coefficient like I did. But, I can’t get it right.

Hope for constructive suggestion from the professional.
If it does not trouble you, I hope you can give a suggestion by point. So, it is easy for me and other beginners to get the point from this discussion.

Provided your Arduino setup also uses 3.3V as reference - as you stated - then it can be assumed that the constants still work on the Photon.
As you already corrected for the difference in ADC resolution your “voltage factor” (`x * 3.3V`) should now fit the rest of the calculation.

Depending on the deviation of your actual from the expected results additional measures may need to be taken due to the difference in nature of the ADCs used on Arduino and Photon.
e.g. decoupling caps, pull-resistors, impedance converter, …

This is a 50kohm at 25C thermistor, and you appear to be using 30kohm in the upper branch of the voltage divider, so at about 25C I would expect the voltage at the analog input to be 3.3V/80k*50k = 2.0625V or 2559 out of 4095 on the raw ADC value.

Can you confirm that the raw value is near 2559?

4 Likes

Yes, sir. I have tried to add it, and now I understand what you explain earlier.
The temperature value now is correct according to the temperature stated in my room thermometer. the values are the same.

Here is the code that I’ve corrected from your suggestion.

``````// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
average = 4096 / (average - 1);
average = (SERIESRESISTOR / average) * 3.3;
``````

Only the above code that change. The steinhart formula is still the same.

It works.
Thank you Sir ScruffR, and thank you all.

2 Likes

Yes, it is also written in the datasheet as you know.
But, after a several experiments conducted using the parameter given by the datasheet, the results shows that the 25C is at 30kOhm.
So, my supervisor told me to use the result output instate of following the datasheet.

One thing @bko tried to convey already may need stressing again - despite the fact that you might already have found a working solution.

The way the ADCs on the Particle devices work differs from the Arduino ADCs.
These devices use switched capacitor ADCs which need a signal source with relatively low output impedance in order to provide enough current to charge the input cap in a relatively short time.
And since your thermistor has a relatively high value, the input cap may not get fully charged in order to provide a reliable reading.

That’s also why I suggested this

3 Likes

I would see if the part is marked 303 since they make the thermistor in many ranges including 30k.

2 Likes