HELP with Infrared emitter + sensor problems

I have a 940nm infrared LED emitter, an infrared receiver (phototrans clear, whatever that means), and all of the necessary components for a standalone Arduino... 16 mhz resonator, a few resistors, 9v battery, +5 v regulator, and a 3v LED. What I want to do is use the infrared emitter and receiver together to create an interactive LED. Simply, when I wave an object over the infrared receiver I want the LED to light up, and to stay off when nothing is within a certain distance of the receiver ( preferably 20cm or so). So I believe I have it all hooked up correctly. The infrared LED is hooked up to digital pin 13 to emit a constant infrared light. Right next to it on the breadboard I have the infrared receiver which is hooked up to ground and analog pin 2, to receive the infrared signal bouncing off of something. Finally, I have the regular LED attached to digital pin 12, to emit light when the receiver receives infrared. There is a barrier between the infrared LED and receiver. Here is my code.... I think I just need a better way of stating what the input actually is and communicating that with code.

void setup()

{
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
}

void loop()

{

val = analogRead(2);

if(val > 0)
{
digitalWrite(12,HIGH);
}
}

Now, I think my problem is with the if (val > 0)..... I'm not sure if the receiver would input information like that..... PLEASE HELP!!

Well, you haven't declared "val", so we'll assume it is an "int".
If you read an analogue pin, unless it is nailed to ground, it will almost certainly read > 0, so that might need some attention too.

The infrared LED is hooked up to digital pin 13

...via a suitable resistor, I assume?

I have the regular LED attached to digital pin 12

...ditto.

I think you may be a little optimistic expecting a 20cm range.

Sorry, yes I have declared it as

int val = analogRead(2)... just forgot it in the post.

the LED to pin 13 is via a 220 ohm resistor, as well as with the LED in pin 12.

Do you believe my code should work? .. (adding int val = analogRead(2)...)???
Also, How could I test the input using the serial() function??

Thanks.

You could print the value read from the analogue input.

You might get better results if you flash the LED on and off and check the input for a change between those two states. That will help filter out background light.

int onVal = 0;
int offVal = 0;

// Take 10 samples with the light ON and OFF
for (i=0; i<10; i++)
    {
    digitalWrite(IRLED, HIGH);
    onVal += analogRead(IRTRANS);
    digitalWrite(IRLED, LOW);
    offVal += analogRead(IRTRANS);
    }

if ((offVal - onVal) > THRESHOLD)  // Value to be determined experimantally
   {
   //  Light up the LED
   }
else
   {
   // Turn off the LED
   }

What is pulse in and would that help me here?

njs707:
What is pulse in and would that help me here?

PulseIn() measures the length of a pulse on an input pin.

No, that would not help you here.

Your best bet would be to flash the LED at 38kHz, and use a 38kHz IR remote receiver to detect it - as johnwasser has pointed, it will give you better immunity to noise.

OK thanks, new question though. I got it to work fairly well by pulsing the infrared LED, but only while everything is attached to the arduino. When I put everything on a breadboard for a standalone, it does not work the same, even though I have a 9v battery hooked up and a +5 voltage regulator??? Why would it not work the same? The only difference is the power? My only thought is that the arduino uses a 16 mhz crystal with 2 capacitors while the stand alone uses a 16 mhz resonator. Could that be it?

njs707:
OK thanks, new question though. I got it to work fairly well by pulsing the infrared LED, but only while everything is attached to the arduino. When I put everything on a breadboard for a standalone, it does not work the same, even though I have a 9v battery hooked up and a +5 voltage regulator??? Why would it not work the same? The only difference is the power? My only thought is that the arduino uses a 16 mhz crystal with 2 capacitors while the stand alone uses a 16 mhz resonator. Could that be it?

Does the stand-alone unit run a simple sketch like Blink? Is it blinking at the right rate? If so, your standalone system is in pretty good shape.

Could it be that you forgot to connect +5v to the AVCC (Analog VCC) pin? The A/D converter gets a separate voltage input so it can be better isolated from electrical noise. That would keep your analog inputs from working.

Yes that is definitely connected. I got it to work with my hand above the sensor at about 15 cm when connected to the arduino, but not even when I touch the receiver when on the breadboard. The weird thing is, if I bring the breadboard setup into the pitch black bathroom, my LED will turn on and I can interfere a little with my hand above the sensor???? I know the IR led is emitting because I can see it with my phone's camera. Any other suggestions? I am pretty sure everything is set up correctly on the breadboard. The LED does do a simple blink perfectly if I program it to do that. Here is my code btw.... it works beautifully with the arduino... :frowning: but not at all with the standalone.

void setup()

{

pinMode(13,OUTPUT);
pinMode(12,OUTPUT);

}

void loop()

{
int receiverVal = analogRead(A0);
digitalWrite(12,HIGH);
delayMicroseconds(5);
digitalWrite(12,LOW);
delayMicroseconds(5);

if ( receiverVal > 1)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}

}

No wonder you aren't getting results... You are taking a reading before you turn on the IR LED.

If only it were that simple. Putting the int receiverVal = analogRead(A0) after the digital pin output does not change anything. It at least did not change the results. :frowning:

Hint: take a reading when the LED is ON, and take a reading when the LED is OFF.

Try this code with Serial Monitor running. If the average onVal and average offVal don't change when you put a hand over the sensor you probably have a wiring error. If the values shift as you move toward and away, publish the output and we can advise you what to do with the data.

const int IRLED = 12;
const int LED = 13;
const int IRInput = A0;
const int SampleSize = 10;
const int CompareThreshold = 17;

void setup()
{
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int onVal = 0;
  int offVal = 0;

  // Take 10 samples with the light ON and OFF
  for (int i=0; i<SampleSize; i++)
  {
    digitalWrite(IRLED, HIGH);
    onVal += analogRead(IRInput);
    digitalWrite(IRLED, LOW);
    offVal += analogRead(IRInput);
  }
  
  Serial.print("average onVal=");
  Serial.print(onVal/SampleSize);
  Serial.print(", average offVal=");
  Serial.println(offVal/SampleSize);


  if ((offVal - onVal) > CompareThreshold)  // Value to be determined experimantally
  {
    digitalWrite(LED,HIGH);
  }
  else
  {
    digitalWrite(LED,LOW);
  }

}

OK so I copy and pasted the code and tried it. It did not look like the numbers where affected by my hand motions. Here is my setup attached between a breadboard and arduino....

GND ____ - Yellow LED + ____ pin 13

GND ____ - IR LED + ____ 100 ohm resistor ____ pin 12

GND ____ - IR Receiver + 10k ohm resistor ____ A0

Should be that simple right?

Here is my question..... this code...

void setup()

{

pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
Serial.begin(9600);

}

void loop()

{
int receiverVal = analogRead(A0);
digitalWrite(12,HIGH);
delay(.5);
digitalWrite(12,LOW);
delay(.5);
Serial.println(receiverVal);

if ( receiverVal > 50)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}

}

.... works perfectly when my LEDs are plugged into the arduino.. but it does not work at all when hooked up to a 9v battery through a 5 voltage regulator on a breadboard... the blink application works but not this code for IR sensing. The only things different between the arduino and my standalone are power source, ( 5v from 9v battery vs. usb from computer from wall ); and timing ( 16mhz resonator vs. 16 mhz crystal ).

What the heck could be wrong. The standalone is virtually the exact same, I have quadroooopal checked where everything is hooked up. Why would it work on the arduino and not on my standalone? PS. I am trying them in the exact same lighting conditions in the exact same spot, so nothing is different.

PPS.... Concerning the previous post... The standalone, when brought into complete darkness like my bathroom, does turn the LED on, but it sort of has the opposite effect, when I bring my hand closer the LED turns off???? Whyyyyyyy? This also proves that the receiver on the standalone IS receiving input, plus the LED works and the IR LED works based on my cameraphone's ability to see infrared. :frowning: :frowning: :frowning: PLEASE HELP

K last post on my part until someone replies I promise! So new and strange development... On my standalone, when I touch the positive side of the receiver with my hand, or touch a bare wire attached to the positive side of the receiver, the LED turns on when I wave my hand above the sensor????? So, I know it can work but why only when I'm touch the positive terminal of the receiver?? any thoughts??

njs707:
OK so I copy and pasted the code and tried it. It did not look like the numbers where affected by my hand motions. Here is my setup attached between a breadboard and arduino....

GND ____ - Yellow LED + ____ pin 13
GND ____ - IR LED + ____ 100 ohm resistor ____ pin 12
GND ____ - IR Receiver + 10k ohm resistor ____ A0

Should be that simple right?

I believe you need to provide a source of power for the IR Phototransistor to work. Try this schematic: