### Measuring Temperature using an Analog Devices TMP04

Peter H Anderson, Baltimore, MD, May, '04

Introduction.

This discussion focuses on interfacing the PICAXE with an Analog Devices TMP03 or TMP04 Temperature Sensor. These sensors provide a nominal 35 Hz pulse train where the ratio of the high and low times vary linearly as a function of temperature. They are available in three terminal packages from Digikey for about \$5.00.

The big advantage of the TMP04 is that it may be electrically isolated from the main processor circuitry using an optocoupler. Further, the output of the TMP04 may be interfaced with a pair of ADM485 (or similar) RS-485 transceivers to increase the range of reliable operation to several thousand feet.

Theory of Operation.

The TMP03/04 temperature sensor outputs a pulse train of nominally 35 pulses per second at 25 degrees C where the temperature is a ratio of the high time to the low time;

```

T_C = 235 - (400 * T1 / T2)
or
T_F = 455 - (720 * T1 / T2)
```
where T1 is the high time and T2 is the low time. It is important to note that the temperature is the ratio of the two quantities.

For 0.1 degree resolution the above may be expressed as;

```
T_C_10 = 2350 - (4000 * T1)/T2
or
T_F_10 = 4550 - (7200 * T1) / T2
```

T1 is relatively constant for a specific device and the specified maximum is 12 ms. T2 increases with temperature. Note that at 125 degrees C, the maximum value of T2 is 43 ms. These values dovetail nicely with the PICAXE PULSIN command where values in the range of 0 to 650 ms may be accommodated with a resolution of 0.00010 ms (10 us).

Program TMP04_1.Bas

Program TMP04_1.BS2 illustrates the simplicity of interfacing with the device. T1 and T2 are measured using the PULSIN command. Typical values are 1000 to 4300.

Note that in the above expression, multiplication of T1 by 7200 will result in a quantity of over 7.2 million, well beyond the capability of the 65535 which may be accommodated in a single 16-bit word. The PICAXE provides a relatively painless technique for performing the operation.

```   HiWord = 7200 ** T1  ' perform multiplication and place overflow (beyond 16-bits) in HiWord.
LoWord = 7200 * T1   ' perform multiplication and put low 16 bits in LoWord.
```
Thus, the result of the multiplication is stored in 32 bits, HiWord and LoWord.

Dividing this by T2 is a matter of repeatedly subtracting T2 (1500 to 4300) from the 32 bit quantity and counting the number of subtractions which occur before the 32-bit quantity "underflows".

This is performed in a two step process. T2 is subtracted from LoWord and if the result is greater than the original LoWord, an underflow occurred and HiWord is decremented. If the new HiWord is greater than the original, an underflow occurred and the division is complete.

```' TMP04_1.Bas
'
' TMP04                       PICAXE-18X
'
' Vout -----------------------> In0 (Term 17)
'
' Continually measures temperature in degrees F using a TMP04.
'
' The high time T1 and low time T2 are measured.  The temperature is then
' calculated using;
'
' TF_10 = 4550 - 7200 * T1 / T2
'
' Note that 7200 * T1 results in a 32 byte quantity.  The division by T2
' is performed using repeated subtraction.
'
' copyright, Peter H Anderson, May, '04

Symbol T1 = W0
Symbol T2 = W1

Symbol HiWordNew = W6
Symbol LoWordNew = W2

Symbol HiWord = W3
Symbol LoWord = W4

Symbol Q = W5
Symbol TF_10 = W5

Main:
Pause 7000	' time to open terminal when debugging
Main_1:
PulsIn 0, 1, T1   ' measure high time
Pulsin 0, 0, T2   ' measure low time

' SerTxD (#T1, "  ", #T2, 13, 10)	' used while debugging

If T1 < 50 Or T1 > 5000 Then Error	' if not in range
If T2 < 50 Or T2 > 5000 Then Error

HiWord = 7200 ** T1		' perfrom the multiplication - 32 bit quantity
LoWord = 7200 * T1

'SerTxD (#HiWord, "  ", #LoWord, 13, 10)  ' used for debugging

For Q = 0 to 60000		' now, divide by T2 using repeated subtraction
LoWordNew = LoWord - T2
If LoWordNew < LoWord Then NoBorrow

' else there was a borrow
HiWordNew = HiWord - 1
If HiWordNew > HiWord Then Done  ' there was an underflow
' else
HiWord = HiWordNew

NoBorrow:
LoWord = LoWordNew
' SerTxD (#Q, "  ", #HiWord, "  ", #LoWord, 13, 10)  ' used while debugging
' Pause 100
Next

Done:
' SerTxD (#Q, " ..........")
TF_10 = 4550 - Q

SerTxD(#TF_10, 13, 10)	' display 10 times the temperature in degrees F
Pause 1000
GoTo Main_1			' continually loop

Error:
SerTxD ("Out of Range", 13, 10)
Pause 1000
GoTo Main_1
```