Thermocouple and RTD linearisation question

I am building a board with an ADC on it which is to measure these two sensor types.

The tricky bit is the linearisation.

All the equations I can find come in multiple parts.

coefficients below 0C and above 0C. But you don't know the temperature until you have evaluated the equation! So you could end up in a situation where the algorithm oscillates between the two equations.

Same with thermocouples, e.g. here

formatting link
although to make life even harder that one gives you the voltage in terms of temperature. I can see one could solve it iteratively. The ARM CPU I have has hardware floats and runs at 150MHz.

I have found some thermocouple equations which cover the whole range with one polynomial e.g. the NIST Polynomial Coefficients for Temperature-to-Voltage Conversion. However I have found these for only some of the eight thermocouple types. I am trying to support B E J K N R S T types.

How do instrument manufacturers deal with this? Do they store a lookup table and interpolate it?

I can see that with some effort one could write software (running on a PC) which takes in these equations and generates lookup tables for the whole lot.

Is there some easier way?

Maths is not my strong point although I can write C code to evaluate a polynomial.

Very many thanks for any tips.

Reply to
Peter
Loading thread data ...

On a sunny day (Wed, 02 Oct 2019 14:45:49 +0100) it happened Peter wrote in :

formatting link
formatting link
formatting link
formatting link
formatting link
formatting link

My C code:

formatting link
top link th-0.4.tgz th-0.4.lsm

Microchip PIC hardware implementaton:

formatting link

Works for me, but no other thing to compare at extremes.

Reply to
Jan Panteltje

I have generally use the NIST polynomials to generate lookup tables, which I stored in the product. Runtime is lookup+interpolation. With a modern ARM cpu with hardware float, you might consider executing the polynomials in real time.

An RTD is fairly linear to start, so a low order poly fixes that. Not CvanD.

This is all lookup tables and interpolation:

formatting link

I programmed that in 68K assembly, all fixed point math, no floats.

I did that in Power Basic.

You can also just look up the numbers in an Omega handbook to make the runtime lookup tables. A point every 10 deg C is probably good enough.

You could just brute-force the voltage-to-temp polynomials in double floats in c. That would run pretty fast on a decent uP. In real life, you don't need the sixteen or whatever terms that NIST uses; you can't measure voltage or resistance well enough to justify that.

$10 DVMs do it.

The big error source is usually the reference junction. That's tricky to measure right, and you need a reverse set of lookup tables to map Tj into the proper offset correction. Cheap instruments apply a linear correction at the ADC input, which isn't really right.

--
John Larkin         Highland Technology, Inc 

lunatic fringe electronics
 Click to see the full signature
Reply to
jlarkin

I have been reading about this, here

formatting link

They arrive at a 0.45C error between the two methods of doing CJC.

AFAICS the problem with the simple method gets worse the further the cold junction is from 0C, no?

It also ought to be possible to have an equation which takes in the thermocouple output voltage and the cold junction temperature and it gives you "exact" hot junction temperature. I guess this is the same thing as calculating the two polynomials; first you do the CJC one (temperature to equivalent voltage) and then you measure the thermocouple voltage, add the CJC voltage to it, and do the thermocouple polynomial on the result.

So I do need polynomials both ways, for all eight types...

Reply to
Peter

Most likely these days just store the coefficients.

There is a fairly simple trick to it in that most important calibration problems are fundamentally either linear or quadratic with a smallish zero offset and some extra nuisance non-linearity thrown in.

Depending on the range of temperatures your sensor is expected to encounter then you can choose the right coefficients. How many you need depends on how accurate you want the calibration to be.

You obtain a crude estimate of the right answer from the leading terms up to quadratic and then iterate using Newton-Raphson or if the non-linearity is quite small and you need to be very fast another polynomial recast in the form that takes a voltage in and returns a temperature.

Linear first guess would be T = (E-d0)/d1

Quadratic first guess would be T = 2(E-d0)/(d1+d1*sqrt(1-4*d2*(E-d0)))

  • be careful solving the quadratic or you end up with the tiny difference of two very large numbers and loss of precision.

** subject to typos

--
Regards, 
Martin Brown
Reply to
Martin Brown

This is brilliant stuff - thank you!

Are there tables for types B and N?

Reply to
Peter

I can find the polynomial coefficients for EJKRST here

formatting link

for both directions. I just need them for B and N.

I am trying to support the full documented temp range for each type.

However I have had no luck yet finding a *single* resistance to temperature equation for the RTD.

Tables can be found for all these so one could generate a lookup table.

I know that in principle one can generate a polynomial for almost any curve, and these curves being monotonic, it is even easier. If you want to fit 10 points exactly, you need a polynomial with 10 (11?) terms. How to do this, I don't know, but clearly it is well known.

Reply to
Peter

It won't hurt much if you use the high range polynomial for temperatures a little below zero or the low range one for room temperatures. It really matters which you use when you get to very hot or very cold. The two range calibration methods should agree pretty well in their overlap.

Providing a bit of hysteresis so you only swap to high range at +30C and to low range at -10C would be a reasonable compromise. I haven't checked what maximum error that would incur (you ought to though).

That is an unfortunate tendency of engineers to overfit their calibration data and get a polynomial that fits all their calibration points exactly and oscillates wildly at all points in between.

--
Regards, 
Martin Brown
Reply to
Martin Brown

Generally so. The usual assumption is to cancel the slope of the tc voltage around room temperature. Since thermocouples are nonlinear, the correction gets worse further away from room temp.

Plus, it's just hard to measure the reference junction temp accurately. We like a platinum RTD in an isothermal box.

The t/c voltage is not a function of the temp difference between the hot and cold junctions. So computing an equivalent ref junction temp doesn't help. You need to inverse compute the equivalent offset voltage of the t/c, add it to the measured t/c voltage, then do the nonlinear lookup.

Really, it's not bad.

I guess this is the same

Yes, that's right.

Yup!

Or lookup tables.

--
John Larkin         Highland Technology, Inc 

lunatic fringe electronics
 Click to see the full signature
Reply to
jlarkin

On a sunny day (Wed, 02 Oct 2019 16:57:57 +0100) it happened Peter wrote in :

Inferred from the above URL where only one character changes:

formatting link
formatting link

---------------------------------------------^ :-)

Reply to
Jan Panteltje

To curve fit, it's generally better to have a bunch more points than the degree of the polynomial. Given N points, you can draw a roller coaster curve that still hits them all.

Some polynomial regression algorithms produce radically dumb curves.

In real life, 3rd order mostly works.

--
John Larkin         Highland Technology, Inc 

lunatic fringe electronics
 Click to see the full signature
Reply to
jlarkin

If there was a way to do it that was adequate they would have done it.

There do tend to be two sorts of users. Those doing precision cryogenics and those doing furnace control - there isn't a lot of overlap needed.

It is essential to have plenty more points than free parameters!

You can generally resolve it by rescaling the problem so that the polynomial variable x is in the range -1 to 1. Otherwise the matrix you have to invert gets a terrible condition number and crazy results.

For equally spaced tabular data Chebeshev polynomials are handy for a quick and dirty fit. I do wonder how they arrived at some of the spline coefficients for that particular curve since it is not continuous at T90=0C ( d0 + b0.exp(b1*126.9686^2) != 0

The polynomial fit in Excel charts is remarkably good (apart from when they broke it in XL2007 briefly to make it agree with MATLAB). You just have to force the display equation format to show you enough digits!

Their other polynomial fit is dodgy. It is a particularly bad problem if you feed x values with a large DC offset into some fitting routines.

--
Regards, 
Martin Brown
Reply to
Martin Brown

Martin Brown wrote

It sounds like that for both RTDs and TCs I should use just the tables and interpolate them. Then you can have the whole range covered.

The max error is also easy to quantify.

It doesn't make sense to use polynomials for 6 of the TCs and table interpolation for the other 2.

Is there a way to generate a polynomial from one of these tables? Surely this must be a well worn challenge.

Reply to
Peter

Martin Brown wrote

formatting link

but they have created polynomials for different bits of the temperature ranges

Reply to
Peter

It doesn't need to be. If your case is always cool, the reference junction can be an insulated glob with a heater that always stays at one setpoint. That cuts out half the calculation overhead, and all it takes is a thermostat (very simple electronics).

It isn't a surface-mount off-the-shelf jellybean, though. And it takes a few seconds to come on-line.

Reply to
whit3rd

If you heat the reference junction, all the incoming thermocouple pair transitions to copper must be at the same temp. The heater creates thermal gradients.

Reply to
John Larkin

If you're using a polynomial regression (approximate fit) then that's true. If using interpolation (line goes through all data points) then it can get dumb.

Interpolation works well if your data is not noisy (i.e. when you're trying to simplify a complex curve to a similar simpler one). I've implemented interpolated cubic splines and they're very effective.

B-splines get used a lot in computer graphics because the derivatives are continuous (though the curves don't pass through the points so are less easy to predictably manipulate than interpolated splines). The functions yield curves that are very quick and easy for 3D graphic hardware to reproduce. Basically all modern car bodies are recognisable as modern precisely because they *all* use b-splines.

Either kind of spline will be continuous at/near all control points, but interpolated splines of order N have non-continuous N-1 derivatives (I think I stated that correctly).

They're both easier to fit and to evaluate than polynomials.

Clifford Heath.

Reply to
Clifford Heath

It's a smooth physical process, and there's nothing fundamental about

0 degrees C. One polynomial can cover the whole range.
Reply to
John Larkin

It's absolutely essential to have more points than parameters.

Since your N-points all have an additional bit of random error, it pays to have a lot more points than parameters, so the random errors have a chance to cancel out.

For my Ph.D. work, where I had to do non-linear multi-parameter curve fitti ng, the curve-fitting routine worked by generating a matrix of partial deri vatives of the least square error between the curve and the data for each p arameter, which it used to improve the parameter estimates from one pass th rough the data to the next, and you could also use that matrix to work out how closely the data defined the best-fit parameters after you had got a fi t.

If you tried to fit too many parameters (four in my case, rather than three ) the confidence limits got a whole lot worse on all of the parameters.

Some people who use curve fitting don't know what they are doing.

In John Larkin's experience, this may be true. It probably doesn't generali se.

--
Bill Sloman, Sydney
Reply to
Bill Sloman

There is if you're a fish.

--
  Rick C. 

  - Get 2,000 miles of free Supercharging 
 Click to see the full signature
Reply to
Rick C

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.