Instructables

Simple IR proximity sensor with Arduino

Featured
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.

 
Remove these adsRemove these ads by Signing Up

Step 1: Materials

Picture of 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.
 

Step 2: Preparing the sensor

Picture of Preparing the sensor
DSCN2312.JPG
DSCN2318.JPG
DSCN2324.JPG
DSCN2330.JPG
Before mount it, we should prepare the IR LED receiver to don't receive light from the sides, so than the sensor is more directional.
I use electrical tape to make it, but you can use a straw or anything that blocks the light from the sides.
Cut a small piece of electrical tape and wrap it around the IR LED receiver, forming a tube. Trim the edge with a pair of scissors until it's about 1cm long.
Have a look at images to see how I made it with electrical tape.
neetahar16 days ago

am trying this code...but in my project i connected buzzer ...and the problem is no voltage at pin 11...bcoz of that buzzer is not genrated sound..wt can i do???Please help me.

mintesinot2 months ago

we are trying to use a photo diode with for our project which is fire fighting robot with two continuous servo and one castor wheel but it does not work can you help us

ricardouvina (author)  mintesinot1 month ago

Sure!

What is the problem? Have you tried using the Serial Monitor for debugging?

If you are using it outside, the sunlight may cause it to not work properly. The fire may cause it too.

MikeJGarcia4 months ago
Great sensor Mr Ricardo! I really enjoyed making it and even used it to create a gesture controlled LED cube. Check it out......but forgive the bad lighting in the vid.

http://www.youtube.com/watch?v=LADpkeqRgic
ricardouvina (author)  MikeJGarcia2 months ago

Thank you! Excellent project you have made!

MauS0911 months ago
Can ANYONE help me please. I have the project ALMOST running. The problem for me is that when i give the microcontroller power and burn the program to it it does absolutely nothing. But when i touch the cable that goes from A0 to the board it comes on. Why is that? Please help me.
ZacWolf MauS093 months ago
I ran into the same issue for awhile, and it turns out I had the anode/cathode wires for the receiver mixed up. I flipped it around and it worked prefectly.
ricardouvina (author)  MauS0911 months ago
Probably you are using the resistor after the IR LED receiver, I recomend you to look up carefully the schematic and follows as it is, and check if all the wires are well fixed.
jpineda127 months ago
Specifically, what type of photodiode is needed here?
Commonly, we buy the emitter and receiver in pairs(at least I do), and I see mostly the pair TIL-32 and TIL-78. I can't assure you that these are the names since I don't remember, but with a lil' google search you can differ the emitter and receiver and their names.
jpineda127 months ago
What does 150-distance mean? Help!
The value 150 he imposed as a "threshold", where the "beeps" 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.
jrešetar3 months ago
Hello.
I did it as you said and I have one problem.
It works great but only when light is on. When I turn light off LEDs turn on with no obstacle in front of it.
Why is that so?
Please help me.
marios85433 months ago
Can you work this out with one ir led
VerDe14464 months ago
@arocafor,
yeah that would work..

but i would like to suggest a good sensor for your motor...
you can use HC-SR04...cause it measures distance

well google is our friend...you may get your answer there.. :)
arocafor4 months ago
Hello sir..

Noob here. How can i reduce the range of the sensor? I want to use it as a hidden switch for a motor. Would that work?tnx
jpineda127 months ago
The buzzer does not make any sound. And the serial monitor does not vary. How to solve this? Help!
13, 9:32 PM.jpg
E1Mike jpineda125 months ago
well, you need infrared leds, those in the picture seems to be normal "blue" leds, the infralight can not be seen be human eye, but in cameras seem to be like a purple light, u can buy infrared in any electronic store or you can use some from TV remotes
benjjo8 months ago
HI, Very thankful for your effort and code.

I have a no0b Q.
I know you've deliberately simplified the code for ppl like me so maybe this Q is redundant but i was wondering about your 1 millisecond delay. Would you get a more accurate result if the delay was shorter? And if so, is there a way to do that with PortManipulation? i.e. http://www.arduino.cc/en/Reference/PortManipulation
tyty938 months ago
This project imposible to detect pulse rate if i modify the program?? Actually, may i know what the main function IR LED receiver and IR LED emitter??

Thanks :)
I am trying to find IR led receiver. But I can;t locate it. What is part no and where I can get it? Whenever I google I get ir receivers that are 3 pin and tuned to 38 khz. Please help.
infanati9 months ago
Hi, me again hehe. I tried replacing the buzzer with an LED and setting it on pin 13 but for some reason it isn't turning responding. this is the code im using.


int IRpin = A0; // IR photodiode on analog pin A0
int IRemitter = 2; // IR
infanati9 months ago
Hi, me again hehe. I tried replacing the buzzer with an LED and setting it on pin 13 but for some reason it isn't turning responding. this is the code im using.


int IRpin = A0; // IR photodiode on analog pin A0
int IRemitter = 2; // IR emitter LED on digital pin 2
int ambientIR; // variable to store the IR coming from the ambient
int obstacleIR; // variable to store the IR coming from the object
int value[10]; // variable to store the IR values
int distance; // variable that will tell if there is an obstacle or not
int LED = 13;

void setup(){
Serial.begin(9600); // initializing Serial monitor
pinMode(IRemitter,OUTPUT); // IR emitter LED on digital pin 2
digitalWrite(IRemitter,LOW);// setup IR LED as off
pinMode(11,OUTPUT); // buzzer in digital pin 11
pinMode(13, OUTPUT);
}

void loop(){
distance = readIR(5); // calling the function that will read the distance and passing the "accuracy" to it
Serial.println(distance); // writing the read value on Serial monitor
// buzzer(); // uncomment to activate the buzzer function
}

int readIR(int times){
for(int x=0;x digitalWrite(IRemitter,LOW); // turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR = analogRead(IRpin); // storing IR coming from the ambient
digitalWrite(IRemitter,HIGH); // turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle
value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average
}

for(int x=0;x distance+=value[x];
}
return(distance/times); // return the final value

if(IRpin<10)
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
}


us2410989 months ago
hiii again !! i tried it but for some reason its not working ..... i mean wen i see through camera i see led lighted but it dont work as proximity sensor(buzzer doesnt make noise )
ricardouvina (author)  us2410989 months ago
Take a look at the serial monitor and check if the value varies.
thanks it was that problem only
Guizbo9 months ago
Hello,

I have a practical question. You mentioned the maximum range to be about 25 cm.
In my project I want to measure the distance up to a mirror.

From what I understand, if the mirror reflects more light, maybe the range is higher. Is my assumption correct? and if so, what could be the maximum range in that situation?
ricardouvina (author)  Guizbo9 months ago
Hi,
I said that the maximum range was about 25 cm, because it was what I measured, I can't tell if the range is gonna get higher or not. You should test it before you implement it.
Thanks for asking.
infanati9 months ago
Hi! Complete noob here. I've been working on this project but for some reason I cant get the servos to respond to the proximity sensor. I'm guessing there is something wrong with my wiring. Can anyone help me out pls?
Problem.....png
ricardouvina (author)  infanati9 months ago
I think this is what you are looking for...
Untitled Sketch 2_Esquemático.jpeg
thanks, but my servos still keep spinning in only one direction even with that setup. I have no idea why.
This is the codes i'm using for the IR sensors and the servos.

#include // Include servo library

Servo servoLeft; // Declare left and right servos
Servo servoRight;

void setup() // Built-in initialization block
{
pinMode(10, INPUT); pinMode(9, OUTPUT); // Left IR LED & Receiver
pinMode(3, INPUT); pinMode(2, OUTPUT); // Right IR LED & Receiver

servoLeft.attach(13); // Attach left signal to pin 13
servoRight.attach(12); // Attach right signal to pin 12
}

void loop() // Main loop auto-repeats
{

int irLeft = irDetect(9, 10, 38000); // Check for object on left
int irRight = irDetect(2, 3, 38000); // Check for object on right

if((irLeft == 0) && (irRight == 0)) // If both sides detect
{
maneuver(-200, -200, 20); // Backward 20 milliseconds
}
else if(irLeft == 0) // If only left side detects
{
maneuver(200, -200, 20); // Right for 20 ms
}
else if(irRight == 0) // If only right side detects
{
maneuver(-200, 200, 20); // Left for 20 ms
}
else // Otherwise, no IR detects
{
maneuver(200, 200, 20); // Forward 20 ms
}
}

int irDetect(int irLedPin, int irReceiverPin, long frequency)
{
tone(irLedPin, frequency, 8); // IRLED 38 kHz for at least 1 ms
delay(1); // Wait 1 ms
int ir = digitalRead(irReceiverPin); // IR receiver -> ir variable
delay(1); // Down time before recheck
return ir; // Return 1 no detect, 0 detect
}

void maneuver(int speedLeft, int speedRight, int msTime)
{
// speedLeft, speedRight ranges: Backward Linear Stop Linear Forward
// -200 -100......0......100 200
servoLeft.writeMicroseconds(1500 + speedLeft); // Set left servo speed
servoRight.writeMicroseconds(1500 - speedRight); // Set right servo speed
if(msTime==-1) // if msTime = -1
{
servoLeft.detach(); // Stop servo signals
servoRight.detach();
}
delay(msTime); // Delay for msTime
}
us24109810 months ago
Hiii!! I cant find photodiodes can I use 3pin receivers??? If yes what are the changes I have to do ? Thanks
ricardouvina (author)  us24109810 months ago
Hi! You probably can, but I don't know how to adapt it to work, and I prefer photodiodes because they are way cheaper.
tobidlb9310 months ago
Does it only work with arduino UNO?
ricardouvina (author)  tobidlb9310 months ago
No, it should work with any arduino.
jrešetar11 months ago
Hi,
please somebody help me.
I have done everything as shown but it does not work. I did not connect buzzer (I do not need it). I opened Serial monitor and got something like this: "?&7!" and then nothing.
I the type of LEDs matters (I do not what type I have- ordered from ebay).
Any advise?
Thank you.
ricardouvina (author)  jrešetar11 months ago
Be sure that your serial monitor is set to 9600.
sir

will u teach me how to count the rpm using IR sensors.
brians606711 months ago
hello i was just wondering if it is possible to have instead of a buzzer, a group of LEDs ?
ricardouvina (author)  brians606711 months ago
I think it is possible, although not necessary, as you can deactivate that function.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!