|
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:
Until the next project - have fun - and don't blow anything up...;o] Regards, -Bruce
|