Introduction: 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!
Step 2: What You Will Need
The parts you will need for this are:
1x 5mm IR (infrared) LED (can be narrow or wide angle)
1x IR receiver
1x Regular straw, any color (not those super fat ones you get from some fast food restaurants)
1x 3-4 inch piece of electrical tape
1x Arduino or other microcontroller (to control the LED and pick up signals)
Step 3: Assemble the Sensor
It takes about 2 minutes to so this part. Just cut off about one inch of straw, and wrap it in electrical tape, but don't cover the ends. Then stick the LED into one end of the straw, it should fit snugly. You can hot glue the end of you want.
That's all for assembly!
Step 4: Program the Arduino
First of all, you need to download the Infrared Library for the Arudino IDE. I attached the folder to this step, so you can just scroll to the bottom and download it. It will be in a .ZIP file, so you will need to extract it first. To install the library in Arduino 1.0.1, refer to this helpful page.
After you have installed the library, upload this sketch to the Arduino.
//CODE STARTS HERE
#include <IRremote.h>
#define PIN_IR 3
#define PIN_DETECT 2
#define PIN_STATUS 13
IRsend irsend;
void setup()
{
pinMode(PIN_DETECT, INPUT);
pinMode(PIN_STATUS, OUTPUT);
irsend.enableIROut(38);
irsend.mark(0);
}
void loop() {
digitalWrite(PIN_STATUS, !digitalRead(PIN_DETECT));
}
//CODE ENDS HERE
Attachments
Step 5: Wire It Up!
Follow this schematic diagram to connect the LED and receiver to the Arduino.
Note: Your IR receiver may have a different pinout than this, so consult the datasheet for your receiver and check the pinouts. If you are not sure, then connect it the way shown in the schematic and see if it works. Most receivers have this pinout.
Note #2: You must have the LED and the receiver next to each other, but not facing towards each other. You must have them pointing in the same direction as shown in the second and third pictures.
Step 6: Try It Out!
Just connect the Arduino to a power source (USB cable or a 9 volt battery) and put your hand in front of the receiver/LED. The LED on pin 13 of the Arduino (there is one built in, it is located on the Arduino board near pin 13, labeled "L") should light up. When you move your hand away, the LED should turn off.
Awesome!

Participated in the
Hurricane Lasers Contest

Participated in the
Hack It! Contest
26 Comments
8 years ago
I'm having some problems that some people seem to be having as well. The LED on my arduino seems to be blinking randomly. I'm using salvaged parts form other circuits, and when checking with a camera (That I know does see IR light), I never seem to get any light from the LEDs. I have 3 different sensors (All salvaged) that I've tried, and 2 different kinds of IR LEDs (Again, all salvaged) that I've tried. I do know that 3 of the LEDs worked before I took them, from the circuit they were in, and that the IR receiver that was with them worked too. However, the circuits I took all the LEDs from take 9 volts originally, so could it be that the LEDs need 9v? I've tried switching the polarity on them. The only other thing I can think of is that maybe I've gotten either the pinouts or voltage wrong for the IR sensors. Can anyone offer any advice?
8 years ago on Introduction
code is not verifying..! IRsend irsend does not name a type error .. plz help..!
8 years ago on Introduction
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?
8 years ago on Introduction
How would I make this code work with a PIC chip?
10 years 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!
Reply 8 years ago
You shoud use QRD14 (QRD13) sensor, this is a specific line-seeker sensor. It work with logic levels
9 years ago on Introduction
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?
9 years ago on Step 6
same, the led on pin 11 just turns on.
9 years ago on Introduction
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??
10 years ago on Introduction
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.
Thanks,
Itzik
Reply 9 years ago on Introduction
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.
9 years ago on Introduction
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!
10 years ago on Introduction
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
11 years ago on Step 5
what´s the name of the program you used to draw the schematic???
Reply 11 years ago on Step 5
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
11 years ago on Step 6
It doesn't work to me, I don't know why.
Reply 11 years ago on Introduction
I found the error.... at the program there are "¡" remove it and it's work perfect
11 years ago on Introduction
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?
Reply 11 years ago on Introduction
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)
Reply 11 years ago on Introduction
BTH i like your BIG blue tires.