Pantech.AI

How to Interface LM35 Temperature Sensor with PIC18F4550

Overview of LM35

The LM35 is a temperature sensor capable of measuring temperatures from -55°C to 150°C. This 3-terminal device provides an analog voltage output that is directly proportional to the sensed temperature. As the temperature rises, the output voltage increases accordingly.

To allow a microcontroller to read this data, the analog output from the LM35 can be converted to digital using an ADC (Analog-to-Digital Converter).

For more details on the LM35 and its applications, refer to the “LM35 Temperature Sensor” section in Sensors and Modules. For guidance on using the ADC feature in the PIC18F4550, check the “ADC in PIC18F4550” section in the PIC Inside area.

Connection Diagram of LM35 temperature Sensor to PIC18F4550

LM35 Temperature Sensor Interfacing with PIC18F4550

Read Temperature using LM35 and Display on LCD16x2 using PIC18F4550

Let’s interface the LM35 temperature sensor with the PIC18F4550 and display the ambient temperature on an LCD16x2 display.

Since the LM35 outputs an analog signal, connect its output pin to one of the ADC channels on the PIC18F4550. This allows the microcontroller to read and process the temperature data for display.

LM35 Code for PIC18F4550

/*
 *LM-35 Temperature Sensor Interfacing with PIC18f4550
 *http://www.electronicwings.com
 */

#include <stdio.h>
#include <string.h>
#include <p18f4550.h>
#include "Configuration_Header_File.h"          /* Header File for Configuration bits */
#include "LCD_16x2_8-bit_Header_File.h"                 /* Header File for LCD Functions */
#include "PIC18F4550_ADC_Header_File.h"

void main()
{    
    char Temperature[10];    
    float celsius;
    int i;
    OSCCON=0x72;                /* set internal Oscillator frequency to 8 MHz*/
    LCD_Init();                 /* initialize 16x2 LCD*/
    ADC_Init();                 /* initialize 10-bit ADC*/
    
    while(1)
    {   
        LCD_String_xy(0,0,"Temperature");
       /* convert digital value to temperature */
        celsius = (ADC_Read(0)*4.88);
        celsius = (celsius/10.00);
       /*convert integer value to ASCII string */
        sprintf(Temperature,"%d%cC  ",(int)celsius,0xdf); 
        LCD_String_xy(1,0,Temperature);                /* send string data for printing */    
        MSdelay(1000);
        memset(Temperature,0,10);
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *