Escolar Documentos
Profissional Documentos
Cultura Documentos
html
Introduction.
This discussion deals with the use of a 10K negative temperature coefficient (NTC) thermistor to measure and
calculate temperature. I have specifically focused on the Vishay / BC Components 2381-640-66103 bead type
thermistor with axial leads and 2381-633-83103 glass thermistor with radial leads. However, the general
approach may be used for any NTC thermistors.
Note that Digikey stocks these as Digikey part numbers BC1482 and BC1491. These are 2322 devices as
opposed to 2381. I am uncertain if the 2381 devices may be new devices which have yet to make it into the
supply chain. However, the thermal characteristics are identical.
NTC thermistors are relatively inexpensive, stable and even with a 10-bit A/D converter are capable of resolving
temperature to within 0.1 degrees C over most of the temperature range. The disadvantage is the calculation of
temperature is beyond the capability of many processors.
In this discussion, I focus on how the calculation may be quite accurately performed with an abbreviated table
lookup and interpolation. In this example, less than 256 reference points (bytes) are used with an added
interpolation of four points between each reference. The result is a resolution of nominally 0.1 Degees C over
the range of 0.0 to 37.0 degrees C and 0.2 degrees over the range of -20.0 to 65 degrees C. My feeling is that this
table lookup approach is as good as using a processor which has floating point calculations and can handle the
log function and raise numbers to powers and it uses a tiny fraction of the program memory.
Note that the number of reference points might be reduced to nominally 128, 64, 32 or even 16 reference points
admittedly with some loss of accuracy.
Details
+5 VDC
|
R1 = 10.0K
|
---------------------------------- PICAXE A/D input
| +
10K NTC thermistor Vtherm
| -
GRD
When using a ten bit A/D, the voltage Rtherm is calculated as;
(2) Vtherm = ADVal / 1024 * +5 VDC
where R1 is 10.0K.
https://www.phanderson.com/picaxe/picaxe_thermistor.html 1/9
1/30/2018 https://www.phanderson.com/picaxe/picaxe_thermistor.html
Note that for an 8-bit A/D, there are 256 quantizing bands and the above expression is;
(4) Rtherm = R1 / (256 / ADVal - 1.0)
The temperature in degrees K is then calculated using a four point model as;
(5) Tkelvin = 1.0 / (A_const + B_const * ln(Rtherm/10.0e3)
+ C_const * ln(Rtherm/10.0e3)^2 + D_const * ln(Rtherm/10.0)^3)
Note that for each value of ADVal, other than zero, there is a corresponding temperature and thus, one
implementation might be a 1024 word (2 byte) lookup table. That is, the value of temperature for each of the
1024 values of ADVal. This is well beyond the capability of any PICAXE.
I used Microsoft Excel to calculate the Tcelcius at 256 points. A portion of the spreadsheet is shown below. The
complete spreadsheet is shown here.
ADVal Rtherm Tkelvin Tcelc Diff Diff * 100
https://www.phanderson.com/picaxe/picaxe_thermistor.html 2/9
1/30/2018 https://www.phanderson.com/picaxe/picaxe_thermistor.html
The first column is the the values of ADVal. Column B are the corresponding values of Rtherm which are
calulated using equation (4). The Tkelvin is calculated using (5) with values of constants noted below. (More on
this later).
A_const = 3.354016e-3
B_const = 2.569107e-4
C_const = 2.626311e-6
D_const = 0.675278e-7
This in itself, might be adequate in many situations. However, a byte is capable of storing unsigned values in the
range of 0 - 255 or signed values of -127 to +128. It is indeed workable, but limits the temperature to one degree
of resolution. I was looking for something better.
Thus, in the next column, I calculated the difference between two adjacent cells in the Tcelcius column and in
the last column I multiplied this by 100.
Thus, if the ADVal is 2, the corresponding Tcelcius_100 (100 times Tcelcius) is 22699. If the ADVal is 3, the
Tcelcius_100 is 22699 - 3789. If the ADVal is 6, the corresponding Tcelcius_100 is 22699 - 3789 - 1984 - 1319 -
978. That is, the Tcelcius_100 for any ADVAL may be calculated using a table consisting of the last column.
Well, there is more complexity as an unsigned byte is limited to values in the range of 0 to 255, not such values
as 3789, 1984, etc.
However, scroll down to an ADVal of 15, where the temperature, Tcelcius_100 is 10542. If the ADVal is 16, the
Tcelcius_100 is 10542 - 254. If the ADVal is 20, the Tcelcius_100 is 10542 - 254 - 236 - 220 - 194. Note that
values such as 254, 236, etc may be stored in a byte.
Thus the Diff * 100 values in the last column over ADVals of 16 to 251 may be arranged in a table. If the ADVal
is less than 15, the temperature is too hot. If the ADVal is above 251, the temperature is too cold. Otherwise, the
start temperature is 10542 minus each value in the table until the ADVal is reached.
Program Therm1.Bas
' Therm1.Bas - PICAXE-18X
'
' Illustrates an interface with an NTC thermistor.
'
' Measures voltage across the thermistor and uses a combination of table lookup
' and linear interpolation to calculate the temperature in degrees C.
'
' copyright, Peter H Anderson, Baltimore, MD, June, '05
Symbol ADVal = W0
Symbol TC_100 = W0 ' ADVal not needed when TC_100 is calculated
Symbol ADValHi8 = B2
Symbol ADValLow2 = B3
Symbol N = B4
https://www.phanderson.com/picaxe/picaxe_thermistor.html 3/9
1/30/2018 https://www.phanderson.com/picaxe/picaxe_thermistor.html
Symbol Diff = B5
Symbol Whole = B6
Symbol Fract = B7
Symbol SignFlag = B4 ' N is not need when this is used
Symbol Dig = B5 ' Diff not needed when this is used
Top:
GoSub MeasTemp
If TC_100 = $7fff Then OutofRange
GoSub DisplayTemp
Top1:
Wait 5 ' perform measurement every five seconds
GoTo Top
OutofRange:
SerTxD ("Out of Range", 10, 13)
GoTo Top1
MeasTemp:
ReadADC10 0, ADVal
ADValHi8 = ADVal / 4 ' isolate the high 8 bits
ADValLow2 = ADVal & $03 ' low two bits
N = N + 1
Read N, Diff
Diff = Diff / 4 * ADValLow2
MeasTemp_1:
Return
TooHot:
TooCold:
TC_100 = $7fff
GoTo MeasTemp_1
DisplayTemp:
SignFlag = Tc_100 / 256 / 128
If SignFlag = 0 Then Positive
TC_100 = TC_100 ^ $ffff + 1 ' twos comp
SerTxD ("-")
Positive:
Dig = Fract / 10
SerTxD (#Dig)
Dig = Fract % 10
SerTxD (#Dig, 10, 13)
Return
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
' EEPROM locations 0 - 15 not used
EEPROM 16, (254, 236, 220, 206, 194, 183, 173, 164)
EEPROM 24, (157, 149, 143, 137, 131, 126, 122, 117)
EEPROM 32, (113, 110, 106, 103, 100, 97, 94, 92)
EEPROM 40, (89, 87, 85, 83, 81, 79, 77, 76)
EEPROM 48, (74, 73, 71, 70, 69, 67, 66, 65)
EEPROM 56, (64, 63, 62, 61, 60, 59, 58, 57)
EEPROM 64, (57, 56, 55, 54, 54, 53, 52, 52)
EEPROM 72, (51, 51, 50, 49, 49, 48, 48, 47)
EEPROM 80, (47, 46, 46, 46, 45, 45, 44, 44)
EEPROM 88, (44, 43, 43, 43, 42, 42, 42, 41)
EEPROM 96, (41, 41, 41, 40, 40, 40, 40, 39)
EEPROM 104, (39, 39, 39, 39, 38, 38, 38, 38)
EEPROM 112, (38, 38, 37, 37, 37, 37, 37, 37)
EEPROM 120, (37, 36, 36, 36, 36, 36, 36, 36)
EEPROM 128, (36, 36, 36, 36, 36, 35, 35, 35)
EEPROM 136, (35, 35, 35, 35, 35, 35, 35, 35)
EEPROM 144, (35, 35, 35, 35, 35, 35, 35, 35)
EEPROM 152, (35, 35, 35, 35, 35, 35, 35, 35)
EEPROM 160, (36, 36, 36, 36, 36, 36, 36, 36)
EEPROM 168, (36, 36, 36, 37, 37, 37, 37, 37)
EEPROM 176, (37, 37, 38, 38, 38, 38, 38, 39)
EEPROM 184, (39, 39, 39, 39, 40, 40, 40, 41)
EEPROM 192, (41, 41, 42, 42, 42, 43, 43, 43)
EEPROM 200, (44, 44, 45, 45, 46, 46, 47, 47)
EEPROM 208, (48, 48, 49, 50, 50, 51, 52, 53)
EEPROM 216, (53, 54, 55, 56, 57, 58, 59, 61)
EEPROM 224, (62, 63, 65, 66, 68, 70, 72, 74)
EEPROM 232, (76, 78, 81, 84, 87, 90, 94, 98)
EEPROM 240, (102, 107, 113, 119, 126, 135, 144, 156)
EEPROM 248, (170, 187, 208, 235)
Calibration.
In the above, the temperature corresponding to an ADVal of 15 is 105.42 (or Tcelcius_100 = 10542). This will
result in a temperature of 25.00 (or Tcelcius_100 of 2500) at an ADVAL of 512, or at 128 in the above table.
This is for an "ideal" thermistor.
Assume the above program is run in an environment where the temperature is 25.30 degrees C, but the reported
temperature is 22.36 degrees. It is a simple matter to add 2530 - 2236 = 294 to the initial 10542 value. For
example;
TC_100 = 10542 + 294
Program Therm2.Bas.
This program illustrates a four channel temperature measurement system using four 10K - 10K thermistor
divider networks on A/D channels 0, 1, 6 and 7. Note that in the example, I have used different calibration
values for each of the thermistors.
https://www.phanderson.com/picaxe/picaxe_thermistor.html 5/9
1/30/2018 https://www.phanderson.com/picaxe/picaxe_thermistor.html
In addition, I have implemented a four channel thermostat using outputs 0, 1, 2 and 3. Note that this would be
somewaht more complex for negative temperatures and trip points.
' Therm2.Bas - PICAXE-18X
'
' Four channel temperature measurement using 10K NTC thermistors on A/D 0, 1, 6, 7.
'
' Measures voltage across the thermistor and uses a combination of table lookup
' and linear interpolation to calculate the temperature in degrees C.
'
' Sends temperature to PC (or similar).
'
' For each channel, operates or releases relays, or similar, on OUT0, 1, 2 or 3. Note that
' this may be used to turn on a fan (hot alarm) or turn on a heater (cold alarm).
'
' Uses about 400 bytes of 2048 available.
'
' copyright, Peter H Anderson, Baltimore, MD, June, '05
Symbol ADVal = W0
Symbol TC_100 = W0 ' ADVal not needed when TC_100 is calculated
Symbol Channel = B2
Symbol ADValHi8 = B3
Symbol ADValLow2 = B4
Symbol N = B5
Symbol Diff = B6
Symbol Whole = B7
Symbol Fract = B8
Symbol SignFlag = B5 ' N is not need when this is used
Symbol Dig = B6 ' Diff not needed when this is used
Top:
For Channel = 0 to 3
GoSub MeasTemp
GoSub DisplayTemp
GoSub ThermostatSet
Next
Wait 5
GoTo Top
MeasTemp:
Branch Channel, (MeasTempCh0, MeasTempCh1, MeasTempCh2, MeasTempCh3)
MeasTempCh0:
ReadADC10 0, ADVal
ADValHi8 = ADVal / 4 ' isolate the high 8 bits
ADValLow2 = ADVal & $03 ' low two bits
TC_100 = 10542 + 234' adjust this as required. Note this varies from channel to channel
If ADValHi8 < 16 Then TooHot
If ADValHi8 > 251 Then TooCold
Goto MeasTempCalculate
MeasTempCh1:
ReadADC10 1, ADVal
ADValHi8 = ADVal / 4 ' isolate the high 8 bits
ADValLow2 = ADVal & $03 ' low two bits
https://www.phanderson.com/picaxe/picaxe_thermistor.html 6/9
1/30/2018 https://www.phanderson.com/picaxe/picaxe_thermistor.html
TC_100 = 10542 - 123' adjust this as required. Note this varies from channel to channel
If ADValHi8 < 16 Then TooHot
If ADValHi8 > 251 Then TooCold
Goto MeasTempCalculate
MeasTempCh2:
TC_100 = 10542 + 321' adjust this as required. Note this varies from channel to channel
If ADValHi8 < 16 Then TooHot
If ADValHi8 > 251 Then TooCold
Goto MeasTempCalculate
MeasTempCh3:
TC_100 = 10542 - 123' adjust this as required. Note this varies from channel to channel
If ADValHi8 < 16 Then TooHot
If ADValHi8 > 251 Then TooCold
Goto MeasTempCalculate
MeasTempCalculate:
N = N + 1
Read N, Diff
Diff = Diff / 4 * ADValLow2
MeasTemp_1:
Return
TooHot:
TooCold:
TC_100 = $7fff
GoTo MeasTemp_1
DisplayTemp:
SerTxD (#Channel, " ");
If TC_100 = $7fff Then DisplayTempOutofRange
SignFlag = Tc_100 / 256 / 128
If SignFlag = 0 Then DisplayTempPositive
TC_100 = TC_100 ^ $ffff + 1 ' twos comp
SerTxD ("-")
DisplayTempPositive:
DisplayTempOutofRange:
SerTxD ("Out of Range", 13, 10)
Goto DisplayTempReturn
DisplayTempReturn:
Return
ThermostatSet:
ThermoStatSetCh0:
If TC_100 > 2900 Then TurnOn ' Hot alarm 29.00, Off at 27.00
If TC_100 < 2700 Then TurnOff
Goto ThermoStatSetReturn
ThermoStatSetCh1:
If TC_100 > 3200 Then TurnOn ' Hot alarm 32.00. Off at 27.00
If TC_100 < 2700 Then TurnOff
Goto ThermoStatSetReturn
ThermoStatSetCh2:
If TC_100 < 400 Then TurnOn ' Cold alarm 4.00. Off at 10.00
If TC_100 > 1000 Then TurnOff
Goto ThermoStatSetReturn
ThermoStatSetCh3:
If TC_100 < 2500 Then TurnOn ' Cold alarm 25.00. Off at 27.00
If TC_100 > 2700 Then TurnOff
Goto ThermoStatSetReturn
TurnOn:
High Channel
Goto ThermoStatSetReturn
TurnOff:
Low Channel
Goto ThermoStatSetReturn
ThermoStatSetReturn:
Return
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
' EEPROM locations 0 - 15 not used
EEPROM 16, (254, 236, 220, 206, 194, 183, 173, 164)
EEPROM 24, (157, 149, 143, 137, 131, 126, 122, 117)
EEPROM 32, (113, 110, 106, 103, 100, 97, 94, 92)
EEPROM 40, (89, 87, 85, 83, 81, 79, 77, 76)
EEPROM 48, (74, 73, 71, 70, 69, 67, 66, 65)
EEPROM 56, (64, 63, 62, 61, 60, 59, 58, 57)
https://www.phanderson.com/picaxe/picaxe_thermistor.html 8/9
1/30/2018 https://www.phanderson.com/picaxe/picaxe_thermistor.html
EEPROM 64, (57, 56, 55, 54, 54, 53, 52, 52)
EEPROM 72, (51, 51, 50, 49, 49, 48, 48, 47)
EEPROM 80, (47, 46, 46, 46, 45, 45, 44, 44)
EEPROM 88, (44, 43, 43, 43, 42, 42, 42, 41)
EEPROM 96, (41, 41, 41, 40, 40, 40, 40, 39)
EEPROM 104, (39, 39, 39, 39, 38, 38, 38, 38)
EEPROM 112, (38, 38, 37, 37, 37, 37, 37, 37)
EEPROM 120, (37, 36, 36, 36, 36, 36, 36, 36)
EEPROM 128, (36, 36, 36, 36, 36, 35, 35, 35)
EEPROM 136, (35, 35, 35, 35, 35, 35, 35, 35)
EEPROM 144, (35, 35, 35, 35, 35, 35, 35, 35)
EEPROM 152, (35, 35, 35, 35, 35, 35, 35, 35)
EEPROM 160, (36, 36, 36, 36, 36, 36, 36, 36)
EEPROM 168, (36, 36, 36, 37, 37, 37, 37, 37)
EEPROM 176, (37, 37, 38, 38, 38, 38, 38, 39)
EEPROM 184, (39, 39, 39, 39, 40, 40, 40, 41)
EEPROM 192, (41, 41, 42, 42, 42, 43, 43, 43)
EEPROM 200, (44, 44, 45, 45, 46, 46, 47, 47)
EEPROM 208, (48, 48, 49, 50, 50, 51, 52, 53)
EEPROM 216, (53, 54, 55, 56, 57, 58, 59, 61)
EEPROM 224, (62, 63, 65, 66, 68, 70, 72, 74)
EEPROM 232, (76, 78, 81, 84, 87, 90, 94, 98)
EEPROM 240, (102, 107, 113, 119, 126, 135, 144, 156)
EEPROM 248, (170, 187, 208, 235)
https://www.phanderson.com/picaxe/picaxe_thermistor.html 9/9