PICAXE-18X - Interfacing with an LM34 Temperature Sensor

copyright, Peter H Anderson, Baltimore, MD, Mar, '04


This note illustrates how a National LM34 analog temperature sensor may be interfaced with a PICAXE to measure and display temperature and also control a relay or similar for thermostat operation. This low cost sensor packaged in a three terminal TO-92 package provides a 10 mV per degree F output. Thus, for 68 degrees F, the output voltage is nominally 680 mV or 0.68 VDC. The LM34 is available from a number of sources including Jameco in the US for less than $2.00.

The PICAXE-18A, -X and similar 28A and X and 40X parts provide a number of ten bit A/D converters. The A/D value is read and the voltage is calculated. Over the range of 0.0 to 5.0 VDC, each of the 1024 quantizing levels is nominally 5 mV and thus the limit of temperature resolution is 0.5 degrees F.

In the following implementation, sixty four 10-bit A/D conversions are performed and summed and the average is then calculated by dividing the sum by 64. The voltage in mV is then calculated as ADVal * 5000 / 1024 or ADVAL * 4.88. The whole part of the temperature is then this voltage divided by ten and the fractional part is the remainder.

In this program, a relay is turned on if the temperature is less than 68.0 degrees F and the relay is released if the temperature is greater than 70.0. Clearly, these trip points and the polarity may be defined to suit your requirements. For example, in Maryland, an attic fan might be turned on when the temperature rises above 120 degrees F and turned off when the temperature falls below 85.

In writing this program I was rather amazed to see that it used only 84 bytes of the 2048 bytes on a PICAXE-18X. I think one might extend this to three or even four LM34 devices, controlling three or four relays and accommodate this in a less expensive PICAXE-18A.

The US uses the F scale, while most of the world uses degrees C, and thus one might be tempted to use the LM35 to measure temperature in degrees C. However, recognize that the LM35 provides 10 mV per degree C and the PICAXE can thus only resolve temperature to within 0.5 degrees C. (or about 0.9 degrees F). In addition, without conditioning of some sort between the LM35 and the PICAXE A/D input, using the LM35 precludes measuring temperatures below 0 degrees C (32 degrees F).

My suggestion, is to use the LM34 to calculate the temperature in degrees F and convert to degrees C if desired. This improves the resolution by a factor of 1.8 and permits simple measurements with no special conditioning down to about minus 17.8 degrees C (0 degrees F).

When locating the LM34 sensor at any distance from the PICAXE, I would suggest running two twisted pairs; +5 VDC and GRD and a second consisting of signal out and GRD. For the second GRD, connect it to ground only at the PICAXE to avoid ground loops. The twisted pair actually is a very effective noise reduction technique. In addition, one might use capacitors between the +5 VDC source and ground and the signal out and ground. Typical values might be a 1.0 uFd electrolytic (for low frequencies) in parallel with a 0.047 ceramic (for high frequencies).


' LM34_1.Bas (PICAXE-18X)
'
' Continually measures the temperature in degrees F using an LM34.
'
' Performs 64 measurements and averages the 10-bit A/D Value.
'
' The voltage in mV (TF_10) is calculated as 5000 / 1024 * ADVal = 4.88 * ADVal
' The temperature is then displayed in decimal format.
'
' Note that the resolution is 0.5 degrees F.
'
' If the temperature is less than 68.0 degrees F, a relay on Out0 is operated.  If
' greater than 70.0, the relay is released.
'
' This was tested on a PICAXE-18X.  It uses 84 bytes leading me to conclude that one
' might extend this to two or three LM34s, each controlling its own relay using the less
' expensive PICAXE-18A.  Quite a lot of punch for a $4.00 processor.
'
'  LM34			PICAXE-18X
'
'   Out (2) ------------ AN0 (term 17)
'
' copyright, Peter H Anderson, Baltimore, MD, Mar, '04

Symbol ADVal = W0
Symbol Sum = W1
Symbol TF_10 = W1

Symbol N  = B4

Symbol Whole = B5
Symbol Fract = B6


Main:

   Sum = 0

   For N = 1 to 64			' sum 64 readings
      ReadADC10 0, ADVal
      Sum = Sum + ADVal
   Next

   ADVal = Sum / 64			' calculate the average

   TF_10 = ADVal * 4		' 4.88 * ADVal
   TF_10 = ADval * 8 / 10 + TF_10
   TF_10 = ADval * 8 / 100 + TF_10

   Whole = TF_10 / 10		' TF whole
   Fract = TF_10 % 10		' TF tenths of a degree

   SerTxD (#Whole, ".", #Fract, 13, 10)

   If TF_10 < 680 Then OperateRelay

   If TF_10 > 700 Then ReleaseRelay

Main_1:
   Pause 1000
   GoTo Main

OperateRelay:
   High 0
   GoTo Main_1

ReleaseRelay:
   Low 0
   GoTo Main_1