Friday, 31 May 2013

How to Use IR LED and Photodiode with Arduino

      In this post I am going to show you how to use and program IR LED and Photodiode pair with Arduino to detect obstacles in a short range. Before going to do the project let us have a brief look at the IR LED and Photodiode.

IR LED:

How-to-Use-IR-LED-and-Photodiode-with-Arduino                   IR LED means Infrared Light Emitting Diode. The IR LED emits Infrared light which is not visible to human eye. we can find these IR LED's in our TV Remotes. IR LED's works like normal LED's but the material used in the core is different, it emits Infrared Light when current passed through it. These IR LED are used to detect obstacles ahead of the robot. The IR LED emits IR light which gets reflected if any obstacle is present in the direction of emitted IR ray, the reflected IR ray caught by Photodiode which calculates the reflected light strength. The higher the reflected IR
ray strength, the closer is the obstacle and vice-verse

Photodiode:

How-to-Use-IR-LED-and-Photodiode-with-Arduino                  Photodiode is a light sensitive semi-conductor diode which converts the light energy into voltage or current based on the mode of operation. In general Photodiodes are operated in reverse bias condition. The clear Photodiode can detect visible and IR rays to limit the Photodiode to detect only IR rays a black cotting is applied to the glass of the Photodiode. The photodiode allows the current to pass through it if the photodiode is exposed to IR rays and it doesn't allow current to pass through it if no IR rays falls on it. The amount of current passed through the photodiode is directly proportional to amount of IR rays falls on it.

IR LED and Photodiode Project:

                  In this project I am going to show you How to use the IR LED and Photodiode pair to detect the obstacle in-fornt of it. I am going to program the Arduino such that, If an obstacle is present before the IR LED and Photodiode pair with in the threshold range then a buzzer will ring.


Materials required:

1) Arduino                        2) IR LED                      3) Piezo buzzer                         4) some Wires.

Circuit diagram:

How-to-Use-IR-LED-and-Photodiode-with-Arduino

How circuit works:

                 In the above circuit the Photodiode is operated in Reverse bias condition i.e., the long leg of photodiode goes to ground and the short leg is connected to 5 Volts supply through 3 K ohms resistor. When the photodiode detects IR rays from the IR LED which is reflected by an obstacle the photodiode conducts then, the current goes to the ground through the photodiode so, the current to the analog pin A0 of Arduino is low so that, we will get low values (around 500) from the analog pin A0 of arduino. In case of no IR rays falls on the photodiode the photodiode doesn't conduct so the current from the digital pin 2 goes to analog pin A0 through the 3 K ohms resister so, the readings from the analog pin A0 of Arduino will be around 900.

Program :

  int pd=2;                      //Photodiode to digital pin 2
 int buzz=13;                   //piezo buzzer to digital pin 13  
 int senRead=0;                 //Readings from sensor to analog pin 0  
 int limit=850;                 //Threshold range of an obstacle  
 void setup()    
 {  
  pinMode(pd,OUTPUT);  
  pinMode(buzz,OUTPUT);  
  digitalWrite(pd,HIGH);       //supply 5 volts to photodiode  
  digitalWrite(buzz,LOW);      //set the buzzer in off mode (initial condition)  
  Serial.begin(9600);          //setting serial monitor at a default baund rate of 9600  
 }  
 void loop()  
 {  
  int val=analogRead(senRead);  //variable to store values from the photodiode  
  Serial.println(val);          // prints the values from the sensor in serial monitor  
  if(val <= limit)              //If obstacle is nearer than the Threshold range  
  {  
   digitalWrite(buzz,HIGH);     // Buzzer will be in ON state  
   delay(20);  
  }  
  else if(val > limit)          //If obstacle is not in Threshold range  
  {  
   digitalWrite(buzz,LOW);      //Buzzer will be in OFF state  
   delay(20);  
  }  
 }  

How program works:

                 The program starts with Initializing variables photodiode 'pd' with digital pin 2, buzzer 'buzz' with digital pin 13, sensor readings 'senRead' to analog pin 0 and the limit variable is set to 500 (reading from the sensor). In the 'void setup()' method the pin mode is definde with the function 'pinMode(variable,OUTPUT/INPUT)'. In the 'void loop()' method the 'val' variable stores the readings from the sensor. The 'Serial.println(val);' method is used to print the values from the sensor to the serial monitor. I had used an if - else ladder to set the buzzer in ON state when the obstacle is in the Threshold range otherwise the Buzzer will be in OFF state.

                If you have any trouble related to this post then, express it in the comment box below.

Related posts :

68 comments:

  1. very useful post bro

    ReplyDelete
  2. Hi ,

    Thanks Very much :)

    I understood all concepts and the code you entered.

    But when I tried it practically on a bread board, the buzzer always rings !

    can you help me !

    ReplyDelete
    Replies
    1. I had updated the code please check it now, It will work fine

      Delete
  3. thanks for this useful post..
    can you suggest how to apply it on something related to medical?

    ReplyDelete
    Replies
    1. IR LED and Photodiode pair cannot give you accurate distance but They can help you to find any obstacle in front of it.
      So, you can use this in any medical application which requires obstacle detection.
      The main advantage of IR LED is - it does'nt emmit any visible light.

      Delete
  4. I like the way of representing circuit diagrams, thanks

    ReplyDelete
  5. Olá Kranthi kumar,
    Posso usar o mesmo para detectar chuva?
    Tenho ideia que o LedIR irá reflectir luz para o fotodiodo quando pingos de água se encontrarem na frente

    ReplyDelete
    Replies
    1. No, IR LED's are not suitable to detect rain.

      Delete
    2. Use an "humidity detection sensor module" to detect rain which will cost you around $2.60.
      all the best for your project.

      Delete
  6. i tried to connect the circuit but it doesn't work. when i remove the digital pin (2) the buzzer is turned on. but when i connect it, even though i put obstacle into the IR led, the buzzer does not sound.

    ReplyDelete
    Replies
    1. It works now.. thank you for the tutorial

      Delete
    2. hi arvin. i am getting the same thing. how did you correct it??

      Delete
  7. why my ir sensor value is constant it doest change .plese help.

    ReplyDelete
    Replies
    1. IR LED showing constant value means there may be some loose connections in your circuit check them and try again

      Delete
  8. if i use a pot in series with 3k will my sensitivity increse.

    ReplyDelete
    Replies
    1. If you want more resolution/sensitivity use an OP-Amp to boost the output signal

      Delete
  9. Thank you,this article was very useful,not only did u present the diagram very well but u clearly explained how it works

    ReplyDelete
  10. excellent post, I tried and working as expected

    ReplyDelete
  11. Buzzers Is Always On When i Connect The Photodiode..:( Please Help

    ReplyDelete
    Replies
    1. Its working fine Dylan Netto. please check your connections again.

      Delete
  12. Yeah It Worked...My Diode Was Bad..:-P Thanks..

    ReplyDelete
  13. hii, can i ask this circuit suitable for smoke detector used? thank you~

    ReplyDelete
  14. Can you please suggest me on the photodiode to be used?
    Should it be a normal photodiode or ir sensitive photodiode?
    Can you please tell me the specifications of the photodiode you used?

    ReplyDelete
    Replies
    1. photodiodes are normally IR sensitive. Just ask shopkeeper to give 5mm Photodiodes

      Delete
  15. i'm using LED instead of buzzer what must b the connection and the code change please anyone help me this is my semester project

    ReplyDelete
    Replies
    1. no need to change anything connect the led with a resistor in series (150 ohms). connect the positive terminal of led to pin 13 connect the negative terminal of led to 150 ohms resistor then connect the other end of the resistor to ground terminal of Arduino.

      Delete
  16. i'm using left servo and right servo to drive robot instead of buzzer what must b the connection and the code change please anyone help me this is my semester project

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. useful tutorial, but i want to make connections on proteus and i don't know which devices to choose(code of the devices like photodiode)

    ReplyDelete
    Replies
    1. Sorry abeer jaradat, I dont know much about proteus. Just google it.

      Delete
  19. First of all my project is ''Fire fighting robot using arduino" now i am tried my best on obstacle avoidance using two IR LED and photo diode(Lascels SX 100-055) with 330ohm to IR LED and 1K to photo diode but still it is not working how can i fix it.please help me both the circuit and arduino code.by the way my robot is 2W continuous servo

    ReplyDelete
    Replies
    1. For your project Two sensors are not enough. Your bot needs sensors at the front and also at the corners and front-left and front-Right. More the number of sensors better the performance. I suggest you to refer Micromouse robot.

      Delete
  20. Where do we write the code(which software) and how do we flash it?

    ReplyDelete
    Replies
    1. You have to write the code in Arduino IDE you can find more information about arduino on following site -> www.arduino.cc

      Delete
  21. very useful post :D thanks!!!!!

    ReplyDelete
    Replies
    1. Thanks for your feedback @GabrieleLiu

      Delete
    2. Hi Kumar, i really enjoyed ur posts and have learnt alot from it. Pls Kumar wud b grateful if i can get ur email adress so we can talk more.I have lots of questions 2 ask about the obstacle avoidance.
      my email address is: anjelotic1@gmail.com

      Delete
  22. hi. Im a newbie and I just brought a set or arduino kit.could you help me with the programming code in controlling a led and a servo with a IR LED?

    ReplyDelete
  23. Hi Kumar, i really enjoyed ur posts and have learnt alot from it. Pls Kumar wud b grateful if i can get ur email adress so we can talk more.I have lots of questions 2 ask about the obstacle avoidance.
    my email address is: anjelotic1@gmail.com

    ReplyDelete
  24. This is a great post! Simple, clear, and well explained. I was going to do something very similar to this, but you beat me to it! Well done!

    ReplyDelete
  25. Thank you very much. i have a problem of this. how can i get accurate values from this?

    ReplyDelete
    Replies
    1. If you want to get more accurate values with more resolution - then use photo transistor instead of photodiode

      Delete
  26. good and helpful tutorial on IR. thank you.
    a.k.a Plustwo

    ReplyDelete
  27. good and helpful tut on IR pair, thank you. a.k.a Plustwo

    ReplyDelete
  28. Hi there

    My name is Jameel Isaacs, I would greatly appreciate if you could advise on how to deal with the coding when it comes to using a colour sensor (TCS3200 Color Sensor) in Arduino.

    Kind Regards

    Jameel

    ReplyDelete
  29. Hi. In your article you say to connect the LED in reverse bias. Looks like in the diagram you have connected it to +5V. Is this a mistake or am I misunderstanding something?

    Thanks,

    Mike

    ReplyDelete
    Replies
    1. HI mike, please go through the post once again. I said photodiodes should be connected in reverse bias not LED.

      Delete
  30. sir buzzer always rings....

    ReplyDelete
  31. Really usefull man, i know its something really simple but i was a bit confused in the way this works, but now i have my mind clear, thanks a lot...!!!

    ReplyDelete
  32. How to program photodiode with PIC16F877 using CCS compiler? any example? thanks

    ReplyDelete
  33. the same circuit i've uploaded, but it is not working.
    can u please help me..??

    ReplyDelete
  34. how can i use this ir to my gizduino propller led display ??

    ReplyDelete
  35. how can i use thir ir to my gizduino propeller led display??

    ReplyDelete
  36. I've had a lot of trouble getting my IR emitter and detector to work together, but once I found your example and very clear presentation, it worked great, so thanks!!! One question: we never set pin 2 low in this sketch, so why connect the 3K resistor to pin 2, why not connect it directly to a 5V source?

    ReplyDelete
  37. looked through a lot of pages but yours was the simplest and most easy to understand .thanks .

    ReplyDelete
  38. Can you please post a code for counting rpm using hall effect sensor and arduino.

    ReplyDelete
  39. First thanks your post, we can use this for measure the vibration of wheel bearing???
    (For Automobile Application)

    ReplyDelete
  40. Sir I have my device and in this have microcontroller and eeprom and with infrared phtodiode so my question is can i use any programmer for read write that eeprom and microcontroller via infrared photodiode

    ReplyDelete
  41. sir will you send me the code for access portD for arduino and how to read 4 bit binary data from port D.the data will receive from rf receiver.please send me the code.it will very usefull for my project.
    thanks

    ReplyDelete