Tuesday, 9 October 2007

Measuring temperature the easy way

I have been asked for more details on my temperature measurement scheme so I have consolidated some of my previous articles :-

The objective is to measure temperature from room temperature to about 250°C using a thermistor. The thermistor resistance is a extremely non linear. It is approximated by a negative exponential of the reciprocal of absolute temperature. Ro is resistance at known temperature To, in this case 25°C, expressed in Kelvin. Beta is a second parameter of the thermistor which can be calculated if you know the resistance at two different temperatures or can be found on the data sheet.

The RepRap thermistor is an Epcos B57540G0103+, data sheet here. R25 is 10KΩ and beta is around 3500. Several values are given on the datasheet for different temperature ranges illustrating that the above equation is only an approximation. Here is a graph of its resistance against temperature :- This can be made more linear by putting a fixed resistors in parallel. The magic value to use appears to be the value of the thermistor at the middle of the temperature range. In this case it is about 470Ω. Here is the resulting combined resistance, the formula for two resistors in parallel is :-

1/R = 1/R1 + 1/R2

I.e. the total conductance is the sum of the two conductances. The resulting resistance is a lot more linear, however to measure temperature with an ADC we need a voltage rather than a resistance. This is easy, instead of wiring the resistor in parallel connect it in series to a voltage source equal to the full scale voltage of the ADC. The voltage across the thermistor is then :-

V = Vref . Rth / (R + Rth)

Here is a graph of the the output voltage when Vref is 5V. Note that the voltage decreases as the temperature rises. This could be inverted by swapping the resistor and thermistor but I prefer to keep one end of the thermistor at 0V so I can use single screened cable. It is also a good idea to put a capacitor across the ADC input to filter out any noise when using long leads like RepRap does. I used a 10uF tantalum bead.

Another consideration is how much power is dissipated in the thermistor as it will cause heating and alter the reading. The maximum dissipation will occur when its value equals the value of the resistor. At this point half the voltage is across the thermistor so the power dissipated in it is :-

P = (Vref / 2)2 / R

In the example above this works out at 13.3mW. The thermistor datasheet specifies a maximum of 18mW and a dissipation factor (in air) of 0.4 mW /K. I think this means that the temperature will rise by 33°C by self heating. The error would be less when not in air, but it is still perhaps a bit high. My system uses a Vref of 1.5 volts which, because it is a square law, only dissipates 1.2mW giving a 3°C rise at the mid range temperature in air.

For a 5V system is is probably worth sacrificing some of the ADC resolution to reduce the self heating error. This can be done by using two resistors :- The full scale voltage is now :-

Vfsd = Vref * R1 / (R1 + R2)

We also want the source impedance of this voltage, which is R1 in parallel with R2, to be 470Ω.

Solving these simultaneous equations gives :-

R1 = R / (1 - Vfsd / Vref)

R2 = R . R1 / (R1 - R)

So for Vfsd = 1.5V, Vref = 5V and R = 470:

R1 = 671Ω and R2 = 1569Ω, preferred values are 680 and 1K6.

And finally here is the Python code to work out the temperature :-
[null, {"document_url": "http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html", "unformatted_src": "http://2.bp.blogspot.com/_QYCIPYPZ-pc/Rr12OF1OGoI/AAAAAAAAAZ4/C2_qyWmxWoo/s400/equation.png", "src": "http://2.bp.blogspot.com/_QYCIPYPZ-pc/Rr12OF1OGoI/AAAAAAAAAZ4/C2_qyWmxWoo/s400/equation.png", "formatted_filename": "equation"}, null, {"document_url": "http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html", "unformatted_src": "http://1.bp.blogspot.com/_QYCIPYPZ-pc/RwvXqxXcO6I/AAAAAAAAAl8/OmH9AbL_Og0/s400/resistance+v+temp.png", "src": "http://1.bp.blogspot.com/_QYCIPYPZ-pc/RwvXqxXcO6I/AAAAAAAAAl8/OmH9AbL_Og0/s400/resistance+v+temp.png", "formatted_filename": "resistance v temp"}, null, {"document_url": "http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html", "unformatted_src": "http://4.bp.blogspot.com/_QYCIPYPZ-pc/RwvfmhXcO7I/AAAAAAAAAmE/ioRy1PhFtA0/s400/linearised.png", "src": "http://4.bp.blogspot.com/_QYCIPYPZ-pc/RwvfmhXcO7I/AAAAAAAAAmE/ioRy1PhFtA0/s400/linearised.png", "formatted_filename": "linearised"}, null, {"document_url": "http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html", "unformatted_src": "http://3.bp.blogspot.com/_QYCIPYPZ-pc/RwwO7BXcO8I/AAAAAAAAAmM/gfkhgjYxX_A/s400/series.png", "src": "http://3.bp.blogspot.com/_QYCIPYPZ-pc/RwwO7BXcO8I/AAAAAAAAAmM/gfkhgjYxX_A/s400/series.png", "formatted_filename": "series"}, null, {"document_url": "http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html", "unformatted_src": "http://2.bp.blogspot.com/_QYCIPYPZ-pc/RwwRUxXcO-I/AAAAAAAAAmc/KlxfiAuGYPU/s400/voltage.png", "src": "http://2.bp.blogspot.com/_QYCIPYPZ-pc/RwwRUxXcO-I/AAAAAAAAAmc/KlxfiAuGYPU/s400/voltage.png", "formatted_filename": "voltage"}, null, {"document_url": "http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html", "unformatted_src": "http://3.bp.blogspot.com/_QYCIPYPZ-pc/RwwXOBXcO_I/AAAAAAAAAmk/OMQlfgXe1xw/s400/divider.png", "src": "http://3.bp.blogspot.com/_QYCIPYPZ-pc/RwwXOBXcO_I/AAAAAAAAAmk/OMQlfgXe1xw/s400/divider.png", "formatted_filename": "divider"}, null, null, {"document_url": "http://hydraraptor.blogspot.com/2007/10/measuring-temperature-easy-way.html", "unformatted_src": "//lh5.googleusercontent.com/-yP0IlDbMxBg/AAAAAAAAAAI/AAAAAAAAGcM/hk9ONhrSae8/s80-c/photo.jpg", "src": "http://lh5.googleusercontent.com/-yP0IlDbMxBg/AAAAAAAAAAI/AAAAAAAAGcM/hk9ONhrSae8/s80-c/photo.jpg", "formatted_filename": "photo", "alt_text": "My photo", "rendered_width": 80, "rendered_height": 80}, null, null, null]