Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

atmega8L avr with temp sensor

Not open for further replies.


New Member
I am doing my first avr exp using a temp sensor LM35 to blink blue green or red led according to the temp of the room.
• Connected the leds to PORT B of atmega8L uC and temp sensor to ADC 3 input.
• Using internal oscillator 1MHz as clk, internal reference voltage of 2.56 as reference voltage in FREE RUNING MODE.
• ADLAR = 0 i.e. right adjusted.
• Connected a 0.1uF decoupling capacitor at VREF pin as per datasheet, “Internal 2.56V Voltage Reference with external capacitor at AREF pin”.
• Using prolight leds 1W (for good intensity), transistor in saturation with Ic around 10mA.
According to what I hv understood with temp sensor 10.0 mV/˚C scale factor, output voltage of sensor is input voltage of ADC …...
ADC = ( Vin * 1024 / Vref )
If room temp= 30˚C, Vin = 300mV = 0.3V, So ADC = (.3*1024/2.56)=120. So I hv divided the result by 4.

Fig b.png

Here is my code that I hv written in AVR Studio4
int main (void)
unsigned int TEMP;
ADMUX= (1<<REFS1)| (1<<REFS0)| (1<<MUX1)| (1<<MUX0);
ADCSRA= (1<<ADEN)| (1<<ADFR);
TEMP=ADCL+ADCH*256; // ADCH is read only to update ADC data register, otherwise no need
else if(TEMP>20&&TEMP<=28)
else if(TEMP>28&&TEMP<=35)
PORTB= ((1<<PB1)| (1<<PB0)| (1<<PB3));

But I don’t know what the problem is; uC only blinks the led which satisfies the minimum criteria for temp i.e. blue. Even if I multiply the final TEMP variable by 10000 and keep min temp criteria for blue led to blink as less than 2˚C, then also only blue led blinks. It seems the uC is taking no voltage as input.
I hv checked that if I write (PORTB=1<<PB0), then green led blinks or (PORTB=1<<PB3), then red led blinks instead of (PORTB=1<<PB1)

I have connected output pin of sensor directly to uC, is there a problem in it or do I need to use more decoupling capacitors somewhere in the circuit.

plz help


  • b.png
    15 KB · Views: 1,532
Last edited:
Have only built a few programs for the AVR, so take with a grain of salt. It looks like you have not started the adc conversion, nor specified the clock in ADCSRA register:

; ADCSRA - (b7)adc enable(b6)start conversion(b5)free running mode
;(b2:b0)110 is clk/64 or 8mhz/64 or 125ksps
ldi temp,0b11100110	
sts ADCSRA,temp
thank you for replying.
i am using calibrated Internal RC Oscillator at 1megahz freq so i am not using any scaling factor. can it be done this way?
or should i use internal RC Oscillator freq of 2megahz and then use scaling factor of two.

and for starting the adc conversion i directly used free running mode " (1<<ADFR) " ,
but i think i also need to give a command to initiate the conversion. i ll try it..
As I understand it, having the ADCSRA, ADPSO 2:0 bits set at 0,0,0 gives you a divisor of 2 and an adc sample rate of 500 ksps, which would be out of spec. Having the prescaler at 8 with a 1 Mhz clock gives you 125 ksps, which is good.

Probably also a good idea to watch for the adc conversion flag before reading the register values. Just one of many AVR adc tutorials out there Using the Analog To Digital Converter. | eXtreme Electronics

It may be a dumb question / observation but you are you using an LM35? Your schematic shows a AD590, that device provides a current output, not voltage.
Possibly another dumb question - the code you supplied is written in C - Atmel Studio4 uses assembler.
i didn't know that the adc frequency has to be between 50k-200k. i ll also check for the adc flag by running in sigle conversion mode.

Angry Badger,
its not at all a dumb question. Well, you are right. .actually what i am using is LM35 but i could not find that component in eagle library ,so picked some temp sensor..

Thank you
no. not at all . may be bcz i am new to avr uC, so i am making mistakes.
But I wrote my code in C in avrstudio4, built all hex and other files through it and even debugged with its help. And when i inputted some random value in ADCL register myself, correct led pin of PORT B also gets selected.
Ok - Have you tried Codevision CVAVr - there is a free version with limited code space - it has a wizard that writes all the initialisation code for you. I have used the commercial version for years - Brilliant.

**broken link removed**
Last edited:
i hv been able to do first part of my project.
thank you to all.

i used codevision, its a bit complex but it initialized all registers and made my work easy.
Last edited:
i got this format from codevision, i hv modified it.
In order to start the ADC in free running mode the ADSC bit has to be set
in the ADSCRA reg.
Internal rc oscillator frequency as 8megaHz and ADC prescalar as 128

int main(void)
unsigned char i;
ACSR=0x80; // Analog Comparator: Off
while (1)
ADMUX=0xC0; // ADC Voltage Reference: Int., cap. on AREF
ADCSRA=0x87; // ADC Clock frequency: 62.500 kHz
ADMUX|= 3; // 3 bcz i am using adc channel 3

// Delay needed for the stabilization of the ADC input voltage


ADCSRA|=0x40; //Initialize ADC in single conversion mode

while ((ADCSRA & 0x10)==0); // Wait for the AD conversion to complete
ADCSRA|=0x20; //Initiatialize free running mode

else if(TEMP>25&&TEMP<=35)
else if(TEMP>35&&TEMP<=40)
Well, learned a little something there, Thanks.

The small delay in the code explains why the my first read in single shot mode was always erroneous. Also the single shot/start mode to free running mode sequence seems more like how things are explained in the data sheet.

Good luck with your project.
Not open for further replies.

New Articles From Microcontroller Tips