Picture of DIY Infrared Proximity Sensor (Arduino Compatible)
Ever looked on Sparkfun or Adafruit for some cool robot parts, and saw those expensive IR and Ultrasonic proximity sensors? I have, and I really wanted one, but they're so expensive ($30-$40 or more) so I decided that I could make one myself with the parts I had on hand for way less.

What I came up with was an infrared distance/proximity sensor that sensed an object up to about 10-12 inches away all for about $3.

It is fully compatible with Arduino and other microcontrollers such as PIC, PICAXE, and Parallax Propeller (although the code would have to be different than the Arduino code I will be showing you).

Step 1: How does it work?

The way this sensor works is like this.

1. An IR (infrared) LED emits a pulse of light at the rate of 38KHz. The pulsing rate of the LED is controlled by an Arduino's PWM output.

2. An IR Receiver module is connected to a digital pin on the same Arduino. This module can detect a IR light signal that is pulsing at 38KHz, which is the speed that led Infrared LED next to it is flashing.

3. If there is an object in front of the IR LED and the receiver, the object reflects some of the light back onto the receiver. The receiver detects this light and sends information back to the Arduino.

4. If the receiver detects an object, it sends LOW to the Arduino's digital input. If it does not detect anything, it sends HIGH.

5. The Arduino then processes this and uses it to control a device of your choosing. It can be anything from an LED to a servo motor to a solenoid.

That's it!
jamalnasim1 month ago

code is not verifying..! IRsend irsend does not name a type error .. plz help..!

PeterJ73 months ago

Ive pulled some IR bits from some old desktop mice from work. Id imagine there are plenty of different makes/models of LED and receiver. How can I check what spec these LEDs and Receivers are? Is there any need for a resistor on the LED just in case it goes pop?

JakeR56 months ago

How would I make this code work with a PIC chip?

polkijain1 year ago
kindly help me out to make a linefollower bot for my school science fair using this code...I mean the ir detection part.... I have 3 ir LEDs and 3 phototransistors for input.... I m good with the motor control code but I need the proper sensor schematics and code.... other instructables use a photodiode and it is unavailable at my place... I have the phototransistors thanks in advance!

You shoud use QRD14 (QRD13) sensor, this is a specific line-seeker sensor. It work with logic levels

JQU10 months ago

For some reason it doesn't work for me. The LED just randomly flickers whether I hold my hand in front of it or not. It also lights up a couple of seconds when I disconnect and reconnect de IR emitter.

Do you know what might cause this?

jobo991 year ago

same, the led on pin 11 just turns on.

devangs31 year ago

Your sketch just blinks the LED, how do I use it to control the motors, because IR signal received is a pulse, and it should be a constant signal(HIGH or LOW) for the arduino to enter an ISR or make a decision. Any suggestions please??

ihendel2 years ago
Hi there!

I'm trying to get the LED operate with the PWM / 38KHz signal using the IR library IRsend as in your program but I do not see the IR LED bilinking with the camera (like a remote control LED) - I see it as constant light (purple)
What could be wrong here? The sensor does not sense this light.


old topic, but 38khz means the led is flashing 38 thousand times every second. neither the camera or your eyes can see that fast so it appears to be solid on. the only way to tell that is is flashing is with the ir receiver or an oscilliscope.

ard0071 year ago
Can you connect ir receiver pin to the analog (~A0-A5) pin on the arduino? I am having an issue to get my 4WD working. Only a pair of the motors are working. How ever when i load a different sketch (very similar but without any ir control) all motors work!
aliviu2 years ago
it's not working for me :( i am realy new to electronics and programing, its just a hobby but i cannot get your eagle sketch to work. i use a camera to check if the ir led emits but no "ir comes out of it" (i tested the ir led with blink and works ok) if i use a remote control and pres butons i can see the let 13 blink. do you have any idea what i am doing wrong? thanks
jlar682 years ago
what´s the name of the program you used to draw the schematic???
xBacon (author)  jlar682 years ago
CadSoft Eagle: http://www.cadsoft.de/download-eagle/?language=de
It's in German so you might have to translate it if you want to read it, but the actual program is in english
cmurcia2 years ago
It doesn't work to me, I don't know why.
cmurcia cmurcia2 years ago
I found the error.... at the program there are "¡" remove it and it's work perfect
WWC3 years ago

Did you incorporate this IR code into your robot program?
I mean when there is detection does you robot do what, stop then turn that kind of thing?
xBacon (author)  WWC3 years ago
I just have it super simple, all it does is when the sensor reads low (there is an obstacle) it backs up until there is no longer an obstacle it it's line of sight. Then it resumes normal operation (seeks the brightest light source)
WWC xBacon3 years ago
BTH i like your BIG blue tires.
xBacon (author)  WWC3 years ago
Haha thank you. I got them from a broken RC car that i found in my closet.
waterlubber3 years ago
can the ATTINY run this? I mean, without the lib?
xBacon (author)  waterlubber3 years ago
Yes, the ATtiny should be able to run this just fine, but you will need to find a way to pulse the LED at 38KHz. You might even be able to port the library over to the ATtiny somehow.
Great! Thanks...I guess delay.microseconds() should be able to do it:
void ledOn() {
while(i = 0){
digitalWrite(1, HIGH):
delay.microseconds(bleh insert value :):):):))
digitalWrite(1, LOW):
same delay here;
too lazy to follow syntax.
xBacon (author)  waterlubber3 years ago
Good idea.
xBacon (author)  waterlubber3 years ago
Yeah, that would probably work if you found the right delay to have it be exactly 38KHz, which will probably require a LOT of testing to see what would work.