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 »

Some findings, adding a 220 uF capacitor at the 5v supply before the LDO (it is the usb from PC actually)


LMT86 to stm32f401 adc pa0 (no Op Amp)
LMT86 - stm32f401 adc pa0 - 220uF cap at 5v
LMT86 - stm32f401 adc pa0 - 220uF cap at 5v
histwe220u.png (5.73 KiB) Viewed 3884 times
count 2000.000000
mean 30.809150
std 2.734337
min 5.710000
median 30.600000
max 54.450000

Next, I set up a filter to remove all values outside +/- 2 deg C from the median, 147 outlier values removed.
This is significantly less than previous

LMT86 - stm32f401 adc pa0 - removing 147 outliers
LMT86 - stm32f401 adc pa0 - 220uF cap at 5v remove 147 outliers
LMT86 - stm32f401 adc pa0 - 220uF cap at 5v remove 147 outliers
histwe220uf.png (12.27 KiB) Viewed 3884 times
count 1853.000000
mean 30.639255
std 0.589653
min 28.660000
median 30.600000
max 32.580000

220 uF is a little extreme, but it did reduce the count of outliers outside +/- 2 deg C significantly

Unfortunately, the range of that outliers did not reduce significantly. It seemed there is a possibility the ADC is picking up signals from sources other than the sensor alone. Though at the pin, only the sensor is connected (across a wire to the probe).
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

This is a data analysis post. I used the Anderson–Darling test to test for fit to normal distribution. After removing the outliers
https://en.wikipedia.org/wiki/Anderson% ... rling_test
https://docs.scipy.org/doc/scipy/refere ... erson.html

Code: Select all

n = 2000, 220uf
no fil
338.0688373916323 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 5 del 80
37.65825596079503 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 3 del 106
16.639158835365834 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 2 del 147
6.330279064999559 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 1.5 del 184
5.565211685938493 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]
count    1816.000000
mean       30.629251
std         0.540934
min        29.100000
median     30.600000
max        32.070000

tol = 1 del 311
9.129102640304609 [0.575 0.654 0.785 0.916 1.089] [15.  10.   5.   2.5  1. ]

tol = 0.5 del 908
12.947477639850604 [0.574 0.654 0.784 0.915 1.088] [15.  10.   5.   2.5  1. ]

----
n = 2000, lmv358 op amp
no fil
286.11314396470016 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 5 del 246
49.1890842386847 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 3 del 404
21.53082374676842 [0.575 0.654 0.785 0.916 1.089] [15.  10.   5.   2.5  1. ]

tol = 2 del 555
6.184812017836293 [0.574 0.654 0.785 0.915 1.089] [15.  10.   5.   2.5  1. ]

tol = 1.5 del 643
2.5784588330257066 [0.574 0.654 0.785 0.915 1.089] [15.  10.   5.   2.5  1. ]
count    1357.000000
mean       30.939005
std         0.605743
min        29.480000
median     30.890000
max        32.430000

tol = 1 del 781
4.116979988109961 [0.574 0.654 0.784 0.915 1.088] [15.  10.   5.   2.5  1. ]

tol = 0.5 del 1203
11.753081703721705 [0.573 0.653 0.783 0.913 1.087] [15.  10.   5.   2.5  1. ]
---
n = 2000, lmt86 direct
no fil
192.2082880456942 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 5 del 254
64.04789448918518 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 3 del 380
28.67826080132636 [0.575 0.654 0.785 0.916 1.089] [15.  10.   5.   2.5  1. ]

tol = 2 del 500
9.98471395356546 [0.574 0.654 0.785 0.916 1.089] [15.  10.   5.   2.5  1. ]

tol = 1.5 del 588
3.7181488303444894 [0.574 0.654 0.785 0.915 1.089] [15.  10.   5.   2.5  1. ]
count    1412.000000
mean       30.791516
std         0.567874
min        29.330000
25%        30.450000
50%        30.810000
75%        31.110000
max        32.290000

tol = 1 del 705
4.255539391071579 [0.574 0.654 0.785 0.915 1.089] [15.  10.   5.   2.5  1. ]

tol = 0.5 del 1203
9.760407662442503 [0.573 0.653 0.783 0.914 1.087] [15.  10.   5.   2.5  1. ]
This result is important. The Anderson-Darling statistics started high and reduce to the smallest after values outside +/- 1.5 deg C from the median is removed. Below that, the statistic increase again showing a poorer fit. So +/- 1.5 deg C is the best fit. This is based on 2000 samples and a static (room) temperature.

After removing outliers beyond 1.5 deg C,
with the 220 uF cap at 5v, standard deviation is 0.540934, 184 values removed
with the Op-Amp (no cap), standard deviation is 0.605743, 643 values removed
LMT86 direct (no cap, no Op-Amp) standard deviation is 0.567874 588, values removed

The graphs is like such:
LMT86 to stm32f401 ADC direct after removing 588 outliers.
LMT86 to stm32f401 ADC direct after removing 588 outliers.
histwef2-1.5c.png (14.17 KiB) Viewed 3881 times
95% confidence interval is about 1.96, so that gives an accuracy of +/- 1.11 deg C at 95% confidence interval measured at the ADC !
If we simply take the +/- 1.5 deg C range, that is 2.64 sigma, this is pretty much 99% confidence interval
https://en.wikipedia.org/wiki/Normal_di ... e_function

Then we go back to the specs for LMT86, which provides -10.9 mV/C.
This means the stm32f401 ADC under test has an accuracy of about +/- 12 mV at 95% confidence interval and +/- 16 mV at 99% confidence interval ;)
But these results are very stochastic, it takes lots of measurements and requires a static value to achieve such accuracy.
The 220 uF capacitor obviously made a difference, as only about 10% (184 values) are outside 1.5 deg C. Without the capacitor, no Op Amp, (30%) 588 values are outside +/- 1.5 C. The capacitor made a difference if smaller sample sizes are needed for averaging purposes, this may be particularly important if the temperature is changing rather than static.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: fake lm35dz temperature sensors

Post by mrburnette »

AN2834
https://www.st.com/resource/en/applicat ... ronics.pdf

Engineering is everything with ADC if best accuracy is demanded. It is a testament to the SAR implemented on-chip that the technology works as well as it does.
In applications involving analog-to-digital conversion, ADC accuracy has an impact on the
overall system quality and efficiency. To improve this accuracy, the errors associated with
the ADC and the parameters affecting them must be understood.
ADC accuracy does not only depend on ADC performance and features, but also on the
overall application design around the ADC.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

Thanks @mrburnette, identifying the noise sources is in progress, but it isn't easy

OK here another test with the 220 uF capacitor on 3.3v after LDO, LMT86 direct to STM32F401 ADC pa0, no Op Amp, 2000 samples, 10 samples per sec, 84 cycle sample time as is previous

LMT86 to STM32401 ADC direct, 220 uF on 3v3
LMT86 to STM32F401 ADC, 220 uF on 3.3v
LMT86 to STM32F401 ADC, 220 uF on 3.3v
histwe3v3.png (6.21 KiB) Viewed 3860 times
count 2000.000000
mean 30.730650
std 7.015711
min -176.190000
median 30.810000
max 56.960000

The above data after removing 193 values > 1.5 deg C from the median:

LMT86 to STM32F401 ADC, 220 uF on 3.3v
removed 193 values > +/- 1.5 deg C from median
LMT86 to STM32F401 ADC, 220 uF on 3.3v removed 193 values > 1.5 deg C from median
LMT86 to STM32F401 ADC, 220 uF on 3.3v removed 193 values > 1.5 deg C from median
histwe3v3f.png (12.95 KiB) Viewed 3860 times
count 1807.000000
mean 30.796469
std 0.492922
min 29.330000
median 30.810000
max 32.290000

The count of outliers outside 1.5 deg C from median is comparable to that if 220 uF is placed before LDO at the 5v rail. 'Wild' outliers still occurs, there is one at -176 deg C (that one has a reading about 2.124 V), temperature measured is room temperature, values closer to room temperature 30 deg C is about 1.776 V. (edit: there is a bug for this value kind of 2.2v should be about 0 deg C as that's the spec for LMT86, Newton's method isn't a most stable solution search algorithm. But that this is still pretty much an outlier. 2.124 - 1.776 = 0.348 v, so using 10.9mV/C as an estimate that makes for a 31.9 deg C difference)

Oh, but the standard deviation after removing the outliers is the smallest so far at 0.49, so 95% confidence interval is +/- 0.96 deg C. at 1.5 deg C based on the filter, that is > 99% confidence interval at > 3 sigma.

It'd take a while to figure out where else are the noise sources, I'd guess. One of those tests I intend to do is to run it on batteries, that would take some work. Currently, it is pushing the values over usb-serial connection to the pc.

But 2 insights
1) That LDO (on stm32f401 black pill) is too underpowered, a bigger LDO is needed, and a big cap there makes a difference :lol:
2) Newton's method is not safe for 'mission critical' stuff, it may not converge. In this case that -176 deg C outlier, I made the algorithm terminate at 200 loops to prevent infinite loops. But as it goes, if it doesn't converge, 'wild' values may evolve.

Edit:
That 'noise' as it turn out is radio waves ! And I thought I'm measuring temperature. There is a cord/wire connecting the sensor/probe about 70 cm, it so happened that is good enough to be an antenna and stm32 ADC picked up the signals !

500 k samples per sec stm32f401 ADC pa0
Image
more in the thread
https://community.st.com/s/question/0D5 ... -stm32f401
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: fake lm35dz temperature sensors

Post by dannyf »

My experience with most 10bit ADC is that the ADC noisy is about 8 lsb. So one of the posts you showed earlier is correct.

I often use a filter on that:

Var' := alpha x Var' + (1-alpha) x Vart

Where Var' is the smoothed out version of the observed variable Vart.

It's implementation in integer math requires some forsight.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

Thanks @dannyf, there are a few things.

That Newton–Raphson iteration to get temperature for millivolts is not stable on LMT86, it may not converge. At the moment, in my own copy, I terminate the loop if it exceeds a certain number of iterations without converging. But this gives wildly wrong values, those values near absolute zero are the bugs.

These findings of *radio waves* is a surprise, and thanks for the suggestion, I'd think that filtering works. It is basically a low pass filter done digitally.
I've simulated this with the values I've got, they work OK, but extreme values, outliers can pull it out if the averaging weight is too large. So I'd need to fix that Newton's iteration first. I'd think this would be the same if I used an RC filter, but that I've not studied it more yet.

For exponential averaging weight I'd normally use
alpha = 2 / N + 1
Then the iterations goes as
s(0) = first value.
s(t) = alpha x next_value + (1 - alpha) x s(t-1).
s(t) becomes the exponential moving average. Choosing a small N, high cut-off frequency, the average tends to move faster with the change in values. This may be important, say in a 3d printer hot end where during heat up temperatures rise rapidly. However, this would make it susceptible to extreme values and outliers, which can throw the measured temperature out quite a bit. If I choose a large N, they tend to need to be pretty large say like 50 or even 100, then the exponential average moves very slowly, by which time the actual temperatures could actually be much higher than the computed average.

I think this is the same for physical RC low pass filters, but I've not explored it. It seems to be about choosing the appropriate low pass cut-off frequency so that those 'radio waves' generated outliers can be filtered out.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: fake lm35dz temperature sensors

Post by mrburnette »

ag123 wrote: Tue Sep 21, 2021 4:04 pm Thanks @mrburnette, identifying the noise sources is in progress, but it isn't easy
...
If it were easy, you would have posted a definitive answer :D

Fact is, we live in a noisy world: AC, universal motors, neighbors with arc welders, induction stoves, wireless surveillance cameras, digital watches, cellphones, fluorescent lightning, OTA television, HAM radio, traffic radar ... lots of broadband interferences.

But your posts are very informative.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

This 'radio waves on ADC' surprise is interesting because I've wanted to make an SDR (software defined radio). I'd never know that a temperature sensor does that in a way it isn't intended to. :lol:
More tests are still needed to confirm if indeed it is radio waves received.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

Here is an update, I patched a LMT86 directly on a breadboard - no long wires / no cords / no op amps etc.

2000 samples, LMT86 direct to STM32F401 ADC PA0
LMT86 direct to STM32F401 ADC PA0
LMT86 direct to STM32F401 ADC PA0
histlmt86.png (6.57 KiB) Viewed 3804 times
LMT86 direct to STM32F401 ADC PA0 discarding 122 values outside 0.5 deg C range from median, this is barely around 5% of 2000 samples.
LMT86 direct to STM32F401 ADC PA0 discarding 122 values
LMT86 direct to STM32F401 ADC PA0 discarding 122 values
histlmt86f.png (14.99 KiB) Viewed 3802 times
count 1878.000000
mean 31.195000
std 0.162021
min 30.740000
25% 31.110000
median 31.180000
75% 31.330000
max 31.620000

This result is astonishing, standard deviation is 0.162 deg C. 95% confidence interval is 1.959 sigma ~ 0.31736.
Now LMT86 measured on stm32f401 ADC pa0 is +/- 0.31736 deg C accurate at 95% confidence interval.
Then taking the full 0.5 deg C interval, that is 3.08 sigma, that is > 99.8% (accurate) confidence interval
https://en.wikipedia.org/wiki/Normal_di ... e_function

LMT86 is an accurate device, and it meets the specs published by TI, when a near noiseless measurement on stm32f401 ADC is possible.
The histogram is showing bars for temperatures well within 0.1 of a degree C. And I'm using only 12 bins for 2000 samples.
Working this back to the specs for LMT86, it is -10.9 mV average gain per deg C.
This translates to a +/-3.46 mV accuracy measured on STM32F401 ADC at 95% confidence interval.
And at 0.5 deg C at 3.08 sigma (> 99.8% confidence interval) it is +/- 5.45 mV at pretty much > 99.8% confidence interval measured on stm32f401 ADC.

Measurements are still stochastic, as I can observe varying numbers with each sample. But the samples and mean conforms well to a bell curve (normal distribution), after dropping the *outliers*.

Still can't rule out radio interference etc as noise sources which probably cause the variations and especially the outliers.

The data analysis looks like such
each line of these is the Anderson–Darling statistics
https://en.wikipedia.org/wiki/Anderson% ... rling_test
https://docs.scipy.org/doc/scipy/refere ... erson.html
This analysis is done by rejecting values outside a tolerance starting with 2 deg C and reducing it.
The Anderson–Darling statistic reduces to the smallest and finally increase, showing a poorer fit as tolerance is narrowed.
Now the tightest fit to normal distribution is 0.5 deg C at 99.8% confidence interval

Code: Select all

n = 2000, lmt86 direct - no cord
no fil
145.97371710597236 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 2 del 13
95.23121766618078 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 1.5 del 29
59.96725246650158 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 1 del 56
33.59035490934821 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]

tol = 0.5 del 122
20.692436455313555 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]
count    1878.000000
mean       31.195000
std         0.162021
min        30.740000
25%        31.110000
50%        31.180000
75%        31.330000
max        31.620000

tol = 0.3 del 197
22.802556095985665 [0.575 0.655 0.785 0.916 1.09 ] [15.  10.   5.   2.5  1. ]
This kind of also confirms the 'radio waves' interference hypothesis, that those outliers, and variances are due to interferences from radio waves.
But that'd need 1 more set of 'scope' charts, to verify it.
Last edited by ag123 on Wed Sep 22, 2021 5:50 pm, edited 4 times in total.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: fake lm35dz temperature sensors

Post by ag123 »

IMHO, this is ideal to use as a thermocouple cold junction compensation (i.e. measure the temperature of the cold junction, normally at room temperature), as the room temperature performance is precise. But that it is necessary to have the cold junction close to the stm32 ADC, and there needs to be pretty much as little *radio* interference / noise as is possible to achieve such performance.
Post Reply

Return to “Off topic”