PicBasic Pro, PicBasic, BASIC Stamp, Microchip PIC, 8051, and Remote Control Projects

Using the LM34 Precision Fahrenheit Temperature Sensor

This project shows how to use the National LM34C Precision Fahrenheit Temperature Sensors with the PIC microcontroller, and PicBasic Pro compiler.

There are quite a few applications for precision temperature sensors, but the majority of decent temperature sensors output each temperature reading in °C. Without the ability to handle floating point numbers with integer math, you're normally limited to close at best results when you convert from °C to °F in the form of some tricky integer math to complete the conversion.

The conversion equation from Celsius to Fahrenheit is: 9/5 °C + 32. Since 9/5 = 1.8, we're left trying to calculate a float for the conversion results.

As an example, suppose we're converting from 22 °C to °F. 9/5 = 1.8, so we need (1.8 x 22 °C) + 32 = 71.6 °F. That's tough to do with integer math, and most tricky calculations leave you a few ° short in either direction - especially when working with negative temperature values.

Unlike the Dallas 1-wire type temperature sensors that force you to convert from Celsius to Fahrenheit, the LM34C from National outputs an analog voltage linearly proportional to the temperature in °F. The linear output of the LM34 is: +10.0 mV/°F scale factor - or simply +10mV per °F.

This makes precision temperature readings incredibly simple with the PicBasic Pro compiler, and any PIC with onboard A/D. If you read the voltage output from the LM34 with your volt meter, a reading of 0.75 volts indicates a temperature of 75°F. This is very easy to convert to something we can quickly use without tricky calculations in the conversion process as with °C to °F.

The relationship between the linear voltage output, and temperature is 10 millivolts per °F. The LM34CAZ has an accuracy of ±3°F over a range of -40°F to +230°F. This application is for an indoor thermometer, so the LM34CAZ is more than appropriate for the task, and very simple to use.

Note: The accuracy of ±3°F is worst case scenario. For most applications where the sensor is used inside your home with temperatures normally between 68°F to 75°F, the accuracy is much better. I have found it to be within 1-1.5°F consistently, and certainly more accurate than my old home thermostat hanging on the wall..;o]

Connect the center pin of the LM34CAZ to PortA.0 of the PIC16F877 [or other PIC with onboard A/D], and the remaining pins as shown above.

Adjust the potentiometer until you have a reading with your volt meter of +2.55 volts from the potentiometer wiper to ground. Then connect the wiper to PortA.3, and the remaining terminals as shown above.

Since the LM34CAZ will output a voltage of 10 millivolts per °F, we can set PortA.3 as the +Vref at 2.55V, and each ADC unit will = 2.55V/255 = 0.01V = 10 mV. We're using the PIC16F877 A/D input-pin PortA.0 for the analog input from the LM34. The A/D is set for 8-bit resolution. With PortA.3 as +Vref, and our + reference set to 2.55V, each ADC unit will = 1°F. This limits the upper range, but for a household application it's more than enough range.

This project sends temperature data to the PC serial port to view in the terminal window. The temperature can be verified while keeping a volt meter on the LM34 output to verify accuracy. Notice in the code below where we setup the DEBUG mode as DEFINE debug_mode 0. This tells the compiler we're sending serial data in TRUE mode.

I used the FLASH-Lab 77 PIC16F877 development board to test the hardware & code for this project, and the FLASH-Lab 77 serial connection to the PC is through the MAX233 RS-232 level converter IC, so mode 0 is necessary. If you're using a direct connection between the PIC serial pin, and your PC serial port, just insert a 1k series resistor between the I/O-pin you use, and the PC serial port RX pin.

Be sure to change the debug mode to 1 like this: DEFINE debug_mode 1 which will send data inverted for the direct connection. If you're not using the FLASH-Lab 77 for development, or not sending data through a MAX233, here's a schematic for the direct connection.

The rest is simple. Here's the results of my temperature readings from the LM34CAZ sent to the terminal window using the free PicBasic MicroCode Studio editor RS-232 terminal program.

The actual voltage/temperature reading of the LM34CAZ was tested with a Fluke 29 series multimeter @ 0.814 volts during the reading above sent to the PC serial terminal window. I have tested the temperature readings from 150°F to 45°F, and the results have been rock-solid over the entire range tested. Hopefully, it will never get down to 45°F in my house, but it's tested down to 45°F...;o]

The code shown below sets up a word size variable named samples. This word sized variable simply accumulates a total of 20 analog readings by: samples = samples + temp within the loop.

Then we simply average the 20 readings to arrive at our final result by: temp = samples/20. The temp variable now holds the averaged result of 20 individual analog temperature readings taken over a period of 20 x 250 milliseconds or 5-seconds. The reasoning behind this approach is to eliminate fluctuations you may have with single readings.

The LM34CAZ is so sensitive that even a gust of wind from walking by or breathing close to the device will cause rapid fluctuations in temperature readings.

The rest is cake. Have fun with the project. You're definitely not limited to just sending the temperature readings to the PC.

The Code:

'****************************************************************
'*  Name    : LM34.BAS                                          *
'*  Author  : Bruce Reynolds                                    *
'*  Date    : 10/23/2001                                        *
'*  Version : 1.0                                               *
'*  Notes   :  8-bit A/D temperature conversion with National   *
'*          ;  LM34CAZ analog temperature sensor.               *
'*          :                                                   *
'****************************************************************

DEFINE loader_used 1    ' Boot loader is being used
DEFINE debug_mode  0    ' Debug sending True serial data
DEFINE debug_reg portc  ' Debug Port = PortC
DEFINE debug_bit 6      ' Debug.bit = PortC.6
DEFINE debug_baud 9600  ' Default baud rate = 9600
DEFINE osc 4            ' We're using a 4 MHz oscillator
DEFINE ADC_BITS 8       ' Set A/D for 8-bit operation
DEFINE ADC_CLOCK 1      ' Set A/D clock Fosc/8
DEFINE ADC_SAMPLEUS 50  ' Set A/D sampling time @ 50 uS
samples VAR WORD        ' Multiple A/D sample accumulator
sample  VAR BYTE        ' Holds number of samples to take
temp    VAR BYTE        ' Temperature storage
samples = 0             ' Clear samples accumulator on power-up

   
TRISA = %11111111   ' Set PORTA to all input
   
ADCON1 = %00000011  ' Set PORTA.0,1,2,5 = A/D, PortA.3 = +Vref
   
PAUSE 500           ' Wait .5 second

loop:
    FOR sample = 1 TO 20    ' Take 20 samples
       
ADCIN 0, temp       ' Read channel 0 into temp variable
       
samples = samples + temp ' Accumulate 20 samples
       
PAUSE 250           ' Wait approximately 1/4 seconds per loop
   
NEXT sample
    temp = samples/20
    DEBUG "Temperature is: ",DEC temp," Deg F",10,13
    samples = 0             ' Clear old sample accumulator
    GOTO loop               ' Do it forever

    END

Until the next project - have fun - and don't blow anything up...;o]

Regards,

-Bruce


Back to the PicBasic Projects Section

Copyright © 1999-2008 Reynolds Electronics