Thursday, April 7, 2011

Interfacing LM35 Temperature Sensor with PIC Microcontroller.

The are many cool sensors available now a days, ranging from IR distance sensor modules, accelerometers, humidity sensors,temperature sensors and many many more(gas sensors, alcohol sensor, motion sensors, touch screens). Many of these are analog in nature. That means they give a voltage output that varies directly (and linearly) with the sensed quantity. For example in LM35 temperature sensor, the output voltage is 10mV per degree centigrade. That means if output is 300mV then the temperature is 30 degrees. In this tutorial we will learn how to interface LM35 temperature sensor with PIC18F4520 microcontroller and display its output on the LCD module.
First I recommend you to go and read the following tutorial as they are the base of this small project.
After reading the ADC tutorial given above you will note the the PIC MCU's ADC gives us the value between 0-1023 for input voltage of 0 to 5v provided it is configured exactly as in the above tutorial. So if the reading is 0 then input is 0v, if reading is 1023 then input is 5v. So in general form if the adc read out is val then voltage is.

unsigned int val;
val=ADCRead(0); //Read Channel 0
voltage= ((val)/1023.0)*5;

The above formula give voltage in Volts, to get Voltage in mili Volts (mV) we must multiply it with 1000, so

voltage=((val)/1023.0)*5*1000); //Voltage is in mV

since 10mV = 1 degree, to get temperature we must divide it by 10, so

t=((val)/1023.0)*5*100); //t is in degree centigrade

simplifying further we get

t=((val/1023.0)*500);
t=(val*0.48876);

we round off this value, so

t=round(val*0.48876);

Hardware for LM35 based thermometer.

You will need a PIC18F4520 chip running at 20MHz attached with a standard 16x2 LCD Module andLM35 on AN0 pin. LM35 is a 3 pin device as show below.
lm35 temperature sensor pinout

Fig.: LM35 Temperature Sensor Pinout



connect the +Vs Pin to 5v and GND to GND. The output must be connected to the analog input pin 0 of the PIC18F4520 MCU. It is labeled AN0 in the datasheet. It is pin number 2 on the 40 pin package. It is also called RA0 because it is shared with PORTA0.
We will use our 40 PIN PIC Development board to realize the project. The base board has all the basic circuit to run the PIC. The extra part required for this project like LCD and the LM35 temperature sensor are installed in the expansion board.
lm 35 temperature sensor demo

General Notes

  • For proper working use the components of exact values as shown above.
  • Wherever possible use new components.
  • Solder everything in a clean way. Major problems arises due to improper soldering,solder jumps and loose joints.
  • Use the exact value crystal shown in schematic.
  • Only burning the HEX file to the MCU is NOT enough. PIC18 devices are fairly complex MCU and can be configured in various ways. Chip is configured using the CONFIG Bytes. Although all hex file given in our site comes with embedded CONFIG bytes. But the user must ensure they are programmed to the chip. Any good programmer has the capability to read the configuration information from the hex file and transfer it to the MCU. Programs will not run without proper configuration byte programming. One major job of configuration is to setup proper oscillator and PLL modes without which the MCU won't execute a single instruction.
  • To compile the above code you need the HI-TECH C and MPLAB IDE. They must be properly set up and a project with correct settings must be created in order to compile the code. So I request you to read the following articles to become familiar with the built steps.
  • To understand the code you must have good knowledge of core C language. Please don't be confused with the basic concept of the language.
  • You must be familier with project concept and multi source file concept that used used in most professional languages like C.
  • You need Proteus VSM if you want to develop or debug the project without any hardware setup.

No comments:

Post a Comment