Author Options:

Proximity Sensing with the Arduino? Answered

I am looking for a way to use the arduino, an IR emitter and an IR detector to use for proximity sensing. I have looked at a lot of tutorials but none of them seem to deal with detectors that only have two pins, most are for the sharp sensor. I am looking for a cheap alternative and would like to stay away from the Sharp sensor. I want to be able to pulse on and off the IR emitter and be able to read the reflective beam with the IR detector. I was able to do this with a reversed led connected to the IR detector, not sure why that worked but it did. I am trying to find the proper way to wire and read the IR level.

When the IR emitter pulses, I want the detector to take a reading and then I am going to use that reading to determine how bright or dull a connected LED will be. Any help is greatly appreciated and I think with a little bit of guidance I will be able to do this =) ... wouldnt mind taking pictures and making an instructable out of this either ;)?

Here are the emitters and detectors I am using:




Best Answer 7 years ago

So since an IR led can draw WAY more current than the arduino pins should supply in a healthy arrangement, you want to use the arduino to switch on and off a transistor, which in turn turns on and off the led. (through a suitable resistor to limit the current)

The sensor is hooked up between an analog input pin and +5v to sense what it returns. It should return an analog voltage between 0 and 5 volts based on how much ir hits it (check the schematic) - may be a more vertical 'curve' and seem more like on/off rather than totally analog. Essentially, the detector will let more voltage through with the more IR it sees.

To sense proximity , aim the IR led AWAY from the detector, and when something reflective comes in range, it will reflect back some IR and the sensor will note this on the analog pin as a voltage change.

The reason to pulse the IR led quickly is to eliminate background noise. You sense when the IR is off, then when the IR is on so you can tell if the sensor is being blinded by ambient light. You can subtract anything the sensor sees while the IR is turned off from when the IR is turned on to set the 'baseline' value, and if it suddenly starts changing from that value, you can count a hit. Depending on how variable the sensor outputs change, you can even use it for crude distance measurement.

EG: led off, sense reads 1 volt
led on, sense reads 2 volts. arbitrary value of 1 is assigned to baseline, not 2
*turn on lights in the room, which emit some IR*
led off, sense reads 3 volts
led on, sense reads 4 volts, arbitrary baseline is still 1 - no object detected.
*introduce an object in front of sensor*
led off sense reads 2 volts
led on, sense reads 5 volts -- arbitrary baseline jumps up by 2 suddenly between a few readings, object detected!

I tried running one leg to 5 volt and the other to the analog pin but it just gives me a value of 1023 regardless...However if I connect an LED backwards it will start giving me a readout of ambient light and actually tell me how much light its getting...does the same if I hook the oscilloscope to one leg...but it isn't using the emitter at all. The IR detector simply acts like a standard photocell and doesnt even seem to use the IR emitter...

I have them side by side pointing directly upwards...The emitter is on 50% and off the other 50%...if the emitter stays on it gets super hot. I have my serial set to 9600.

//IR detector attempt

int IR = 12; //IR emitter
int val; //value to store analog reading

void setup(){
pinMode(IR, OUTPUT);
void ir(){
digitalWrite(IR, HIGH);
digitalWrite(IR, LOW);

void loop(){
val = analogRead(5);

Any help would be awesome...proximity sensing seems like it would be really fun to work with on a lot of projects =)

You don't just need to flicker the led, you need to put it on, take a reading, then turn it off, take a reading, otherwise you're flashing it, then taking a reading 70ms after it's been turned off.

void irON(){
void irOFF(){

void loop(){
delay(25); //make sure the sensor has had time to sense
onValue=analogRead(5); //Should be a constant which pin 5 is
delay(25); //probably not necessary but who knows.

AmountDetected = onValue - offValue;


The output of this gives me 0, -1, and 1's ...

How should the detector sensor be wired...the detector sensor has two legs, one leg is connected to 5 volts and the other is connected to analog pin 5.....regardless of how the detector is wired it returns the same results...

i can see the emitter working perfectly when I look through a camera...my problem is the detector doesnt seem to be wired properly or returning any IR readings.

Double check the polarity of the detector. It may help to wire it up as a series switch in a simple led circuit to say 'when ir detected, power on the led'- then you'd know what to expect as an 'output'. It may in fact need to sink to ground rather than source from 5v.

If anything's getting hot, its wired wrongly. You have to fit a current limiting resistor to the emitter !


You'll need to modulate the emitter, and read the detector via a digital filter for decent performance. Using DC for the source is going to give you all sorts of fun with noise.