LM35 Temperature Sensor Interfacing with AVR ATmega16/ATmega32

LM35 is a sensor that is used to measure temperature. It provides an electrical output proportional to the temperature (in Celsius).

57.8k views2 min readUpdated May 8, 2023
LM35 Temperature Sensor Interfacing with AVR ATmega16/ATmega32

Overview of LM35

LM35 Temperature Sensor
LM35 Temperature Sensor

 

LM35 is a temperature sensor that can measure temperature in the range of -55°C to 150°C.

It is a 3-terminal device that provides an analog voltage proportional to the temperature. The higher the temperature, the higher is the output voltage.

The output analog voltage can be converted to digital form using ADC so that a microcontroller can process it.

For more information about LM35 and how to use it, refer to the topic LM35 Temperature Sensor in the sensors and modules section.

For information about ADC in AVR ATmega16/ATmega32 and how to use it, refer the topic ADC in AVR ATmega16/ATmega32 in the ATmega inside section.

 

Measure Temperature using LM35 With Atmega16/32 Microcontroller

Let’s interface the LM35 temperature sensor with ATmega16 and display the surrounding temperature on the LCD16x2 display.

LM35 gives output in the analog form so connect out pin of a sensor to one of the ADC channels of ATmega16/ATmega32.

 

Connection Diagram of LM35 with ATmega16/32

LM35 Temperature Sensor Interfacing with ATmega16/32
LM35 Temperature Sensor Interfacing with ATmega16/ATmega32

 


 

LM35 Code for ATmega16/32

/*
	LM35 Interfacing with ATmega16/32
	http://www.electronicwings.com
 */ 

#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
#include "LCD_16x2_H_file.h"

#define degree_sysmbol 0xdf

void ADC_Init(){										
	DDRA = 0x00;	        /* Make ADC port as input */
	ADCSRA = 0x87;          /* Enable ADC, with freq/128  */
	ADMUX = 0x40;           /* Vref: Avcc, ADC channel: 0 */
}

int ADC_Read(char channel)							
{
	ADMUX = 0x40 | (channel & 0x07);   /* set input channel to read */
	ADCSRA |= (1<<ADSC);               /* Start ADC conversion */
	while (!(ADCSRA & (1<<ADIF)));     /* Wait until end of conversion by polling ADC interrupt flag */
	ADCSRA |= (1<<ADIF);               /* Clear interrupt flag */
	_delay_ms(1);                      /* Wait a little bit */
	return ADCW;                       /* Return ADC word */
}

int main()
{
	char Temperature[10];
	float celsius;

	LCD_Init();                 /* initialize 16x2 LCD*/
	ADC_Init();                 /* initialize ADC*/
	
	while(1)
	{
	   LCD_String_xy(1,0,"Temperature");
	   celsius = (ADC_Read(0)*4.88);
	   celsius = (celsius/10.00);
	   sprintf(Temperature,"%d%cC  ", (int)celsius, degree_sysmbol);/* convert integer value to ASCII string */
	   LCD_String_xy(2,0,Temperature);/* send string data for printing */
	   _delay_ms(1000);
	   memset(Temperature,0,10);
	}
}

Components Used

Powered byMouser Electronics

Downloads

Comments11

Join the discussion — share a question or tip.

  • NA
    nandintsetseg

    Hi, is there another record that worked on the temperature sensor's atmega32? If so, please help

  • SH
    shani0tnt

    i am using the same code... i added little bit.. i want that when my temp is going to up(40'C) led shound be off and when temp is going low (30'C) then led should be again on.. My code is ..but am not getting result which i want if ((int) celsius==40) { LCD_Command(0x90); LCD_String(" Led Off"); PORTD=0b0000000; // led off if ((int) celsius==30) // if again temp getting low then ... { LCD_Command(0x99); LCD_String("On"); PORTD=0b00000000; // led off }

    • HA
      HarshGupta· edited

      Did you got any solution

    • HA
      HarshRusia

      you have to add. what it is doing in between 30 to 40'c

  • SE
    senafitra5

    i have written a code about this lm35. and it includes a threshold too. pls check if it has wrong code. #include &lt;avr/io.h&gt; #include &lt;util/delay.h&gt; #include "lcd-routines.h" #include&lt;stdio.h&gt; char buffer1[5]; //char buffer2[5]; char buffer3[5]; //int WertADC() //{ // PIN // ADMUX = 0x01; // ADCSRA |= (1&lt;&lt;ADSC); // while( ADCSRA&amp;(1&lt;&lt;ADSC)) //ADSCRA = (1&lt;&lt;ADIF); // return (ADC); //} int main() { DDRC=0; _delay_ms(50); ADCSRA = (1&lt;&lt;ADEN)|(1&lt;&lt;ADIE)|(1&lt;&lt;ADPS0)|(1&lt;&lt;ADPS1)|(1&lt;&lt;ADPS2); ADMUX = (1&lt;&lt;REFS1)|(1&lt;&lt;REFS0); ADMUX = 0x00; int16_t Temp = 0; char SHOWA[3]; int max = 70; int min = 0; lcd_init(); lcd_clear(); ADCSRA |= (1&lt;&lt;ADSC); //ADC_init(); while(1) { Temp = ADC/4; Temp = Temp * 0.48828125; lcd_string("AKT."); lcd_setcursor(8,1); lcd_string("Max"); sprintf(buffer1,"%i",max); lcd_string(buffer1); lcd_setcursor(14,1); lcd_string("°C"); lcd_setcursor(0,2); lcd_string("TMP:"); itoa(Temp,SHOWA,10); lcd_string(SHOWA); //sprintf(buffer2,"%d",Temp); //lcd_string(buffer2); lcd_setcursor(8,2); lcd_string("°C"); lcd_setcursor(10,2) lcd_string("Min"); sprintf(buffer3,"%i",min); lcd_string(buffer3); lcd_setcursor(14,1); lcd_string("°C"); if(Temp&gt;max) { PORTB = 0x01; } if (Temp&lt;min) { PORTB = 0x02;} else {PORTB = 0x04;} _delay_ms(20); lcd_clear(); } return (0); }

  • TR
    trojanhorsedigital· edited

    After Reading your adc_read() function i can say this wrong , bcz i read in data sheet the conversation result save in ADCL and ADCH, WHERE you have to read low bit first and after that high bit, in your code u dint use both resistors. Am i write sir ???

    • LO
      lokeshc

      Yes you are right. But here ADCW register is used which is 16-bit adc register.

  • ME
    mehdiprht

    Hi are you sure about the .hex file ? i think didn't work

    • FA
      faces666· edited

      Hello, you set the fusebit? The processor works on an internal 8MHz oscillator. For the internal clock of 8MHz bits: CKSEL3..0 = "0100". Setting all bits for the 8MHz oscillator: http://mirley.firlej.org/files/ART_fuseM16_04B.gif

    • AU
      authorized· edited

      Replying to @faces666

      i think it may using internal clock. as per datasheet The device is shipped with default CKSEL = “0001” ( Calibrated Internal RC Oscillator ). you need to change it. e.g. for 8MHz CKSEL = "0100" . in above program #define F_CPU 8000000UL line says it using clock of 8MHz.