Hi all. I made a precision bridge and instrumentation amplifier to read an RTD that's embedded in a product being tested. The full temperature range output available corresponds to 20-137C.

I predicted about 3 degrees nonlinearity (confirmed in calibration) and decided just to do a linear map like this:

``````    temp = map (analogRead(tempPin), 0, 1023, 20, 137);
``````

I've changed my mind and would like to account for the parabolic response of my circuit. I've thought of a couple of ways:

1: Make my temp variable a float and just calculate the temp directly from a curve fit between response temp and the analog input value (0-1023)

2: Break my temperature range into roughly linear segments and do an integer map depending on which range of the analog input it falls in.

1. Keep my temp variable an int but bias my map to reduce the mid range error (would result in about a 1 degree error) like this:
``````    temp = map (analogRead(tempPin), 0, 1023, 18, 135);
``````

Is there a more elegant way to do what I want?

Thanks for any suggestions.

EDIT: a 1 degree C error is acceptable

1. Scale by some amount (say 1000) and use fixed-point arithmetic (a modification of #1).

It really depends on what problem(s) you're trying to solve. If there's enough CPU time and Flash available, there is nothing wrong with #1.

"Piecewise linear fit" is very often how you do it.

however, depending on how much of your code memory space you are using, i'd be inclined to make a lookup table in program space and use that to map from the sensor to the temp readings.

I wasn't clear about why I wanted a better fit. This was my first Arduino project and I went overboard to avoid floating point math. The truth is that this is a very slow system with plenty of time and memory available.

The data is used for upper and lower bound limits and except for a faulty unit, will never be reached. I also count the number of complete heating/cooling cycles over a period of time so absolute accuracy is irrelevant for that function.

The reason for changing is that with all the care I took to get an accurate signal it seems a shame to not report the temperature as accurately as possible.

Thanks for the idea about scaling. That will keep my coefficients from being out in the fifth decimal place.