LM35 Temperature Sensor Interfacing with AVR ATmega16/ATmega32

LM35 Temperature Sensor Interfacing with AVR ATmega16/ATmega32

Introduction

LM35 Temperature Sensor
LM35 Temperature Sensor
LM35 is a temperature sensor which can measure temperature in the range of -55°C to 150°C.
It is a 3-terminal device that provides analog voltage proportional to the temperature. Higher the temperature, 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 the topic LM35 Temperature Sensorin 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.

Example

Let’s interface LM35 temperature sensor with ATmega16 and display the surrounding temperature on the LCD16x2 display.
As LM35 gives output in analog form so connect out pin of a sensor to one of the ADC channel of ATmega16/ATmega32.

Interfacing Diagram

LM35 Temperature Sensor Interfacing with ATmega16/32
LM35 Temperature Sensor Interfacing with ATmega16/ATmega32
Program
/*
 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);
 }
}

No comments:

Post a Comment