Temperature Measurement using LM35 and AVR Microcontroller

In this project, we are measuring the temperature using the LM35 temperature sensor and display the reading on the 16×2 LCD display. After reading the temperature, sensor gives the analog output to the AVR ATmega8 microcontroller.

This analog output from the sensor is sent to the ADC0 of the atmega8 that converts the analog data to the digital by inbuilt ADC in it.

The atmega8 has 6 ADC channel i.e. ADC0-ADC5. It means we can connect six analog sensors. The Port C and ADC pins are multiplexed to each other. The primary function of this pins is as an I/O and secondary is as ADC. To enable the ADC we need to enable it by setting the internal register of the atmega8 by writing the programming code.

The project consists two parts.

  • LCD connection with atmega8
  • LM35 interfacing with atmega8

What is LM35 Temperature Sensor

The LM35 series are precision integrated-circuit temperature sensors, whose output voltage is linearly proportional to the Celsius(Centigrade) temperature. The LM35 does not require any external calibration or trimming to provide typical accuracies of ±1⁄4°C at room temperature and ±3⁄4°C over a full −55 to +150°C temperature range.
Features

  • Calibrated directly in ° Celsius (Centigrade)
  • Linear + 10.0 mV/°C scale factor
  • 0.5°C accuracy guaranteeable (at +25°C)
  • Rated for full −55° to +150°C range
  • Suitable for remote applications
  • Low cost due to wafer-level trimming
  • Operates from 4 to 30 volts
  • Less than 60 μA current drain
  • Low self-heating, 0.08°C in still air
  • Nonlinearity only ±1⁄4°C typical
  • Low impedance output, 0.1 W for 1 mA load

More about LM35 Datasheet.

LM35 Pinout

How to Calculate Analog to Digital Value – Formula

 

Use the following ratio:
e/V max  =  d/2 n– 1 
where

  • V max maximum voltage that the analog signal can assume
  • number of bits available for the digital encoding, Here n = 10
  • present digital encoding
  • present analog voltage from sensor

Example:

How to Calculate the Digital Value if we have 30 °C temperature.
Here
scale factor = 10.0 mV/°C or 0.01V/°C
If we have 30 °C temperature than Voltage = 30 * 0.01 = 0.3V
So e = 0.30V
Vmax = 5V
d = e * 2 n– 1 / V max 
d = 0.30 * 1023/5
d = 61.38 or almost double
Note: Every time we calculate the digital value, it is double to the temperature.
So in programming, we divide it by 2 for getting the approximate value of the temperature.

LM35 Temperature Sensor AVR ATmega8 Connection

LM35 Temperature Sensor AVR ATmega8 Code

#include <avr/io.h>
#include <avr/interrupt.h>
#include<util/delay.h>
#include”lcd.h”
char buffer[5];
int ReadADC(uint8_t ch)
{
ADMUX=ch;
ADCSRA |= (1<<ADSC);
while(!(ADCSRA&(1<<ADIF)));
ADCSRA|=(1<<ADIF);
return(ADC);
}

void initADC()
{
ADMUX=(1<<REFS0);
ADCSRA=(1<<ADEN)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1);//|(0<<ADPS0);
}
int main()
{
lcd_init(LCD_DISP_ON);
lcd_clrscr();
initADC();
int analogVal;
DDRC=0b0000000;
while(1)
{
lcd_puts(“www.maxphi.com”);
lcd_gotoxy(0,1);
lcd_puts(“Temperature=”);
lcd_gotoxy(12,1);
analogVal=ReadADC(0);
analogVal=analogVal/2;
sprintf(buffer,”%d”,analogVal);
lcd_puts(buffer);
_delay_ms(20);
lcd_clrscr();
}
return 0;
}