DIY Infrared Proximity Sensor (Arduino Compatible)





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.


#include <IRremote.h>

#define PIN_IR 3
#define PIN_DETECT 2
#define PIN_STATUS 13

IRsend irsend;
void setup()

void loop() {
  digitalWrite(PIN_STATUS, !digitalRead(PIN_DETECT));


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.




    • Science of Cooking

      Science of Cooking
    • Trash to Treasure

      Trash to Treasure
    • Paper Contest 2018

      Paper Contest 2018

    We have a be nice policy.
    Please be positive and constructive.




    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?

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

    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?

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

    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


    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?

    same, the led on pin 11 just turns on.

    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??

    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.