Fads to Obsessions and Beyond...




Free domain for life, exceptional technical support, website transfer

LM35 Temperature Sensor

A LM35 temperature sensor is interfaced to a PIC16F876A microcontroller. The sensor output (10mV/oC) is converted with the ADC module and displayed on a LCD. ADC oversampling to improve temperature resolution is demonstrated.

There are many projects in which the measurement of temperature is required. From measuring ambient temperature for weather station/data logging through to helping regulate battery charging or identifying possible error conditions on a PCB (e.g. overheating of a circuit component). Similarly, there are many ways of measuring temperature from that of using thermistors (or even just plain resistors in a "pinch" and/or if accuracy not particularly required) through to IC's providing digital output of the converted temperature (e.g DS18S20).

The LM35 provides a 10mV/oC linear output in the range of -55o to +150o and does not require any external calibration or trimming. This makes the LM35 much more convenient than say a thermistor (which would need to be calibrated if a scaled temperature read-out was desired). While the DS18S20 temperature sensor has a number of features that can make this component more suitable in particular circumstances (user-definable nonvolatile alarm settings for example), this also requires much more ROM/coding within the PIC mircocontroller. Simiarily, the DHT-11 humidity/temperature sensor also conveniently provides a factory calibrated digital temperature as output, but also requires a degree of ROM/coding overhead within the PIC mircocontroller, and the datasheet recommends that the DHT-11 should not be polled for updated temperatures at a frequency of greater than five seconds.

The fruit/vegetable dehydrator project, required a temperature sensor that could provide relatively frequent data (>0.5 second) as input to a PID controller for the regulation of air temperature via a heater element. The LM35 was selected as it provides a convenient 10mV/oC linear output, doesn't require calibration, and in conjunction with an ADC can provide updated temperature readings within milliseconds and also the following from the datasheet:

    • Calibrated directly in oCelsius (Centigrade)
    • 0.5oC accuracy guaranteeable (at 25oC)
    • Rated for full -55o to +150oC range
    • Operates from 4 to 30 volts)
    • Less than 60 mA current drain
    • Low self-heating, 0.08oC in still air
    • Nonlinearity only 1/4oC typical
    • Accuracy of 1/4oC at room temperature

The circuit involves the usual minimum components to enable the operation of the PIC microcontroller. In addition, a RS232 connection and a Nokia 5110 LCD are provided to enable data output to a connected desktop/laptop computer and immediate display on the LCD respectively. The LM35 temperature sensor is interfaced to the PIC via the microcontroller analog to digital converter (ADC) which is supported by the LM336 providing a precision 2.5V reference voltage.

Power Supply

A typical "wall-wart" power-supply is used (a surplus laptop charger in this case) in conjunction with a voltage regulator (LM7805) to provide the regulated 5V required by the PIC microcontroller. The Nokia 5110 LCD requires a 3.3V supply (from the datasheet, 2.7-3.3V) which consumes ~7mA during normal operation. There are numerous ways this can be done, and in this case a LM1086 voltage regulator is used to step down the 5V from the LM7805.

Circuit Operation

The crystal X1 and associated capacitors C1 and C2 provide the oscillator for the PIC16F876A microcontroller. Incircuit serial programming (ICSP) of the PIC16F876A microcontroller is provided via connector J1 with switch SW1, resistor R1 and diode D2 providing voltage protecting during loading code into the PIC microcontroller.

The Nokia 5110 LCD inputs require 3.3V (from the datasheet, all input voltages -0.5V to Vdd+0.5V), but the PIC16F876A microcontroller produces 5V output. Some references on the web advise they have successfully used 5V input from microcontroller into the Nokia 5110 LCD (but probably not advised - 'level shifting' components are cheaper than replacing a burnt-out 5110). The CD4050 non-inverting hex buffer provides a readily available, convenient solution (3.3V connected to pin 1) that acts as a 'level shifter'.

The MAX232 provides the necessary level conversion to enable RS232 communication between the PIC microcontroller and a connected desktop/laptop computer.

The LM7805 provides the 5V circuit voltage, in this case, stepping down from the 12V input from a "wall-wart" power-supply. This 5V supply is then converted to a precision 2.5V reference for the PIC ADC module by the LM336 (R2 limits current through the LM336). Depending upon the temperature accuracy/precision required, the LM336 could potentially be replaced by a simply voltage divider and or have the 5V supply used directly as the ADC voltage reference.

The LM35 contains the necessary internal circuitry to provide a voltage output, 10mV/oC, that is linearly proportional to the Celsius temperature.

As per the datasheet, although the LM35 sensor can drive capacitive loads up to 10,000 pF without oscillation, output voltage transient response times can be improved by using a small resistor (R4) in series with the output of the temperature sensor. As an added benefit, this resistor (R4) forms a low-pass filter with the cable capacitance, which helps to reduce bandwidth noise.

If the temperature sensor is likely to be used in environments where the ambient noise level can be very high, resistor R4 helps to prevent rectification by the devices of the high frequency noise. The combination of resistor R4 and the supply bypass capacitor (C10) offers the best protection.

Calibration

The datasheet advises the LM35 provides 10mV/oC linear output in the range -55o to +150o and doesn't require calibration.

The Testing/Experimental Results Section nevertheless shows the results of comparing the temperature reported by the LM35 versus a mercury-bulb thermometer.


Note: Image loading can be slow depending on server load.

  • LM35 SchematicLM35 Schematic

    Silver Membership registration gives access to full resolution schematic diagrams.

    LM35 Schematic

This project did not require a PCB.

The construction was done using prototyping board. See the photographs and schematic diagram sections.

Qty Schematic Part-Reference Value Notes/Datasheet
1R110k1/4W, 10% 
1R21k1/4W, 10% 
1R31001/4W, 10% 
1R4681/4W, 10% 
Capacitors
2C1,C222pFCeramic 
1C30.33uF 
1C40.1uF 
6C5-C101uF 
Integrated Circuits
1U1PIC16F876APIC microcontroller  datasheet
1U27805Linear Voltage Regulator  datasheet
1U3CD4050Hex non-Inverting Buffer datasheet
1U5MAX232ERS232 Driver/Receiver datasheet
1U4LM1086IT 3.3Linear Voltage Regulator  datasheet
1U4LM35Temperature Sensor datasheet
Diodes
1D11N-4004 
1D2LM336-2V52.5V Reference Voltage  datasheet
Miscellaeous
1J1CONN-H55-pin connector for ICSP
1P1CONN9-pin connector for RS232
1SW1SW-SPDT 
1X110MHzCrystal Oscillator
1Z1 Nokia 5110 LCD datasheet

Description Downloads
LM35 Sensor Bill of Materials Text File Download

The testing of the LM35 temperature sensor in part also involved investigating the various parameters associated with the analog to digital convertor (ADC) from the PIC 16F876A. As described in the "Background Section" above, the LM35 provides a 10mV/oC linear output with temperature. Therefore, the ADC is required to measure and convert the voltage output from the LM35 to calculate/display the actual measured temperature. The PIC ADC Module page gives specific detail about usage of the ADC, whereas, Learning PIC Microcontrollers gives specific details about using PIC microcontrollers in general. Of particular revelence here is the ADC clock value and the reference voltage.

The following figure presents a number of graphs showing the results of differing ADC clock values and reference voltages used with the PIC 16F876A ADC connected to the LM35. The ADC clock value must be set appropriately for the PIC oscillator speed being used (10Mhz in this instance) in order that the PIC ADC module has sufficient time to perform the conversion of the input voltage to a digital value (see PIC ADC Module). The ADC_CLOCK_DIV32 (red graph) shows a greater range of voltages from the LM35 (ie temperature variation) compared to the ADC_CLOCK_DIV16 (black graph) in the following figure, as the "DIV16" setting for the PIC 16F876 at 10Mhz was insufficient to enable the full range of voltage to be sampled and was subsequently baised low.

  • Graph 1: ADC Clock Value and Vref Value versus LM35 Reading

    ADC Clock Value and Vref Value versus LM35 ReadingADC Clock Value and Vref Value versus LM35 Reading

    Silver Membership registration gives access to full resolution diagrams.

    ADC Clock Value and Vref Value versus LM35 Reading

The effect of input reference voltage to the ADC is shown by the comparison of the 1.25V (blue graph) compared to the 2.5V (red graph) using the same ADC clock setting. The ADC has a set number of "bits" resolution (10-bit in this case). The input reference voltage divided by the maximum bit value (1023 = 210 - 1) then gives the minimum voltage difference that can be measured by the ADC. Using a lower input reference voltage to the ADC then allows each "availabe" bit to represent a smaller difference in voltage. The 1.25V (blue graph) reference voltage shows a more "gaussian" shape to the measured voltage giving confidence that the reported temperature is likely of better resolution (25.5oC in this case as measured with a mercury bulb thermometer - see the Photographs Section).

However, using a lower input reference voltage to the ADC means of course a lower maximum voltage can be measured (1.25V in this case). Since the LM35 provides a 10mV/oC linear output with temperature, the 1.25V ADC reference voltage means a maximum temperature of 125oC, compared to 150oC that the LM35 is capable of measuring as per the datasheet.

Sensor Connection

As mentioned in the Circuit Details Section, a resistor (R4 in the schematic diagram) in conjunction with the bypass capacitor (C10 in the schematic diagram) acts as a low-pass filter and is beneficial when long sensor cable lengths are involved and or noisy environmental conditions.

The following graph 2 shows the results of observed temperature from a LM35 versus sensor cable length and use of the RC filter. The temperature reported by the LM35 when connected directly onto the breadboard (i.e the shortest possible connection) is given by the black-coloured points on graph 2 which is ~24.9oC. Ambient temperature as measured by a mercury-blub thermometer was 25.1oC.

When the LM35 was connected by ~1.2m cable length (the red-coloured points on graph 2) the recorded temperture dropped to ~24.2oC (due to voltage drop/loss across the cable). The addition of the RC low-pass filter (R2 and C10 in the schematic diagram) corrected the observed temperature reported by the LM35 (blue-coloured points on graph 2).

  • Graph 2: Sensor cable length and RC filter effect

    Sensor ConnectionSensor Connection

    Silver Membership registration gives access to full resolution diagrams.

    Sensor Connection

Sensor 'Probe'Construction

The LM35 used during the testing was in the form off a TO-92 package. This TO-92 package needs to be connected to the necessary cabling and sealed appropriately to form a 'probe' that can be used to measure the tempeature of environments remote from the control circuitry (PIC microcontroller, power supply etc). The desired environments includes liquids (aqueous) up to ~100oC (i.e. boiling water).

The Photographs Section shows two probes constructed from LM35 TO-92 packages, using alternatively hot-glue and a expoxy glue to seal the TO-92 package into a sealed plastic tube. The following graph 3 shows the results of measuring temperature using the constructed probes versus temperature as measured with a mercury-blub thermometer.

  • Graph 3: LM35 probe construction material versus temperature/time

    Sensor Construction MaterialSensor Construction Material

    Silver Membership registration gives access to full resolution diagrams.

    Sensor Construction Material versus temperature

From graph 3, the temperature probe constructed using hot-glue (blue data points) has a 'lag' when comparing temperature as reported by the mercury-bulb thermometer. This is most readily explained as the hot-glue was acting as an insulator. The probe constructed using expoxy-glue (which allowed the majority of the LM35 TO-92 package to be exposed, yet enable a water-tight seal) gives temperature (red data points) in close agreement with that as measured by the mercury-bulb thermometer.

Graphs 4 and 5 show another factor in relation to the material used to construct a 'probe' using a LM35. The hot-glue (graph 4) does not hold-up well to elevated temperature and begins to leak.Whereas, the expoxy-glue constructed probe (graph 5) proved to be durable at the temperatures/times observed.

  • Graph 4: LM35 probe from hot-glue - temperature versus time

    LM35 probe from hot-glue - temperature versus timeLM35 probe from hot-glue - temperature versus time

    Silver Membership registration gives access to full resolution diagrams.

    LM35 probe from hot-glue - temperature versus time


  • Graph 5: LM35 probe from expoxy-glue - temperature versus time

    LM35 probe from expoxy-glue - temperature versus timeLM35 probe from expoxy-glue - temperature versus time

    Silver Membership registration gives access to full resolution diagrams.

    LM35 probe from expoxy-glue - temperature versus time

Oversampling to increase Resolution

The LM35 sensor 10mV/degree Celsius output measured with 10-bits ADC and 2.5V voltage reference input produces a "step" response when monitoring slowly fluctuating temperature changes. This is due to 10-bits with Vref=2.5V equating to 2.4mV per bit (2^10=1024, 2.5/1024 = 2.4mV/bit), and therefore, with 10mV = 1 degree Celsius, a minimum temperature difference of 0.24oC (without taking into account noise etc). For reference, with 10-bits ADC and Vref=5V this equates to 0.5oC per bit and 8-bits ADC with Vref=5V 2oC per bit.

Now while the accuracy of the LM35 (datasheet states ±0.5oC accuracy guaranteeable at 25oC) is set by the internal circuitry, the resolution between temperature measurements with the LM35 can be improved by the "oversampling" technique. This is desirable in circumstances such as measuring the relative changes in a slowly fluctuating temperature situation, or using/displaying the relative change in temperature as the signal changes between set points (for instance in a thermostat application). Again, to emphasise, oversampling does not improve accuracy, but can improve resolution.

The AVR article "AVR121: Enhancing ADC resolution by ovesampling" gives the background details about increasing ADC resolution by oversampling, averaging and decimation, and noise reduction by averaging samples (2). The interested reader is directed to this article for the theory of operation, Nyquist theorem and required criteria for applicability, however, using the method is fairly straight-forward.

The following code snippet implements ADC oversampling using CCS C code, and this was applied to measuring the temperature of cooling hot water using a LM35 temperature sensor (data presented in graphs 6 and 7 below).

Code Snippet 1: ADC oversampling


int16 overSampling_ReadADC(int8 numExtraBits) {
   //numExtraBits over actual 10-bit ADC
   int8 numSamples, i;
   int16 sumSamples = 0, ADCvalue;
   
   numSamples = 1 << (2*numExtraBits);
   for (i=0; i < numSamples; i++) {
      ADCvalue = Read_ADC();
      sumSamples = sumSamples + ADCvalue;
   }
   sumSamples = sumSamples >> numExtraBits;
   return (sumSamples);
}            
            

Graph 6 and 7 below presents LM35 temperature data from monitoring a beaker of hot water allowed to cool to ambient room temperature using 10bit ADC with Vref=2.5V and then with "11-bit" and "12-bit" via using ADC oversampling. The 10bit ADC experimental run also had the water temperature monitored simultaneously by manually reading a mercury bulb thermometer.

The 10-bit temperature data shows the "steps" due to the limited resolution of the data, as discussed above, in what should have been otherwise a smooth continuous curve. The use of ADC oversampling to give in effect 11-bit and 12-bit resolution to the monitored temperature resulted in the expected "smoother" cooling curves.

The collected temperature data does indeed show that "software" ADC oversampling can improve resolution, but this does not alter the inherent accuracy of the LM35 which is ±0.5oC.

  • Graph 6: LM35 with ADC oversampling - cooling water temperature versus time

    Graph 6: LM35 with ADC oversamplingGraph 6: LM35 with ADC oversampling

    Silver Membership registration gives access to full resolution diagrams.

    Graph 6: LM35 with ADC oversampling

  • Graph 7: LM35 with ADC oversampling - temperature versus time, expanded scale

    Graph 7: LM35 with ADC oversamplingGraph 7: LM35 with ADC oversampling

    Silver Membership registration gives access to full resolution diagrams.

    Graph 7: LM35 with ADC oversampling


Since the testing of the LM35 was only done to a breadboard stage, this is a relatively simple project and no particular difficulties, other than the usual care and attention required when constructing any electronic circuit, should be expected.

The only particular 'difficulty' involves constructing the LM35 into a 'probe' that is suitable for whatever environmental conditions in which it is to be used (i.e, measuring environments such as air or liquid and expected temperature range). My particular application involved aqueous liquids up to ~100oC (i.e. boiling water), so expoxy-glue within a plastic sealed tube was required (see the Testing/Experimental Results Section).


Comments/Questions

No comments yet.

Add Comment/Question

Only Logged-In Members can add comments

"If we could sell our experiences for what they cost us, we'd all be millionaires".

Please donate any amount to help with hosting this web site.

If you subscribe (only $2/annum) you can view the site without advertisements and get emails abouts updates etc.

X

Sorry!

Only logged-in users with correct Membership Level can download.


Membership starts at only $2, so join now!