Continue to Site

# atmega8L avr with temp sensor

Status
Not open for further replies.

#### welkyb

##### New Member
Hi,
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.
• 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
IN ATTACHMENT AS A THUMBNAIL

Here is my code that I hv written in AVR Studio4
#include<avr\io.h>
int main (void)
{
unsigned int TEMP;
DDRB|=0xFF;
while(1)
{
TEMP=TEMP/4;
if(TEMP<=20)
PORTB=1<<PB1; //BLUE
else if(TEMP>20&&TEMP<=28)
PORTB=1<<PB0; //GREEN
else if(TEMP>28&&TEMP<=35)
PORTB=1<<PB3; //RED
else
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

#### Attachments

• 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:

Code:
; 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

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

Hi,

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.

nickelflippr,
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.

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

ronp849,
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.

Last edited:

i hv been able to do first part of my project.
thank you to all.

ronp849,
i used codevision, its a bit complex but it initialized all registers and made my work easy.

Last edited:
Curious as to what the ADSRA and ADMUX registers were set at?, whether single shot, free running, and the Osc clock rate.

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
Internal rc oscillator frequency as 8megaHz and ADC prescalar as 128

int main(void)
{
unsigned char i;
DDRB=0xFF;
ACSR=0x80; // Analog Comparator: Off
SFIOR=0x00;
while (1)
{

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

_delay_us(100);

while ((ADCSRA & 0x10)==0); // Wait for the AD conversion to complete

TEMP=0;
for(i=0;i<100;i++)
{
}
TEMP/=400;
if(TEMP<=25)
PORTB=1<<PB1; //BLUE
else if(TEMP>25&&TEMP<=35)
PORTB=1<<PB0; //GREEN
else if(TEMP>35&&TEMP<=40)
PORTB=1<<PB3; //RED
else
PORTB=(1<<PB1)|(1<<PB0)|(1<<PB3);
}
}

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.

Status
Not open for further replies.

Replies
1
Views
4K
Replies
0
Views
3K
Replies
0
Views
1K
Replies
0
Views
2K
Replies
0
Views
1K