# Interfacing LDR with atmega32

I have to make LDR based light switch that turn on lights in per-defined light condition. To do this I have to use atmega32 Micro controller. I follow some tutorials and write some codes. the code compile in Atmel studio without any issues but when I run the code using Proteaus simulator code doesn't work

This is the code

#include <avr/io.h>
#include <util/delay.h>

#define LTHRES 500

{
// AREF = AVcc

// ADC Enable and prescaler of 128
// 16000000/128 = 125000
}

{
// select the corresponding channel 0~7
// ANDing with '7' will always keep the value
// of 'ch' between 0 and 7
ch &= 0b00000111;  // AND operation with 7
ADMUX = (ADMUX & 0xF8)|ch;     // clears the bottom 3 bits before ORing

// start single conversion

// wait for conversion to complete
// till then, run loop continuously

}

int main()
{
char int_buffer[10];
DDRC = 0x01;           // to connect led to PC0

_delay_ms(50);

while(1)
{

// condition for led to glow
PORTC = 0x01;
else
PORTC = 0x00;

}
}


And this is a screenshot of proteaus simulation.

I got this error

[AVR AD CONVERTER] Referance value = 0

I only have basic knowledge of programming micro-controllers and simulation using proteaus. can someone please help me how should I configure this ?

• Perhaps puting R1 from Vcc to LDR is a better solution, LDR grounded directly. Nov 16, 2015 at 8:27
• Proteaus giving me this error. [AVR AD CONVERTER] Referance value = 0 Nov 16, 2015 at 8:59

Make sure u connect AVcc with 5v

and my code for ADC Config

void ADC_Init()
{
DDRA=0x0;               /* Make ADC port as input */

}

{

int Ain,AinLow;

_delay_us(10);
Multiply with weight */
Ain = Ain + AinLow;
return(Ain);                    /* Return digital value*/
}


#define F_CPU 1000000UL // (or 8000000UL or 16000000UL depending upon what
//frequency you want your Atmega32 to run at)


before or after you include util/delay.h Proteus requires your Atmega's frequency to be determined beforehand.

Comment if it still doesn't work out!

edit: not an arduino board. AVCC and AVREF still need to be hooked up, however.

An arduino board has more electronics on it than just a single ATMEGA and an LED (although not too much more).

Make sure that your AVCC and AVREF pins are hooked up to the correct voltages, and also make sure your XTAL pins are connected to a crystal and some capacitors.

See if you can find a schematic for your particular Arduino - that should show up exactly how to set up the chip's voltage, ground, and crystal pins.

• Thanks but I cant use Arduino. I have to do this using Atmega32 alone Nov 16, 2015 at 8:34
• Have you done a 'hello-world' in your simulator? Maybe loaded a simple program that just flashes the LEDs a few times and then terminates? Nov 16, 2015 at 8:40
• Yes I did and part of my simulation working perfectly. (relay part) but I want to measure LDR value and turn OFF/ON relay. to do this I have to do ADC. program compiles successfully but when I run it in Proteaus I got an error. I have to make this before tomorrow night. Could you please help me sir :( Nov 16, 2015 at 8:48
• Proteaus giving me this error. [AVR AD CONVERTER] Referance value = 0 Nov 16, 2015 at 8:56
• Try connecting AREF and AVCC to the right values. I'm guessing you don't care what the values are, so try connecting both of them to 5V and see if your circuit runs then. Nov 16, 2015 at 9:03