Temperature Sensor LM35 Interfacing With PIC Microcontrollers

Previous Tutorial Previous Tutorial Tutorial 23 Next Tutorial Next Tutorial
LM35 Temperature Sensor Interfacing
Introductory Level ★☆☆☆☆

 

In this tutorial, we’ll discuss how to interface the LM35 temperature sensor with a PIC microcontroller. Using the ADC to get the analog output voltage of the sensor then converting it back to Celsius degrees, and finally display the result on an LCD and also send it via serial port to a host PC to monitor the temperature remotely. So, let’s get started!

[toc]


   Required Components   

Qty. Component Name Buy On Amazon.com
1 PIC16F877A Add
1 BreadBoard Add
1 LM35 Sensor Add
1 Alphanumeric LCD 16×2 Add
1 USB-TTL (FTDI) Serial Converter Add
1 Jumper Wires Pack Add    Add
1 LM7805 Voltage Regulator (5v) Add
1 4MHz Crystal Oscillator Add
1 PICkit2 or 3 Programmer Add

The Prototyping Board Setup

Prototyping Board - Embedded Systems Tutorials With PIC MCUs


   LM35 Temperature Sensor   

 

LM35-Temperature-Sensor

The LM35 is a temperature sensor widely used in electronic projects and midrange devices. It has limited usage in industrial applications due to maximum temperature range limitations. It’s rated to a full range of −55°C to 150°C. You can just power it up and instantly read the voltage level on the output terminal. The Vout of the sensor directly maps to the sensor’s temperature as we’ll see hereafter.

 Features 

  • Calibrated Directly in Celsius (Centigrade)
  • Linear + 10-mV/°C Scale Factor
  • 0.5°C Ensured Accuracy (at 25°C)
  • Rated for Full −55°C to 150°C Range
  • Low-Cost
  • Operates From 4 V to 30 V
  • Less Than 60-μA Current Drain
  • Non-Linearity Only ±¼°C Typical
  • Low-Impedance Output, 0.1 Ω for 1-mA Load

 Pinout 

LM35-temperature-sensor-Pinout

 V-T Characteristics 

LM35 Transfer Function

The accuracy specifications of the LM35 are given with respect to a simple linear transfer function:
VOUT = (10 mv / °C) × T
where VOUT is the LM35 output voltage & T is the temperature in °C

LM35 Temperature Sensor Operating Ranges

 Typical Applications 

  • Low-Temperature Measurements
  • Battery Charging Management
  • Power Supply Monitoring
  • Small Boilers
  • HVAC
  • And a variety of electronic projects

 Alternatives For LM35 

  • Negative Temperature Coefficient (NTC) thermistor. The effective operating range is -50 to 250 °C.
  • Resistance Temperature Detector (RTD), also known as a resistance thermometer. Platinum RTDs offer a fairly linear output that is highly accurate (0.1 to 1 °C) across -200 to 600 °C. While providing the greatest accuracy, RTDs also tend to be the most expensive of temperature sensors.
  • Thermocouple. Thermocouples are nonlinear sensors, requiring conversion when used for temperature control and compensation, typically accomplished using a LUT (lookup table). Accuracy is low, from 0.5 °C to 5 °C. However, they operate across the widest temperature range, from -200 °C to 1750 °C.

   Extracting Information From Analog Signal   

 

 Analog Sensors 

There is a variety of electronic sensors that produce an analog output instead of digital data that could be directly manipulated by our digital micro-computers (MCUs). The output is typically a low voltage signal (0->3.3 or 5v). Which directly maps to the physical parameter being measured by the sensor (e.g. Temperature, pressure, light intensity, sound level, etc).

Usually, the transfer functions of analog sensors are linear for easier calculations and more accurate results under operating conditions.

 Using ADC 

Reading an analog sensor involves using an ADC or A/D (Analog To Digital Converter). Which helps the microcontroller to evaluate the voltage level on an analog input pin and converters it to a digital value for further manipulations and storage.

LM35 Temperature Sensor Tutorial

<< Check Out The ADC Tutorial >>

ADC With PIC Tutorial Thumbnail

There are many ways/schemes to read an ADC with your microcontroller. Every aspect of ADC is completely presented in the above tutorial, so check it out. Hereafter in this tutorial, we’ll be using a couple of functions which we’ve previously created. First of which, is the ADC initialization routine. And the other one initiates an A/D conversion and returns back the 10-Bit AD Result.

 Result Conversion 

The output of an ADC will be a digital (numeric) value stored in a memory register. Which means you’ll have to manipulate & convert this value to estimate the voltage level on the analog input pin. Therefore, you’ll be able to use the transfer function equation of your sensor to know the physical parameter real measurement. Whether it’s temperature, pressure, or light intensity, it’s all the same as seen by the processor.

Numeric Example

[Assuming 10-Bit ADC with Vref=5v and Temperature Sensor Transfer Function of 10mV/°C]

Let’s start with an A/D Result of 100

=> ADRES = 100

So, The Sensor’s Output Voltage (VOUT) is calculated as follows

=> ADC_Resolution = FSR/2^10 = 5v/1024 = 4.88mV

=> VOUT = ADC_Resolution x ADRES = 0.488Volts

Therefore, The Temperature Measured By The Sensor is as follows

=> Temperature = VOUT/10mV = 48.82°C


  LM35 Temperature Sensor + LCD With PIC – LAB1  

 

Lab Name LM35 Temperature Sensor Interfacing + LCD Display
Lab Number 22
Lab Level Beginner
Lab Objectives Learn how to configure The ADC Module and read analog signals. Interface The LM35 Temperature Sensor and print out the result on an alphanumeric LCD screen.

 

       1. Coding       

 

Open the MPLAB IDE and create a new project name it “LM35_LCD”. If you have some issues doing so, you can always refer to the previous tutorial using the link below.

timer-preloading

Set the configuration bits to match the generic setting which we’ve stated earlier. And if you also find troubles creating this file, you can always refer to the previous tutorial using the link below.

timer-preloading

Now, open the main.c file and let’s start developing the firmware for our project.

The first task is to initialize the ADC module using the basic firmware drivers which we’ve already built previously. Then, we’ll read convert the voltage level on Analog Channel 0 and convert the 10-Bit A/D conversion result to temperature in Celsius and print it out to the LCD.

Here is the Full Code Listing For This LAB

 

       2. Simulation       

 

Here is an animation for the running simulation tests.

ML35-Temperature-Sensor-With-LCD-PIC

 

       3. Prototyping       

 

Wiring up this schematic on a breadboard should be an easy task. Just upload your firmware hex file to the microcontroller chip. And hook up the input power rails and start testing it up!

Here is a video for the final output of this LAB.

 

Download Project


 LM35 Temperature Sensor + Serial – LAB2 

 

Lab Name LM35 Temperature Sensor Interfacing + Serial Monitor
Lab Number 23
Lab Level Beginner
Lab Objectives Learn how to configure The ADC Module and read analog signals. Interface The LM35 Temperature Sensor and print out the result to the serial monitor on your computer.

 

       1. Coding       

 

Open the MPLAB IDE and create a new project name it “LM35_Serial”. If you have some issues doing so, you can always refer to the previous tutorial using the link below.

timer-preloading

Set the configuration bits to match the generic setting which we’ve stated earlier. And if you also find troubles creating this file, you can always refer to the previous tutorial using the link below.

timer-preloading

Now, open the main.c file and let’s start developing the firmware for our project.

The first task is to initialize the ADC module using the basic firmware drivers which we’ve already built previously. Then, we’ll read convert the voltage level on Analog Channel 0 and convert the 10-Bit A/D conversion result to temperature in Celsius and print it out to the serial monitor.

Here is the Full Code Listing For This LAB

 

       2. Simulation       

 

Here is an animation for the running simulation tests.

LM35 Temperature Sensor With UART PIC Microcontroller

 

       3. Prototyping       

 

Wiring up this schematic on a breadboard should be an easy task. Just upload your firmware hex file to the microcontroller chip. And hook up the input power rails and start testing it up!

LM35 Temperature Sensor Serial With PIC Microcontroller

Here is a snapshot for the final output of this LAB. Here I’m using the Arduino’s serial monitor to display the serial data being received by my pc from the microcontroller proto-board

LM35 Temperature Sensor Serial Data With PIC Microcontroller

 

Download Project

 

if you’ve found this helpful, then please share it with your network. And good luck for your next projects!

 

Previous Tutorial Previous Tutorial Tutorial 23 Next Tutorial Next Tutorial

 

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

3 thoughts on “Temperature Sensor LM35 Interfacing With PIC Microcontrollers”

    • Give a look at the datasheet for the adc chapter. You’ll find that the adcon0 register is responsible for choosing the adc channel. And also the numer of channel maps to the value to be written to that register. That’s what we do here

      Reply

Leave a Comment