Hello guys! In this instructable I'll teach you how to make a very simple proximity sensor using infrared LEDs and Arduino.
After several times trying to optimize it, I finally came up with something that is quite simple e precise. Just like my first instructable, this project is perfect for beginners in the arduino's world, with a few components anyone can make it. I hope you all enjoy it.

Step 1: Materials

For this instructable you are gonna need:
- Arduino
- 1 IR LED receiver (photodiode with 2 pins, not the phototransistor with 3 pins)
- IR LED emitters (as much as you can get, but at least 2)
- 100K resistor (brown black yellow)
- Jumper wires
- Breadboard
- Electrical tape
- Buzzer(optional)

Be careful to don't mix up the LED receiver with the LED emitter, they all look the same.
great work buddy!
<p>This Is the code with the buzzer working!!</p><p>int IRpin = A0; // IR photodiode on analog pin A0<br>int IRemitter = 2; // IR emitter LED on digital pin 2<br>int ambientIR; // variable to store the IR coming from the ambient<br>int obstacleIR; // variable to store the IR coming from the object<br>int value[10]; // variable to store the IR values<br>int distance; // variable that will tell if there is an obstacle or not<br><br>void setup(){<br> Serial.begin(9600); // initializing Serial monitor<br> pinMode(IRemitter,OUTPUT); // IR emitter LED on digital pin 2<br> digitalWrite(IRemitter,LOW);// setup IR LED as off<br> pinMode(11,OUTPUT); // buzzer in digital pin 11<br>}<br><br>void loop(){<br> distance = readIR(5); // calling the function that will read the distance and passing the &quot;accuracy&quot; to it<br> Serial.println(distance); // writing the read value on Serial monitor<br> buzzer(); // uncomment to activate the buzzer function<br>}<br><br>int readIR(int times){<br> for(int x=0;x&lt;times;x++){<br> digitalWrite(IRemitter,LOW); //turning the IR LEDs off to read the IR coming from the ambient<br> delay(1); // minimum delay necessary to read values<br> ambientIR = analogRead(IRpin); // storing IR coming from the ambient<br> digitalWrite(IRemitter,HIGH); //turning the IR LEDs on to read the IR coming from the obstacle<br> delay(1); // minimum delay necessary to read values<br> obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle<br> value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average<br> }<br> <br> for(int x=0;x&lt;times;x++){ // calculating the average based on the &quot;accuracy&quot;<br> distance+=value[x];<br> }<br> return(distance/times); // return the final value<br>}<br><br><br>//-- Function to sound a buzzer for audible measurements --//<br>void buzzer(){<br> if (distance&gt;1){<br> if(distance&gt;100){ // continuous sound if the obstacle is too close<br> digitalWrite(11,HIGH);<br> }<br> else{ // bips faster when an obstacle approaches<br> digitalWrite(11,HIGH);<br> delay(150-distance); // adjust this value for your convenience<br> digitalWrite(11,LOW);<br> delay(150-distance); // adjust this value for your convenience<br> }<br> }<br> else{ // off if there is no obstacle<br> digitalWrite(11,LOW);<br> }<br>}</p>
Which arduino board will interface both IR sensors and image input...... And give one output at a time..... My project is smart traffic light control using IR sensors and image processing technique
<p>I think arduino mega or due will do the job! :)</p>
<p>sir. can u help me with my project. im working on project that using this sensor. my project smart street light that can detect car movements then the light will be on. can u help me please.</p>
<p>I did not understand your project much but if you replace the buzzer with an led it will work as well! Good luck and let me know if you need anything else!</p>
Hello, can any one tell which arduino board will interface both IR sensors and image input..... And give one output
<p>Hi all, how can i connect LED instead of buzzer ??</p><p>what is the code for that??</p>
<p>hi. around how many mA does the circuit need?</p>
<p>not counting the buzzer if possible</p>
<p>when i try to use your code .. I notice the pinMode(1,OUTPUT); was active and i don't know how to turn it off. <br>I made a project that's related to this code.. i hope you can help me.</p>
<p>Port 1 is the serial com port. this port is active whenever you use the serial.begin(serial monitor) if you remove the serial begin statement from the setup this port won't be active but you will also not be able to use serial monitor.</p>
<p>Can it detect black color?</p><p>If it detects black color, then how do I differentiate between white and black color?</p><p>Also, what are the changes to be done in the code if I am using only one transmitter?</p><p>I will be very excited if you reply as soon as possible! </p>
<p>Also, what changes should I do in the code for me to detect only black color objects which will be placed at a small distance (~2cm-3cm) from the led pair.</p><p>Again I will be excited if you reply as soon as possible!</p>
<p>hi i want to track any valuble devices like keys or etc . i want to add that sensor that device like key or etc . so that i can check how much far from me . In this scenario will it be helpful for me . i have seen that it detects height of a object from the top . but i need sensor which can tell how much distance far from me. can u tell me which device will be help ful for my project.</p>
<p>You should try an ultra-sound boad such as the HC-SR04</p>
<p>You should try an ultra-sound boad such as the HC-SR04</p>
<p>Hey! i'm doing this for my graduation project (including other work) .</p><p>i want a signal only if a specific distance is mesured (distance detector) </p><p>for example if a small car is standing below it will be 2 meters far from it .. if a big car is standing below and the detector gets less then 1.2 meters it will give a signal so the car can't get in </p><p>pleeaaaase i need help replay as soon as u can</p>
<p>Hi I would like to ask if you used any pwm functions for the LEDs as usually that is required when working with LEDs</p>
Visitors to this instructable should understand that the IR LED setup in this schematic is wrong on many levels. The schematic should be corrected and updated. <br> <br>There is no type of diode that can be safely and reliably connected in parallel this way. LEDs are not resistive light bulbs. The forward voltage Vf from device to device is not guaranteed to be the same for a given current. You are effectively limiting the current to the diodes (and not evenly) by maxing out the driver in the microcontroller. <br> <br>In the simplest and least desirable solution you need one limiting resistor per diode, where the resistor size is (5 -Vf)/If where If is the desired forward current of the diode. So a typical steady 20mA and Vf of 1.3V is a resistor value of 185 ohms. <br> <br>In practice you should reduce resistive losses by stacking 2 or three diodes such that (5 - 3*Vf)/If = 55 ohms and now you get three times the light for the same current output. <br> <br>Further, considering the limitations of your power supply, the current can be pulsed much higher, and the values read back during the on cycle, so long as the average power dissipation stays below rated. In this case you will have to offload the switching with an external transistor.
<p>Hi Schuitz. </p><p>Thanks for this. I was also wondering the same. I assumed 1 limiting resistor per emitting diode. But this could be done in parallel from the breadboard power bus (using the Arduino as a power source). Is that correct?</p><p>Do you also need a limiting resistor for the IR sensor (phototransistor)?</p><p>Ta</p><p>Nick</p>
<p>Would it make sense to switch off the LEDs in your code after checking the distance. The current code only switches them off momentarily. Considering that you're not using any current limiting resistors - the LEDs could get damaged.</p>
<p>Hi Dushyantahuja, </p><p>Did you eventually use current limiting resistors for your emitting diodes?</p><p>Ta</p><p>Nick </p>
<p>Works great! Thanks for the tutorial</p>
<p>Hi Ricardouvina, For the receiver, is there a difference if i used the dome shaped one with 3 legs rather than your 2 leg ones? also I tried using ur program but only when i changed the <strong>delay=100 </strong>for <strong>emitter=low</strong>, would the result be stable. Otherwise the result would be just a jumpy mess. But it wouldnt be logical since it would most of the time be reading the environmental infrared radiation? Sorry for the messy explanation. if u need more details to the problem please inbox me. Thanks in advance!</p>
<p>Also, im not using a buzzer. Im trying to extract out the distance for my project use.</p>
<p>i wanted to make this for my bungalow gate but then i would have to put the receivers on the other side right or the transmitter on the other side.</p><p>also what if i had to make this thing for my bedroom door without the bread board.any link or anything like .</p><p>Thanks Alot</p>
<p>I made it!!</p>
Update:The buzzer work but is always on...it isnt giving beeps as in the video...??
I made the project...But the buzzer isnt working..plz help
<p>Very low range At least for me and the photo diode is reverse biased</p>
I think there is some error in schematic ...... I think that the reverse bias should be added to photodiode ( IR LED Receiver) and not forward bias. I connected up with forward bias and could not get any reading. I have even triple checked my arduino's analog input pins. Mostly at many other IR sensor tutorials and schematic it is reversed biased. I think Arduino is not sensitive to voltage changes in forward bias. Please double check your schematic and your project.
<p>i her you but i dont understand where you see a issue? can you post a quick pencil sketch pic of the proposal diagram of the renewed schematic? thanks </p>
<p>Thanks for the very good example and the error logs in this posts. Keep up the good work. I will try to move brushless motors through ESCs by using IR proximity sensors (probably 2). I will post the example when I'm done.</p><p>Cheers</p>
<p>did you ever got this esc and motors done?&gt; </p>
<p>Yeah, actually the vast majority of the IR sensors are supposed to be reverse biased, and forward biasing them would create almost the same effect of a normal IR emitter, and sometimes it may even burn the diode.</p>
I have been faced to the same problem. My ir receiver didn't worked in forward (I have tried with different bipolar IR receiver) but it nicely works on reverse bias. Like you said the arduino wasn't sensible in forward. Your comment help me much. Thank you @mgupta15
<p>why 4 ir's emitters ? why not just 1 if is only going to be in a upwards direction? or any direction really this works by reflecting of waves coming back from a object in proximity and waves expands like water ripples so why 4? </p>
<p>Hello, nice work for putting this together, I'm wondering if there's a possibility to replace the buzzer for a servo, I'm trying to make an IR proximity sensor that moves the servo depending on the distance but I can't figure it out yet, any help will be very much appreciated!</p>
<p>The output of the IR sensor is Vcd at certain level which in this case Ricardo is sending it to OutPin11 &quot;pinMode(11,OUTPUT); // buzzer in digital pin 11&quot;</p><p>When the voltage is too low you won't be able to move your servo but with this same output you could move it to a certain extent. Im gonna try this approach for a prototype but I will connect the output of the micro controller to an ESC and a brushless motor. Before I do that I will try it on a servo because I will be using a similar code in the ESC. </p><p><strong>SOLUTION:</strong></p><p>Have you tried just plugging the servo control signal to pin 11 and making the changes in the code using the example on arduino's webpage and replacing the pot for the IR output? <em>See images attached and weblinks...</em></p><em><strong>This webpage shows what you are attempting exactly to do but with IR integrated sensors. SHARP </strong>brand (see image 3)</em><a href="http://electronics.stackexchange.com/questions/59467/sharp-ir-distance-sensor-outputting-consistently-high-voltage" rel="nofollow"><strong>http://electronics.stackexchange.com/questions/594...</strong></a><p>Arduino's forum for servo control:</p><p><a href="http://playground.arduino.cc/Learning/SingleServoExample" rel="nofollow">http://playground.arduino.cc/Learning/SingleServoE...</a></p><p><strong>Important: You will have to make some changes to the code posted in Ricardo's example and combining it with what the servo needs. </strong></p><p>Let me know how it goes? I will have my code in about a week. I am using a SHARP IR sensor instead due to the application but it is the same principle.</p><p>Cheers, I hope everything works. Otherwise do not hesitate to contact me.</p><p><em><strong>IMAGES ARE NOT MINE. CREDIT TO THE REFERENCES.</strong></em></p>
<p>there are 2 green wires in which pin did u insert it </p>
What does 150-distance mean? Help!
The value 150 he imposed as a &quot;threshold&quot;, where the &quot;beeps&quot; from the buzzer will repeat at decreasing rates, since the distance(variable) will get higher(because the object is getting close) providing the distance variable value to be inversely proportional to the real, physical distance measure.
<p>on line 55 and 57, I am wondering what happened more than 151 negative number of distance? if 150 - 151(151 is convert distance number) will be negative 1. because it said delay(150 - distance); , so will it work if delay(-1) function? </p><p>here example code...</p><p>```</p><p>void buzzer(){</p><p> if (distance&gt;1){</p><p> if(distance&gt;100){ // continuous sound if the obstacle is too close</p><p> digitalWrite(11,HIGH);</p><p> }</p><p> else{ // beeps faster when an obstacle approaches</p><p> digitalWrite(11,HIGH);</p><p> delay(150-distance); // adjust this value for your convenience</p><p> digitalWrite(11,LOW);</p><p> delay(150-distance); // adjust this value for your convenience</p><p> }</p><p> }</p><p> else{ // off if there is no obstacle</p><p> digitalWrite(11,LOW);</p><p> }</p><p>}</p><p>```</p>
<p>any ideas on how to get this to work in the raspberry</p>
<p>Hello, i followed every step of your tutorial, but i get values raging from -5 to -6. I have made sure that every component is okay and working.Do you have any idea why i get this values and how to fix it.</p><p>P.S. I did disconnect the IR leds and the IR receiver(the photodiode) and then i am getting 0 as a value.Which means that the leds and the IR receiver work...?</p>
<p>Where do you recommend buying the IR LED receiver and multiple IR LED emitters? All the electronic parts shops I've talked to only sell them in pairs so I can't get more than one emitter at a time.</p>
<p>I've been doing this with on IR Transmitter but it isn't working</p>

About This Instructable




More by ricardouvina:Simple IR proximity sensor with Arduino Mood Lamp with Arduino 
Add instructable to: