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 by National Semiconductor is a popular and low cost temperature sensor. It is also easily available. It has three pins as follows.
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.
- Using the ADC of AVRs.
- Using Seven Segment Display.
- Using Seven Segment Display in Multiplexed Mode.
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
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.
- Multiplexed Seven Segment Display.
- Using LCD Modules with AVRs.
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)
#define SEVEN_SEGMENT_PORT PORTD
#define SEVEN_SEGMENT_DDR DDRD
uint8_t digits; //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
n must be less than 9
//if decimal point should be displayed
//make 0th bit Low
//This symbol on display tells that n was greater than 9
//so display can't handle it
void Print(uint16_t num)
uint16_t ReadADC(uint8_t ch)
//Select ADC Channel ch must be 0-7
//Start Single conversion
//Wait for conversion to complete
while(!(ADCSRA & (1<
//Clear ADIF by writing one to it
// Prescaler = FCPU/1024
//Enable Overflow Interrupt Enable
//Port C[2,1,0] as out put
//Turn off all segments
//Enable Global Interrupts
//Convert to degree Centrigrade
//Print to display
//Wait some time
static uint8_t i=0;