PDA

View Full Version : Using IR Sensor In AVR



irobocop
02-15-2011, 09:42 PM
Sir..
I connected the output of a ready made ir sensor( giving 3.5v in detection) to the pinc.0 of my atmega8l and i have a led at portd.0 to light up as and when the sensor detects something.. ( just a bit of experimentation as a step in building line follower using 2 sensors) ..but nothing happened...:(
the code is as follows:
(PS: Iam actually a part of team in which nobody knows even a bit of programming ...they are done with chassis...and are now getting on me for programming..but it aint working..PLZ PLZ HELP)
#include <mega8.h>

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0
PORTD=0x00;
DDRD=0x01;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

while (1)
{
if(PINC==0b00000001)
PORTD=0b00000001;

}
}

pratheek
02-16-2011, 09:48 AM
Are you sure your hardware setup is working fine?

First try switching the LED on and off and then if you are successful in that then go for accepting the input from the sensor.

Also, our code doesn't take care of the situation when the sensor is off. You will have to use another conditional statement that switches the LED off when the sensor's output is 0.

Actually, for a simple 2 sensor line follower, a microcontrollerless design is as good as the one with a microcontroller.

irobocop
02-17-2011, 03:28 PM
Thanks for the reply sir..
actually we are building a edge avoider and a line follower is an interim step
Sir..
I did the blinking led experiment( as u said) and its working..
I experimented with all types of inputs(5v/0v with and without pull ups( 4 cases))
The 0v cases worked ok..
But with the 5v cases:
i used the following initialisation for portc
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=0 State0=P
PORTC=0x01;
DDRC=0x02;
....
.....
...
while (1)
{
if(PINC.0==1)
PORTC.1=1;
else
PORTC.1=0;


}
}
In this case the led doesnt go off...
even when the pinc.0 is connected to 5v
while if there are no pullups
the led flickers and gives a constant state at 5v..

IS IT that uC detects 0V BETTER THAN 5V??
plz help..???

pratheek
02-17-2011, 04:39 PM
Enable the internal pull up of the input pin you are using. The pin will get connected to Vcc through a high resistance. This will ensure there is some know potential at the pin even when you haven't connected anything to it. Once you have the internal pull up enabled, wire the switch such that it connects the pin to Gnd every time it is pressed. You can then check the input status of the pin quite reliably.

If you do not have the pull ups, then the pin will be left floating and the microcontroller will be unable to sense the potential at the pin reliably.