Digital Thermometer using PIC Microcontroller and LM35 Temperature Sensor Circuit diagram

Figure 1: Digital Thermometer Circuit Diagram

The LM35 series are precision integrated-circuit temperature sensors from Texas Instruments, whose output voltage is linearly proportional to the Celsius (Centigrade) temperature. Its output is linearly proportional to Centigrade temperature Scale and it changes by 10 mV per °C.

The LM35 thus has an advantage over linear temperature sensors calibrated in ° Kelvin, as the user is not required to subtract a large constant voltage from its output to obtain convenient Centigrade scaling. 
The LM35 does not require any external calibration or trimming to provide typical accuracy of ±1⁄4°C at room temperature and ±3⁄4°C over a full −55 to +150°C temperature range. Low cost is assured by trimming and calibration at the wafer level. 
The LM35’s low output impedance, linear output, and precise inherent calibration make interfacing to readout or control circuitry especially easy. It can be used with single power supplies, or with plus and minus supplies. 
As it draws only 60 μA from its supply, it has very low self-heating, less than 0.1°C in still air.

The LM35 Temperature Sensor has Zero offset voltage, which means that the Output is 0V when the temperature is at 0 °C. Thus for the maximum temperature value (150 °C), the maximum output voltage of the sensor would be 150 * 10 mV = 1.5V. 
If we use the supply voltage (5V) as the Vref+ for Analog to Digital Conversion (ADC) the resolution will be poor as the input voltage will goes only up to 1.5V and the power supply voltage variations may affects ADC output. So it is better to use a stable low voltage above 1.5 as Vref+. We should supply Negative voltage instead of GND to LM35 for measuring negative Temperatures.
   1. Calibrated directly in ° Celsius (Centigrade) 
   2. Linear + 10.0 mV/°C scale factor 
   3. 0.5°C accuracy guaranteed (at +25°C) 
   4. Rated for full −55° to +150°C range 
   5. Suitable for remote applications 
   6. Low cost due to wafer-level trimming 
   7. Operates from 4 to 30 volts 
   8. Less than 60 μA current drain 
   9. Low self-heating, 0.08°C in still air 
  10. Non-linearity only ±1⁄4°C typical 
  11. Low impedance output, 0.1 Ω for 1 mA load

               Extract from the LM35 Datasheet

Full-Range Centigrade Temperature SensorLM35 Packages

More information can be found from the LM35 datasheet

 Circuit diagram

As shown on the circuit diagram on figure 1 above, it is very easy to interface the LM35 to a PIC, its output pin can be connected to any analog input of the PIC, the VS to +5 supply and the ground to ground. In this example, the LM35 is connected to Analog 0 (pin 2 of PIC18F2620).
VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND. The MCLR is disabled in software and an internal oscillator clock is selected so no need for external crystal. 
A 16 x 2 lines LCD display is connected to PORT B. refer to the Interfacing LCD Display with PIC microcontroller article to learn more.

You can watch the video below to learn how to build the circuit diagram.

Watch The Video Tutorial: Circuit Diagram


To configure Analog to Digital converter is really easy, all we need is to configure the ADC, Start conversion and then manipulate the results.
Refer to the article Analog to Digital Converter in PIC Microcontroller to learn more.


If you are using XC8 v1.35, the Peripheral Libraries which include the LCD and other peripherals like ADC, SPI, I2C libraries are no longer included in the installation file as with previous versions. You can either write your own LCD library and use MPLAB code configurator to configure the other peripherals or the easiest is to download the Peripheral Libraries as a separate download and install them,. You can download them at microchip website under the MPLAB XC Compilers downloads tab. They are now called PIC18 Legacy Peripheral Libraries.

Download: PIC18 Legacy Peripheral Libraries

Configure ADC: 

void init_ADC(void)  //Initialize ADC
                  //ADC configured for: FOSC/2 as conversion clock, Result is right justified, Aquisition
                 //time of 2 AD, Channel 1 for sampling, ADC interrupt off, ADC reference voltage from VDD & VSS

Start Conversion:

unsigned int ADCResult=0;
ConvertADC();                //Start conversion
while(BusyADC());            //Wait here until conversion is finished
ADCResult = ReadADC();       //Read the converted data

Manipulate Results: 

float temperature=0;
temperature = (ADCResult*5.0)/10.24;  //convert the 10-bit data into temperature (LM35 produces 10mV per degree celcius)

Watch The Video Tutorial: MPLAB XC8 Code

 Full Code:

* File: Thermometer.c
* Author: Student Companion SA:
* Digital Thermometer with PIC microcontroller and LM35 Temperature sensor
#include <stdio.h>
#include <stdlib.h>
#include <Delays.h>
#include "Thermometer.h"
void init_ADC(void);        //Initialize ADC
void init_XLCD(void);       //Initialize LCD display
void DelayFor18TCY( void ); //18 cycles delay
void DelayPORXLCD (void);   // Delay of 15ms
void DelayXLCD (void);       // Delay of 5ms
//**************Declare Global Variables**************************************
unsigned char buf[20];
unsigned int ADCResult=0;
float temperature=0;
void main(void) {
     OSCCON=0x76;         //Configure to use 8MHz internal oscillator.
     init_XLCD();         //Call the Initialize LCD display function
     init_ADC();          //Call the Initialize ADC function
   putrsXLCD("Digital");           //Display "Digital"
   SetDDRamAddr(0x40);             //shift cursor to beginning of second line
   putrsXLCD("Thermometer...");    //Display "Thermometer..."
   for (int x=0; x<=20;x++) __delay_ms(50); // 1 second delay
WriteCmdXLCD(0x01);                         //Clear Screen
while (1){
ADCResult =0;
//---sample and convert----
ConvertADC();                              //Start conversion
while(BusyADC());                          //Wait here until conversion is finished
ADCResult = ReadADC();                     //Read the converted data
temperature = (ADCResult*5.0)/10.24;       //convert data into temperature (LM35 produces 10mV per degree celcius)
putrsXLCD("Temp is ");                     //Display "Temp is" on the screen
sprintf(buf, "%.3g", temperature );        //Convert temperature float value to string
putsXLCD(buf);                             //Display the temperature on the screen
putcXLCD(223);                             //Display degree symbol
                                           //Different LCD displays have different char code for degree
putrsXLCD("C");                            // Display "C" for Celsius
putrsXLCD(" ");                            // Clear after comma
WriteCmdXLCD(0x02);                        //Home position on LCD
void init_XLCD(void)                //Initialize LCD display
OpenXLCD(FOUR_BIT&LINES_5X7);       //configure LCD in 4-bit Data Interface mode
                                    //and 5x7 characters, multiple line display
while(BusyXLCD());                  //Check if the LCD controller is not busy
                                    //before writing some commands
WriteCmdXLCD(0x06);                 //Move cursor right, don't shift display
WriteCmdXLCD(0x0C);                 //Turn display on without cursor
void init_ADC(void)                 //Initialize ADC
/**** ADC configured for:
* FOSC/2 as conversion clock
* Result is right justified
* Aquisition time of 2 AD
* Channel 1 for sampling
* ADC interrupt off
* ADC reference voltage from VDD & VSS
void DelayFor18TCY( void )         //18 cycles delay
void DelayPORXLCD (void)           //Delay of 15ms
void DelayXLCD (void)              //Delay of 5ms

You can download the full project files (MPLAB XC8 source code and Proteus Schematic design) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download Digital Thermometer Header  

Download Digital Thermometer HEX 

Download Digital Thermometer Main C  

Download Digital Thermometer Proteus