Tuesday, November 25, 2008

i have tested my ir-led-photodiode pair for more than 30 different lights and surfaces..... they are just awsome..... awsome but not intelligent (unlike TSOPs).... but you can make them intelligent using your programming skills and some thinking....

with correct technique they can detect presence of white surface (paper in my case) upto 50cm (*conditions applied ) even in the presence of very ir-emitting audience.

The Principle:






in words : Take a reading with IR-led OFF and subtract it from reading when IR-led is ON. This way you get reading because of IR-led only. This principle assumes that the neighbouring IR conditions remains unchanged in between two readings.


Circuit:

  • Atmega8
  • IR-led-Photodiode
  • reflecting surface
  • BC547 or any switching element
  • some discretes



CAUTION: As you can see, there is no resistance in series with IR-led. so if the transistor is kept ON for more than few milliseconds, IR-led will be damaged. By default, the output of atmega pins is high when unprogrammed. So first program the chip and then connect IR-led, or use a toggle switch in between.



Program:

/*Code for controlling IR-led-photodiode sensor using atmega8
internal clock 1MHz
written in AVR Studio 4
*/
#include"avr/io.h"
#include"util/delay.h"

int main(void)
{
DDRB=255; //set port b for output
DDRD=255; //set port d for output
DDRC=0; //set port c for input
ADMUX=0b00100000;
//setting ADC to AREF, left adjusted, ADC0
ADCSRA=0b10000011; //enable ADC, prescale 8
unsigned register char i=0,j=0; //to store ADC results
while(1)
{
PORTB=0b00000001; //turn ON IR-led
_delay_us(20); //wait for turn-ON time
ADCSRA=0b11000000; //start ADC conversion
_delay_us(30); //wait for conversion to complete
//instead of this 30us delay you can use
//while(ADCSRA!=0b10010000);
PORTB=0; //turn OFF IR-led
i=ADCH; //store led-ON value in 'i'
_delay_us(20); //wait for turn-OFF time
ADCSRA=0b11000000; //start ADC conversion
while(ADCSRA!=0b10010000);
//wait for conversion to complete
j=ADCH; //store led-OFF value in 'j'
if(i-j>70)
//compare the difference with threshold value
PORTD=255; //if more than threshold, turn ON LED
else
PORTD=0; //if less than threshold, turn OFF LED
_delay_us(1000); //wait to let IR-LED to cool.
//don't decrease it below 500

}
}



Testing:

here is a video showing my testing results for wall detection at 8-10 cms.



So, how far can it go???thats the question which came in everyone's mind while using ir-sensors. normally, people think that ir-led-photodiode works upto 12-15 cms only. for longer distances we should use TSOP. So here i tested my ir-pair for a distance of 50cms. the main principle is to overdrive the ir-led at higher voltage. with 7V i got 50cms.
(CAUTION : remember, overdriving is risky if not done properly)

Enjoy!!!!


6 comments:

  1. HEY..
    Nice post i must say.. i see a lot of hard work going into this and it's documentation.
    And thank u for being so kind to put ur hard earned techniques open for use by anyone. Specially im surely going to use it and i hope it works..
    Jut a few suggestions for the code that u have written...
    When using ADC and reading the results u have blindly given a time delay for the completion of the conversion but the standard method is to chk the ADIF(adc interrupt flag)....
    better if u use that.

    I will surely try this thing again.. Though earlier the results were not as good but after seeing the videos im sure this thing really works well.. even in direct light!!! gr8
    gr8 work there... congratulations.

    ReplyDelete
  2. Actually, i just copied and pasted the code from a large file. i was using that 30usec for completing my algo related task. this gives me ability to do multitasking. still i will change it in blog for others to learn the trick.
    Thanks!!!!

    ReplyDelete
  3. This is really interesting. Is there any way of getting the atmega8 to give a variable output depending on the distance your hand is from the IR photodiode? Please email me if there is.

    Mark
    jamesmarkjackson@yahoo.co.uk

    ReplyDelete
    Replies
    1. i know i am too late for this to be of any use to you.... Sorry for that... but still answering for others....
      The information about distance is coming in the output from ADC. It is not a linear relation but with few experiments and data collection, you can create a look-up table and use interpolation to get fair results....

      Delete
  4. what about the effect of ambient light! when it comes the results would be worst. If not your design is simply gr8.

    ReplyDelete
    Replies
    1. The problem of ambient light is taken care by the principle explained above... i have tested the same circuit in direct sunlight and in dark room... The only cases where this can fail are :
      1) Case when the ambient lights are flickering within 50us... Possibility of that to happen in natural environment is rare...
      2) Ambient light is strong enough to push the Photodiode to saturation... Well direct sunlight cant do it... but a big bulb placed nearby can....

      Delete