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Ω.
1/R = 1/R1 + 1/R2
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 :-
from math import *
class Thermistor:
"Class to do the thermistor maths"
def __init__(self, r0, t0, beta, r1, r2):
self.r0 = r0 # stated resistance, e.g. 10K
self.t0 = t0 + 273.15 # temperature at stated resistance, e.g. 25C
self.beta = beta # stated beta, e.g. 3500
self.vadc = 5.0 # ADC reference
self.vcc = 5.0 # supply voltage to potential divider
self.vs = r1 * self.vcc / (r1 + r2) # effective bias voltage
self.rs = r1 * r2 / (r1 + r2) # effective bias impedance
self.k = r0 * exp(-beta / self.t0) # constant part of calculation
def temp(self,adc):
"Convert ADC reading into a temperature in Celcius"
v = adc * self.vadc / 1024 # convert the 10 bit ADC value to a voltage
r = self.rs * v / (self.vs - v) # resistance of thermistor
return (self.beta / log(r / self.k)) - 273.15 # temperature
def setting(self, t):
"Convert a temperature into a ADC value"
r = self.r0 * exp(self.beta * (1 / (t + 273.15) - 1 / self.t0)) # resistance of the thermistor
v = self.vs * r / (self.rs + r) # the voltage at the potential divider
return round(v / self.vadc * 1024) # the ADC reading