• 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

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.
• 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
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;
ADMUX= (1<<REFS1)| (1<<REFS0)| (1<<MUX1)| (1<<MUX0);
ADCSRA= (1<<ADEN)| (1<<ADFR);
DDRB|=0xFF;
while(1)
{
TEMP=ADCL+ADCH*256; // ADCH is read only to update ADC data register, otherwise no need
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

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
 

welkyb

New Member
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
 
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.
 

ronp849

New Member
Possibly another dumb question - the code you supplied is written in C - Atmel Studio4 uses assembler.
 

welkyb

New Member
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.

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
 

welkyb

New Member
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.
 

ronp849

New Member
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.

Download
 
Last edited:

welkyb

New Member
:)
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:

welkyb

New Member
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;
DDRB=0xFF;
ACSR=0x80; // Analog Comparator: Off
SFIOR=0x00;
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

_delay_us(100);

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

TEMP=0;
for(i=0;i<100;i++)
{
TEMP=TEMP+ADCW;
}
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.

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

Latest threads

EE World Online Articles

Loading
Top