I was looking for a free K-type thermocouple linearization and cold
junction compensation code online and couldn’t find it, so I wrote one.
It’s on my github.

There are some libraries available that use polynomial based
linearization, but those are not really suitable for embedded
applications as floating point operations and math functions take a lot
of program space. Besides, it’s possible to achieve the same or better
accuracy using a hand-picked lookup table with less code.

The idea is like this - pick up some points and interpolate the unknown
values linearly.

Points do not have to be distributed evenly. One can put more points in
more nonlinear parts of the response one is trying to approximate. I’ve
chosen 65 points and shuffled them around to get the minimal error. Data
was taken from K-thermocouple tables provided by
NIST.

I’ve used fixed point arithmetics for all the calculations. This makes
the library lightweight when compared to polynomial-based libraries that
use floating point operations.

On the plots below you can see a characterization of errors introduced
by the algorithm. The temperature conversion accuracy is typically less
than 0.06 degrees C for a K-type thermocouple. My library produces
slightly better results with less code.

The code currently does not support negative ambient temperatures, it’s
work in progress. I’m open for comments and questions. Ping me if you
find it useful.