PICAXE Temperature Sensing with LM335
Alex Wong
Sydney PIC Club

Introduction

This project measures temperature using a PICAXE-08M to read voltage output from an LM335 sensor. A PC program polls the PICAXE through the serial port for temperature readings and displays them in real-time on a GUI interface created with Autoit. Readings are also logged into a CSV file. The prerequisites for this project are:

Schematic



        

The LM335 series are precision, easily-calibrated, integrated circuit temperature sensors. Operating as a 2-terminal zener, the LM335 has a breakdown voltage directly proportional to absolute temperature at +10 mV/Kelvin. With less than 1Ohm dynamic impedance the device operates over a current range of 400 µA to 5 mA with virtually no change in performance. When calibrated at 25℃ the LM335 has typically less than 1℃ error over a 100℃ temperature range. Unlike other sensors the LM335 has a linear output.

PICAXE Code

'TempSensor.bas - Measure temperature by using a PICAXE-08M to read the voltage from
'an LM335 sensor. The results are converted to Centigrade(ASCII) and sent to
'PC through an RS232 connection for display. The PC polls for values by sending the
'string SSSS to the PICAXE at regular intervals.
'COM port settings - 2400,N,8,1
'------------------------------------------------------------------------------------
   Symbol ADVal=W0                        'voltage from LM335
   Symbol Sum=W1                          'total readings
   Symbol SensorOutput=W1                 'LM335 output voltage
   Symbol Fraction=W1                     'eg: 8 in 10.8
   Symbol Whole=W2                        'eg: 10 in 10.8 
   Symbol factor=W4                       'Kelvin to Centigrade conversion factor
   symbol loop=b10                        'loop counter

   factor=273                             '273k = 0c
main:
   serin 2,n2400,b1,b2,b3,b4              'Wait for poll string from PC
   if b1 = "S" then tempout               'got SSSS? send temperature
   if b1 = "C" then calibrate             'got Cnnn? calibrate factor
   goto main                              'ignore anything else

tempout:
   Sum = 0
   For loop = 1 to 64                     ' sum 64 readings
      ReadADC10 1, ADVal
      Sum = Sum + ADVal
   Next
   ADVal = Sum / 64                       ' calculate the average

'The ADC has a resolution of 10 bits ie. it will return a value between 0 and 1023 over
'the 5V power supply range ie. each value represents 5/1024 = 4.88 mV. The LM335 outputs
'10mV/Kelvin rise hence - temp in Kelvin=total mV/10.
'Note: PICAXE does not support floating point maths hence, in a multiply calculation, the
'integer and fractions must be separately added together.

   SensorOutput = ADVal * 4                       'total mV=4.88 * ADVal
   SensorOutput = ADval * 8 / 10 + SensorOutput   'add fraction
   SensorOutput = ADval * 8 / 100 + SensorOutput  'LM335 output in mV
   Whole = SensorOutput / 10                      'get Kelvin (10mV/Kelvin)
   Whole=Whole-factor                             'get Centigrade (273k = 0c)
   Fraction = SensorOutput % 10                   'fraction=10th of degree

   SEROUT 4,n2400,(#Whole, ".", #Fraction, 13,10)   'send ASCII temp to PC

   goto main                              'wait for next poll

calibrate:      'change value of factor. Useful if battery runs low.
   factor=b2-48 *100                      'eg: ascii 216 -> bin 200
   factor=b3-48 *10 +factor               '200 + bin 10
   factor=b4-48 +factor                   '210 + bin 6 = 216
   goto main

Autoit GUI

The GUI interface to this project is created using a freeware BASIC-like scripting language called AUTOIT. The software requires an additional freeware module called Netcomm.OCX from Richard Grier for serial port communications.

This is what the GUI looks like:



AutoIt uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt enables you to create some complex GUIs like this below.



My personal experience with Autoit.

ProsCons
  • Simple to learn, less complexity.
  • Easy prototyping.
  • Built-in forms designer.
  • Customised IDE for editing.
  • Very good support forum.
  • Lots of user-contributed functions.
  • Freeware.
  • Code is interpreted - Slow.
  • No real debugging capabilities.
  • Limited error handling facilities.
  • A bit buggy in places.
  • 1-way forms designer.
  • Language not object-oriented.