fake lm35dz temperature sensors

Anything not related to STM32
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

STM32duino (official) core version for LMT86
https://www.ti.com/product/LMT86

Main changes are call analogReadResolution(12) in setup. We need all 12 bits.
I'm not too sure how to go about changing the ADC_SAMPLING_TIME, it seemed predefined.
Choosing a longer sampling time provides more stable values as these devices provides very little currents.

Code: Select all

#include <Arduino.h>
#include <math.h>

void sleep(uint16_t msecs);

void setup() {
	Serial.begin();
	pinMode(LED_BUILTIN, OUTPUT);

	// we need all 12 bits
	analogReadResolution(12);

	pinMode(PA0, INPUT_ANALOG);

}

float f(float t, float v) {
	return 1777.3F - (10.888*(t-30)) - 0.00347*(t-30.0F)*(t-30.0F) - v;
}

float df(float t) {
	return -10.888*t - 0.00347*2*(t-30.0F);
}

float nstep(float t, float v) {
	return t - f(t,v) / df(t);
}

#define TOLERANCE 0.005

float newton(float v) {
    float t0 = 30.0F;
    float t;
    uint16_t n = 0;
    while(true) {
        t = nstep(t0, v);
        n++;
        if(fabs(t-t0) < TOLERANCE)
            break;
        t0 = t;
    }
//    Serial.print("loop ");
//    Serial.println(n);
    return t;
}

void loop() {
	digitalToggle(LED_BUILTIN);
	Serial.print("pa0:");
	uint16_t val = analogRead(PA0);
	Serial.print(val);
	Serial.print(", ");
	float mv = 3.3 * (float) val * 1000.0 / 4096.0;
	Serial.print(mv);
	Serial.print("mV, ");
	float temp = newton(mv);
	Serial.print(temp);
	Serial.println(" deg C");

	sleep(1000);

}

void sleep(uint16_t msecs) {
	for(uint16_t i=0; i<=msecs; i++)
		asm("wfi");
}
Otherwise, it works pretty much the same (and just as fast as libmaple f4) on stm32f401cc blackpill.
Not quite a library as Newton's method may not converge.
But otherwise, it is fast converting volts to temperature and works well so far. The key is that FPU in stm32f4, this'd likely run just as well on those without FPU, but it's likely slower.

Edit:
I tried defining in my makefile
-DADC_SAMPLINGTIME=ADC_SAMPLETIME_84CYCLES

This works, it changes the sample time, but a rebuild is needed (at least including the analog.o object).
In Arduino IDE, the same can be achieved via minor edits in boards.txt or platform.txt.
Note that this codes works OK with the default sample time. i.e. a longer sample time may not be needed after all.
I'd guess due to the challenges of various different hardware, the core codes select a sample time with a set of defines.
That works well generally.
Last edited by ag123 on Wed Sep 15, 2021 1:56 pm, edited 2 times in total.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: fake lm35dz temperature sensors

Post by fpiSTM »

Thanks.
About ADC sampling it can be redefined using the build_opt.h:

https://github.com/stm32duino/Arduino_C ... pp#L33-L47

Simply defined ADC_SAMPLINGTIME with the value you want ;)
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

Hi all, in case you are reading this, there is something rather 'disturbing'.
LMT86 https://www.ti.com/product/LMT86
I'm seeing temperature values like this, it is room temperature and nothing changed:
600 values of temperature readings, 1 sample / s
histogram 600 values
histogram 600 values
hist.png (5 KiB) Viewed 3328 times
I've had to discard about 100 values > +/- 0.5 sigma (standard deviation) from the mean to get this
~500 values with values outside +/- 0.5 sigma removed
histogram removed values outside 0.5 sigma (~ 500 values)
histogram removed values outside 0.5 sigma (~ 500 values)
histfilter.png (13.6 KiB) Viewed 3328 times
now the mean temperature is 31.3 deg C, sigma 0.22
using 95% confidence interval about 2 sigma, this gives the temperature sensor accuracy about +/- 0.45 that is in line with the specs
https://www.ti.com/lit/gpn/lmt86
So the rest of wild fluctuations are likely coming from 'elsewhere'. I'm suspecting it to be that lousy LDO on the stm32f401 pill board.
It is powering both the f401, and ssd1306 LCD + this little temperature sensor.
Is that LDO the likely source of error? (i.e. not enough power)
Last edited by ag123 on Thu Sep 16, 2021 11:01 am, edited 1 time in total.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

Nope, not good, I powered the temperature sensor off 5v instead

600 values, room temperature, 1 sample / s
histogram 600 values
histogram 600 values
hist.png (4.43 KiB) Viewed 3328 times
remove 160 values outside +/- 0.5 sigma, ~ 440 values remaining
histogram removed values outside 0.5 sigma (~ 440 values)
histogram removed values outside 0.5 sigma (~ 440 values)
histfilter.png (12.55 KiB) Viewed 3328 times
mean 30.75, sigma 0.51
that gives 95% confidence interval at +/- 1 C at best, and that's after removing the 'outliers'
this result is rather poor, especially with the unfiltered results.
It is possibly not good enough for safety or accuracy critical use
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

I did some experiments, I used a 50k potentiometer tuned about mid-range as a voltage divider, so it is getting between 1.5-2v simulating the temperature sensor.

50k potentiometer, 1000 samples
50k resistor divider
50k resistor divider
hist50k.png (11.47 KiB) Viewed 3289 times
mean 55.338110
std 0.725853

Next I repeat the same with 5k potentiometer, 1000 samples.
5k resistor divider
5k resistor divider
hist5k.png (11.25 KiB) Viewed 3289 times
mean 24.093520
std 0.219107

Hence, the problem apparently is at the ADC end, an op amp is needed to get better accuracy.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

What is a little interesting is a large sample time 480 clocks max in the register causes more variance.
This is a little counterintuitive, but it means the noise is 'digital'. e.g. switching circuits on the SOC die probably changes the voltages measured in the ADC. This is likely, as mere millivolts variances changes the temperature readout quite a bit.

5k resistor divider, 480 clocks sample time, 1000 samples
5k resistor divider 480 clocks sample time
5k resistor divider 480 clocks sample time
hist5k480.png (13.27 KiB) Viewed 3287 times
mean 25.556960
std 1.608072

The histogram is noticeably not a normal distribution

it turns out minor jitter on the dupont pin contacts can cause wildly incorrect values
5k resistor divider 84clocks sample time 900 samples
5k resistor divider
5k resistor divider
hist5k-2a.png (8.56 KiB) Viewed 3276 times
This is a lot of trouble for measuring things 'accurately'
Last edited by ag123 on Sat Sep 18, 2021 6:53 am, edited 2 times in total.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: fake lm35dz temperature sensors

Post by mrburnette »

ag123 wrote: Fri Sep 17, 2021 11:51 am ...
This is a lot of trouble for measuring things 'accurately'
Which is why it is so important to pick the right component for the job at hand:

- Small range temperature accuracy: diode junction
- Room ambient temperature: thermistor
- Engine exhaust temperature: thermocouple + cold-junction compensation + transconductance amplifier
- Critical chemical reactions: Platinum RTD sensors

You and I do our homework which often means lot of PDF reading on the PC and often some analytical lab work. But, often after acquiring a part, we are surprised during POC testing!

I'm just writing here to tell the newcomers in the forum that there is no sure shortcut to research and component testing.

Thermocouples do not need to be expensive ... or even commercial "grade" to get fair results. 'T' extension wire is cheap for homemade test probes:
Thermocouple.PNG
Thermocouple.PNG (67.26 KiB) Viewed 3257 times
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

I've walked into a ditch that I seldom paid attention to, ADC noise on stm32 ADCs. The problem seemed worse on F4xx and higher devices vs STM32F103xx, This may be because of the sampling capacitance on F4 is a mere 4pf smaller than that on stm32f103 which has max 8pf spec.
For once, I'm having to deal with millivolts of variances that matters. Normally for 'small' signals, it is just amplifying them and make do, but temperature sensors needs both the range and mV precision.

What is observed in the graphs above is ADC sampling noise. As prior, that few resistor divider graphs are not even a temperature sensor. Those are just resistor dividers.

I've done further tests, this time it is the LMT86 with an LMV358 Op Amp as a buffer.

Those observations are further followed up here
https://forum.allaboutcircuits.com/thre ... st-1667837
and here
https://community.st.com/s/question/0D5 ... -stm32f401

I'm reposting the findings here as I've got some rather important results related to stm32
Last edited by ag123 on Tue Sep 21, 2021 11:01 am, edited 1 time in total.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

I've been messing around with a LMT86 temperature sensor

This sensor is deemed high output impedance as the documented loads it drives is 50uA.


ADC noise on STM32 isn't 'new'. Just that for once I'm observing such volatility up close. The problem is mere millivolts differences means a different temperature reading from the sensor, and accordingly there is already a sort of 'amplification' from the sensor itself.

Threads found from a Google search are like such.
https://embdev.net/topic/stm32f0-adc-in ... stigations
https://itectec.com/electrical/electron ... c-noise-2/
https://www.eevblog.com/forum/microcont ... adc-noise/
https://stackoverflow.com/questions/554 ... lues-stm32

Among the discussions is that given this is a high impedance source, an Op-Amp is needed to buffer inputs to the ADC.

So next I strapped up an LMV358 Op-Amp as a buffer and here are the observations. This is with the Op-Amp

2000 samples, 10 samples/s, measuring room temperature about 30.5-31 deg C
LMT86 to LM358 op amp to STM32F401 adc pa0 removing 555 outliers
LMT86 to LM358 op amp to STM32F401 adc pa0 removing 555 outliers
histwelmv358-2.png (5.54 KiB) Viewed 3183 times
count 2000.000000
mean 30.882000
std 6.782125
min -213.360000
median 30.960000
max 57.800000

No, temperature did not accidentally drop to near absolute zero. It is room temperature all the while during measurement, a multimeter did not change a single millivolt while variances are recorded by the ADC.

Next, I set up a filter to remove all values outside +/- 2 deg C from the median, 555 outlier values removed.
LMT86 to LM358 op amp to STM32F401 adc pa0
LMT86 to LM358 op amp to STM32F401 adc pa0
histwelmv358f2.png (13.43 KiB) Viewed 3183 times
count 1445.000000
mean 30.973156
std 0.733296
min 29.030000
median 30.890000
max 32.950000
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

Next remove that Op Amp and connect the sensor LMT86 directly to the ADC (on stm32f401)

2000 samples, 10 samples/s, measuring room temperature about 30.5-31 deg C
LMT86 - STM32f401 ADC pa0 direct
LMT86 - STM32f401 ADC pa0 direct
histwe2.png (5.6 KiB) Viewed 3181 times
count 2000.000000
mean 30.930100
std 4.062343
min 3.670000
median 30.810000
max 59.260000

Next, I set up a filter to remove all values outside +/- 2 deg C from the median, 500 outlier values removed.
LMT86 - STM32f401 ADC pa0 direct remove 500 outliers
LMT86 - STM32f401 ADC pa0 direct remove 500 outliers
histwef2.png (13.49 KiB) Viewed 3181 times
count 1500.000000
mean 30.817787
std 0.698166
min 28.810000
median 30.810000
max 32.800000

It is a dilemma kind of. The Op-Amp probably reduce the impedance (significantly?), but that the errors/variance are about the same !

What is more concerning is the large outlier values as those are observed in the unfiltered input. This didn't seem to go away when the OpAmp is placed as a buffer between the sensor to stm32f401.
Post Reply

Return to “Off topic”