Tuesday, February 12, 2013

Thermistor Calculations

So this post is more of a maths orientated post. It incorporates maths equations powered by mathjax and so needs a relatively modern browser to work. Mathjax is a tad slow to load as its hosted on their site as I can't add the code to Blogger.

So I've decided I would test out some thermistors that I had bought a while ago. I was looking round and found it hard to find a solid circuit diagram and I couldn't find any equation to get from the ADC value to the temperature. I eventually had the great idea of checking how the RAMPS circuit was done for the RepRap System.

This lead me too the following circuit.
The resistance of the thermistor in the calculations will be referred to as Rt. One last thing to be wary of is that the internal pull up shouldn't be used as its resistance is unknown, the manual for the AVR for instance gives a minimum and maximum value but not a typical value.

So the first thing to do for our calculations is to work out the instantaneous resistance of the thermistor. For the calculations, the capacitor can be ignored as we are calculating with DC voltages and the capacitor is solely for getting rid of noise in the signal (capacitors don't conduct DC current).

To work out the resistance we are going to use voltage divider therm and manipulate it to get the thermistor resistance, Rt, out the front. Here is a picture of our simplified circuit.


From the voltage divider circuit we can now get:

\[ \begin{aligned} V_{t} & ={VCC} \frac{R_{t}}{R_{1}+R_{t}} \\ \frac{V_{t}}{{VCC}} &=\frac{R_{t}}{R_{1}+R_{t}} \end{aligned} \]

Now we multiply both sides by the denominator on the right:

\[ R_{t}=\frac{V_{t}(R_{1}+R_{t})}{VCC} \]

Rearrange and expand:

\[ R_{t} \left(1- \frac{V_{t}}{VCC} \right) = \frac{R_{1} V{t}}{VCC} \]

Rearrange again to get only Rt on the left and we're done:

\[ R_{t}=\frac{R_{1} V_{t}}{VCC - V_{t}} \tag{1} \]

Next we put Vt in terms of the ADC value. The ADC value is simply a ratio as shown below:

\[ \begin{align*} \frac{V_{t}}{VCC}&=\frac{ADC}{ADC_{max}} \\ V_{t}&=\frac{ADC \times VCC}{ADC_{max}} \tag{2} \end{align*} \]

Substitute equation (2) into equation (1):

\[ R_{t}=\frac{R_1 \times ADC}{ADC_{max} - ADC} \tag{3} \]

Now we have the relationship between the resistance of the thermistor and the ADC value we can use the beta (B) parameter equation that is derived from Steinhart's equation:
\[ T_{Kelvin} = \frac{B}{\ln \left(\frac{R_{t}}{r_{\infty}} \right) } \tag{4} \\ \text{where: } r_{\infty} = R_{0} \times e^{\frac{-B}{T_{0}}} \]

We can now substitute our resistance relationship equation into the Steinhart equation to get our final equation for the relationship between temperature and the ADC value.
\[ T_{Kelvin} = \frac{B}{ \ln \left( \frac{R_{1} ADC}{(ADC_{max}-ADC)R_{0} e^{\frac{-B}{T_0}}} \right) } \tag{5} \]

R0  is the resistance of the thermistor at standard laboratory temperature, T0. For example I'm using a 100k Ohm thermistor, so R0 is 100000 and T0 is 298.15 K (25 degrees C). The temperatures are all in Kelvin so the final temperature has to have 273.15 taken off it to get to degrees C. ADC_max is the maximum value the ADC can be. So for a 10-bit ADC, the max value would be 1024 (2^10). B should be given in the data sheet of the thermistor.

As this equation is quite a hard equation to solve, I can understand why the firmware/software for RepRap all use lookup tables. When I started out, I didn't think this would be such a hard equation to solve (in terms of CPU cycles needed for a small microcontroller to solve). For this reason I'm going to do the same on for my software but I might make the intervals between entries in the table a bit smaller than that used in the RAMPS firmware for RepRap.

Hope that helps anyone who wants to understand how to wire up a thermistor and the equations to get from the ADC value on the microcontroller to temperature.

UPDATE: I've uploaded a spreadsheet with the equation set up. It includes a graph which facilitates in helping tune the R1 resistor value to get the best range of the desired temperatures. I will later be making a program with C that outputs the values into a table for easy copy to a C lookup table.

UPDATE 2: Well I previously stuffed up my calculations. I've fixed them now. I found this by having doing some tests with my thermistor and finding they weren't working!