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:
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
ray strength, the closer is the obstacle and vice-verse
Photodiode:
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 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 :
very useful post bro
ReplyDeleteThanks for your feedback ANKITH
DeleteHi ,
ReplyDeleteThanks 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 !
I had updated the code please check it now, It will work fine
Deletethanks for this useful post..
ReplyDeletecan you suggest how to apply it on something related to medical?
IR LED and Photodiode pair cannot give you accurate distance but They can help you to find any obstacle in front of it.
DeleteSo, 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.
I like the way of representing circuit diagrams, thanks
ReplyDeleteThanks
DeleteOlá Kranthi kumar,
ReplyDeletePosso 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
No, IR LED's are not suitable to detect rain.
DeleteUse an "humidity detection sensor module" to detect rain which will cost you around $2.60.
Deleteall the best for your project.
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.
ReplyDeleteIt works now.. thank you for the tutorial
Deletehi arvin. i am getting the same thing. how did you correct it??
Deletewhy my ir sensor value is constant it doest change .plese help.
ReplyDeleteIR LED showing constant value means there may be some loose connections in your circuit check them and try again
Deleteif i use a pot in series with 3k will my sensitivity increse.
ReplyDeleteIf you want more resolution/sensitivity use an OP-Amp to boost the output signal
DeleteThank you,this article was very useful,not only did u present the diagram very well but u clearly explained how it works
ReplyDeleteThanks for your reply
Deleteexcellent post, I tried and working as expected
ReplyDeletethanks for ur reply
DeleteBuzzers Is Always On When i Connect The Photodiode..:( Please Help
ReplyDeleteIts working fine Dylan Netto. please check your connections again.
DeleteYeah It Worked...My Diode Was Bad..:-P Thanks..
ReplyDeletehii, can i ask this circuit suitable for smoke detector used? thank you~
ReplyDeleteNo, Photodiode cannot detect smoke.
DeleteCan you please suggest me on the photodiode to be used?
ReplyDeleteShould it be a normal photodiode or ir sensitive photodiode?
Can you please tell me the specifications of the photodiode you used?
photodiodes are normally IR sensitive. Just ask shopkeeper to give 5mm Photodiodes
Deletei'm using LED instead of buzzer what must b the connection and the code change please anyone help me this is my semester project
ReplyDeleteno 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.
Deletei'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
ReplyDeleteI did'nt understand your question
DeleteThis comment has been removed by the author.
ReplyDeleteuseful tutorial, but i want to make connections on proteus and i don't know which devices to choose(code of the devices like photodiode)
ReplyDeleteSorry abeer jaradat, I dont know much about proteus. Just google it.
DeleteFirst 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
ReplyDeleteFor 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.
DeleteWhere do we write the code(which software) and how do we flash it?
ReplyDeleteYou have to write the code in Arduino IDE you can find more information about arduino on following site -> www.arduino.cc
Deletevery useful post :D thanks!!!!!
ReplyDeleteThanks for your feedback @GabrieleLiu
DeleteHi 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.
Deletemy email address is: anjelotic1@gmail.com
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?
ReplyDeletesure
DeleteHi 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.
ReplyDeletemy email address is: anjelotic1@gmail.com
nice work man
ReplyDeleteThank you
DeleteThis 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!
ReplyDeleteThank you very much. i have a problem of this. how can i get accurate values from this?
ReplyDeleteIf you want to get more accurate values with more resolution - then use photo transistor instead of photodiode
Deletegood and helpful tutorial on IR. thank you.
ReplyDeletea.k.a Plustwo
good and helpful tut on IR pair, thank you. a.k.a Plustwo
ReplyDeleteHi there
ReplyDeleteMy 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
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?
ReplyDeleteThanks,
Mike
HI mike, please go through the post once again. I said photodiodes should be connected in reverse bias not LED.
Deletesir buzzer always rings....
ReplyDeleteReally 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...!!!
ReplyDeleteHow to program photodiode with PIC16F877 using CCS compiler? any example? thanks
ReplyDeletethe same circuit i've uploaded, but it is not working.
ReplyDeletecan u please help me..??
how can i use this ir to my gizduino propller led display ??
ReplyDeletehow can i use thir ir to my gizduino propeller led display??
ReplyDeleteI'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?
ReplyDeletelooked through a lot of pages but yours was the simplest and most easy to understand .thanks .
ReplyDeleteCan you please post a code for counting rpm using hall effect sensor and arduino.
ReplyDeleteFirst thanks your post, we can use this for measure the vibration of wheel bearing???
ReplyDelete(For Automobile Application)
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
ReplyDeletesir 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.
ReplyDeletethanks