Oct-12th-2008

Interfacing Temperature Sensor – LM35


By interfacing different types of sensors with our MCU we can sense the environment and take decisions, in this way we can create "smart" applications. There are wide variety of sensors available. In this tutorial we will learn about a popular sensor LM35 which is precision centigrade temperature sensor. It can be used to measure temperature with accuracy of 0.5 degree centigrade. We can interface it easily with AVR MCUs and can create thermometers, temperature controller, fire alarms etc.

LM35

LM35 by National Semiconductor is a popular and low cost temperature sensor. It is also easily available. You can buy one from here online. It has three pins as follows.

Fig - LM35 Pin Configuration

 

 

The Vcc can be from 4V to 20V as specified by the datasheet. To use the sensor simply connect the Vcc to 5V ,GND to Gnd and the Out to one of the ADC (analog to digital converter channel). The output linearly varies with temperature. The output is

10MilliVolts per degree centigrade.

So if the output is 310 mV then temperature is 31 degree C. To make this project you should be familiar with the ADC of AVRs and also using seven segment displays. Please refer to following articles.

The resolution of AVRs ADC is 10bit and for reference voltage we are using 5V so the resolution in terms of voltage is

5/1024 = 5mV approx

So if ADCs result corresponds to 5mV i.e. if ADC reading is 10 it means

10 x 5mV = 50mV

You can get read the value of any ADC channel using the function

ReadADC(ch);

Where ch is channel number (0-5) in case of ATmega8. If you have connected the LM35's out put to ADC channel 0 then call

adc_value = ReadADC(0)

this will store the current ADC reading in variable adc_value. The data type of adc_value should be int as ADC value can range from 0-1023.

As we saw ADC results are in factor of 5mV and for 1 degree C the output of LM35 is 10mV, So 2 units of ADC = 1 degree.

So to get the temperature we divide the adc_value by to

temperature = adc_value/2;

Finally you can display this value in either the 7 segment displays by using the Print() function we developed in last tutorial or you can display it in LCD Module. To know how to display integer in 7 segment displays and LCD Modules see the articles.

In this tutorial I have used three 7 segment displays to show the temperature. I have used the xBoard MINI - ATmega8 board to make the project. The complete program is given below.

Program (AVR GCC)


#include <avr/io.h>
#include <avr/interrupt.h>

#include <util/delay_basic.h>


#define SEVEN_SEGMENT_PORT PORTD
#define SEVEN_SEGMENT_DDR DDRD

uint8_t digits[3];   //Holds the digits for 3 displays


void SevenSegment(uint8_t n,uint8_t dp)
{
/*
This function writes a digits given by n to the display
the decimal point is displayed if dp=1

Note:
n must be less than 9
*/
   if(n<10)
   {
      switch (n)
      {
         case 0:
         SEVEN_SEGMENT_PORT=0b00000011;
         break;

         case 1:
         SEVEN_SEGMENT_PORT=0b10011111;
         break;

         case 2:
         SEVEN_SEGMENT_PORT=0b00100101;
         break;

         case 3:
         SEVEN_SEGMENT_PORT=0b00001101;
         break;

         case 4:
         SEVEN_SEGMENT_PORT=0b10011001;
         break;

         case 5:
         SEVEN_SEGMENT_PORT=0b01001001;
         break;

         case 6:
         SEVEN_SEGMENT_PORT=0b01000001;
         break;

         case 7:
         SEVEN_SEGMENT_PORT=0b00011111;
         break;

         case 8:
         SEVEN_SEGMENT_PORT=0b00000001;
         break;

         case 9:
         SEVEN_SEGMENT_PORT=0b00001001;
         break;
      }
      if(dp)
      {
         //if decimal point should be displayed

         //make 0th bit Low
         SEVEN_SEGMENT_PORT&=0b11111110;
      }
   }
   else
   {
      //This symbol on display tells that n was greater than 9
      //so display can't handle it

      SEVEN_SEGMENT_PORT=0b11111101;
   }
}

void Wait()
{
   uint8_t i;
   for(i=0;i<10;i++)
   {
      _delay_loop_2(0);
   }
}

void Print(uint16_t num)
{
   uint8_t i=0;
   uint8_t j;
   if(num>999) return;


   while(num)
   {
      digits[i]=num%10;
      i++;

      num=num/10;
   }
   for(j=i;j<3;j++) digits[j]=0;
}


void InitADC()
{
ADMUX=(1<<REFS0);// For Aref=AVcc;
ADCSRA=(1<<ADEN)|(7<<ADPS0);
}

uint16_t ReadADC(uint8_t ch)
{
   //Select ADC Channel ch must be 0-7
   ch=ch&0b00000111;
   ADMUX|=ch;

   //Start Single conversion

   ADCSRA|=(1<<ADSC);

   //Wait for conversion to complete
   while(!(ADCSRA & (1<<ADIF)));

   //Clear ADIF by writing one to it
   ADCSRA|=(1<<ADIF);

   return(ADC);
}





void main()
{
   uint16_t adc_value;
   uint8_t t;
   // Prescaler = FCPU/1024
   TCCR0|=(1<<CS02);

   //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);

   //Initialize Counter

   TCNT0=0;

   //Port C[2,1,0] as out put
   DDRB|=0b00000111;

   PORTB=0b00000110;

   //Port D
   SEVEN_SEGMENT_DDR=0XFF;

   //Turn off all segments

   SEVEN_SEGMENT_PORT=0XFF;

   //Enable Global Interrupts
   sei();

   //Enable ADC
   InitADC();

   //Infinite loop
   while(1)
   {
      //Read ADC

      adc_value=ReadADC(0);

      //Convert to degree Centrigrade
      t=adc_value/2;

      //Print to display
      Print(t);

      //Wait some time
      Wait();

   }
}

ISR(TIMER0_OVF_vect)
{
   static uint8_t i=0;
   if(i==2)
   {
      i=0;
   }
   else

   {
      i++;
   }
   PORTB=~(1<<i);
   SevenSegment(digits[i],0);

}

Hardware

The hardware of the project is simple you first make the last project (Multiplexed Seven Segment Displays) and then add the LM35 as stated above. You can use the Homemade AVR Devboard or the low cost and fully assembled and tested xBoard MINI. xBoard MINI can be a great tool to learn AVR MCUs at low cost, it can be used to quickly prototype AVR based projects. You can program the xBoard MINI with our USB AVR Programmer.

Fig - Final Digital Thermometer. Temperature read out = 29 C

 

 

Downloads

LM35 Datasheet

Sample Program given above.


17 Responses to “Interfacing Temperature Sensor – LM35”

  1. 1
    Zsolt Says:

    Hello Gupta,
    I’m a beginner with AVRs, so great thanks for the very useful series!

    As I see the LM35 provides -1V to +6V representing the temperature. What is the best practice in this case to convert negative voltages with ADC? Here in Hungary now we have less than -15°C at night :) )

    Thanks in advance!
    Zsolt

  2. 2
    Avinash Says:

    @Zsolt

    Hello.)
    In response to your question i have posted a topic in forum, see it for answers.

    http://forum.extremeelectronics.co.in/viewtopic.php?f=2&t=29

    :)

  3. 3
    Hadi Says:

    I have a question?
    May I have PCB & diagram of this project.
    cas I’m not sure about the circuit!!!!!

  4. 4
    Ritesh Says:

    Hello Avinash

    Do you have any tutorials to send AT commands via C program

  5. 5
    MONIKA ARYA Says:

    hi i m a student of third yr electonics i have recetly made a project using LM35.The name of project is temperature sensor i jst wanted to know what r the advantages of this project and application areas where i can use this project pratically

  6. 6
    Avinash Says:

    Hello Monika,

    Are u talking abt the above project or u got it from some where else?

    Your question seems to be too academic !

    :)

    Well Digital Temperature sensors are used every where, from monitoring CPU temperature in PC to Space stations !!!

    Also in modern refrigerators and Ovens. :)

  7. 7
    praveen Says:

    Hi avinash,
    i tried the above code on my board by deccan robots.When the program is burned its showing b.b.b .I am not able to trace what the problem is.I am using atmega16 Mcu.I have connected lm35 to pin0 of portA,data lines of portD to seven segment display,and portC [2,1,0]to display control lines.Kindly tell what is the problem?Also can u explain what happens once the adc values are read?
    I will explain what i have understood.Once print (t)is executed,it will go to that subroutine and check if no is less than 999 or not.If yes it will calculate digits[o]=(suppose)255%10=5 and num=25,now i will be incremented by 1.After this digits[j]=digits[0]=0. Now it will go to timer overflow rotine and and display the digit by using SevenSegment(digits[i],0);mean while next display is also detected.But i am not able to find out why u have used digits[j].Please explain me?

  8. 8
    teja Says:

    hiii avinash,
    in this ADC conversion u have sex REFERENCE voltage to AVcc i.e REFS0 as 1, and REFS1 as 0, and in the data sheet of ATMEGA8 i have found that, if the reference voltage is set like this, we need to use an oscillator should be connected externally to Aref terminal , is that required? if it is required plzzz tell me from which terminal to terminal v need to connect that oscillator…..thanx for posting all this stuff for us…..

  9. 9
    Avinash Says:

    @Teja

    It is not OSCILLATOR but an INDUCTOR of 10uH

    and YES it is necessary to put that for perfect,clean and noise free performance.

    You can find one here

    http://shop.extremeelectronics.co.in/product_info.php?cPath=24_31&products_id=55

  10. 10
    Ashutosh Says:

    Hi, Avinash can you just help in the CODE to make ADC conversion at slow speed so that the bits doesn’t fluctute while displaying the LM35 value on the seven segment display

  11. 11
    Ashok Says:

    Hi Avinash

    pls give mail me the whole circuit connection of the above project(interfacing LM35 and 3 digit LED display with ATMEGA)

  12. 12
    manikandan Says:

    Hi avinash

    I’m a beginner with AVRs, so great thanks for the very useful series i tried this it works display the temperature but in rotating i want continuous (3 digits SAME time) Where can i to change in program pl help me

  13. 13
    Avinash Says:

    @manikandan

    This is because the CPU is running slow. Make it fast by attaching an external 16MHz crystal and write FUSE BYTE as high=C9 and low=FF

  14. 14
    manikandan Says:

    Hi Avinash,
    Thanks for ur kind help… i changed the oscillator as 16MHz and got a stable display with slit blinking… pls guide me where to change the FUSE BYTE.

  15. 15
    geetha Says:

    hi Avinash,
    I’m a beginner with AVRs, so great thanks for the very useful series, pls help me how to stop the flickering of LED displays… i hav used 16MHZ externally and the digits are visible to eyes but have some flickering.

  16. 16
    Gaurav Parida Says:

    Quote
    “As we saw ADC results are in factor of 5mV and for 1 degree C the output of LM35 is 10mV, So 2 units of ADC = 1 degree.”

    can’t figure out ,how the declaration is made.
    can anybody explain me the above declaration taken from the tutorial.

    Regards,
    -Gaurav Parida

  17. 17
    Norberto Oldfield Says:

    Hey, was just looking through your web site and decided to add the RSS feed, however it’s not working with my webbrowser (I am viewing it with Opera) any kinda way to get around this?

Leave a Reply

Comments

    • Del Chann: Hello is it possible to use this board to interface with a smoke sensor, motion sensor,...
    • Elijah Nicolia: May I consider part regarding your main guide to my personal site
    • Tifany Wiebusch: I go along with you actually, I believe! Might this become doable for you to have...
    • Sinopteek: When i try to run this software under OpenSuSe 11.2 (x64), i’ll have next error:...
    • kapil: @Dhananjay I used that its working and it can programme 3 to 4 times after that there is a...
    • Dhananjay: Sir, At present I am using progisp to flash AT89SXX with usbasp(with modified firmware)....
    • Shashi Jain: plz dont fight wid each other…k i accept my fault… neway m not a rich person...

Video