Atmega 32 Interfacing with LDR

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

So I am trying to have an LDR connected in a voltage divider setup to one of the pins of an Atmega32. I am trying to test a digital condition, presence or absence of light, so no ADC involved. However, after setting up my circuit on proteus, am not getting any positive results. The led still lights when I turn one of two switches on my circuit (which are meant to simulate a PIR SENSOR). I personally dont think it's a problem with the voltage divider but instead a problem with my connection of the micro controller. Please help out. As seen in the screenshot below, I have tried the voltage divider on 3 pins already (trial and error) with no success.

 

The code

 

    #define F_CPU 1000000UL
    
    #include<avr/io.h>
    #include<util/delay.h>
    #include <avr/interrupt.h>
    int main()
    {
        DDRB=0x00; //configuring PortB as input
        PORTB=0x00;
        DDRA=0x00; //configuring PortB as input
        PORTA=0x00;
        DDRD=0x00; //configuring PortD as input
        PORTD=0x00;
        DDRC=0xFF; // configuring PortC as output
        PORTC=0x00; // initially off
        
        GICR |= (1<<INT0)| (1<<INT0);
        MCUCR |= (1<<ISC10) | (1<<ISC11);
        
        sei();
        
        while(1)
        {
            if(!(PIND&(1<<0))) // In presence of darkness (When there is darkness pin goes low)
        {
            if((PINB&(1<<1))|(PINB&(1<<2)))            // check for sensor pin PB.1 and PB.2  using bit. The 2 switches.
            {
                PORTC = 0x03;           // buzzer /LED on
                
                _delay_ms(1000);
                
                PORTC = 0x00;
            }
            else
            PORTC=0x00;  // buzzer/LED off
        }
        }
        return 0;
    }  

J.A.

Last Edited: Thu. Aug 22, 2019 - 07:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to AVRFreaks JA.

 

This is a case for using the analog comparators AIN0 and AIN1.

 

The following article talks you through the essential elements of this functionality.

 

https://www.electronicwings.com/...

 

Cheers,

 

Ross

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank You Ross,

Am a novice and might take a while but let me go through that carefully then and we see if it works for me.

 

J.A.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What's connected to pin PIND.0?

 

This looks highly suspicious:

 if((PINB&(1<<1))|(PINB&(1<<2)))    

 

do you really mean:

 

 if((PINB&(1<<1)) || (PINB&(1<<2)))       //PIN B.1 OR PINB.2

 

or...

 

 if((PINB&(1<<1)) && (PINB&(1<<2)))  //PINB.1 AND PINB.2     

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I don't see the LDR in the schematic above, it normally looks like this in a schematic:

Your VCC symbols should ALWAYS point up, never sideways or down, likewise, ground symbols should always point down, and generally, inputs on left side of sheet and outputs on the right.

Take some time to redraw your schematic, neatness counts, your instructor will thank you for presenting a neat schematic!

Rather then using (1<<1) or (1<<2), first define your push buttons then use the symbols in the code, makes it much easier to read and understand.

#define BUTTON1 1
#define BUTTON2 2

.....
if( (PINB & (1<<BUTTON1)) || (PINB & (1<<BUTTON2)) ) //much easier to read with some white space between operators

Jim

 

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Thu. Aug 22, 2019 - 04:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would assume that at some point you will transition from a simulator to using a real chip.

 

For a real chip one generally wants ARef tied to ground with a 0.1 uF by-pass cap, and NOT directly connected to Vcc.

 

Although you might wish to use Vcc as the reference voltage for the ADC, AC, or DAC modules, one generally selects that option through a setup register, using software, and through a direct, hard-wired, external connection.

 

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

R1 and D3 are not connected on the schematic.   The blue LED (D2) needs a resistor: 500 - 1K ohms is OK.

 

R1 and R5 make a voltage divider, but the resulting voltage is 99% of Vcc, because R1 is 1K and R5 is 100K ohms.  Perhaps the values of R1 and R5 should be reversed.

 

With AVRs, push switches are usually implemented with the internal pull-up resistor making the switch pin high when not pressed, and low when pressed.

 

I don't recommend using a triangle shape for Vcc, because this is a common symbol for analog ground (0 volts) when the digital ground is represented by three lines.  Vcc power is usually a single line or small circle with the words "Vcc" or "V+" next to it.

Last Edited: Thu. Aug 22, 2019 - 09:39 PM