HELP with Infrared emitter + sensor problems

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:

Ok, tried that but it seems weird to connect an analog pin to ground and the collector 2 5v. So I'm clear... this is how I would set this up using the atmega328 on a breadboard...

VCC ____ - Receiver + _____ A0 pin __________________
|

  • _____ 10k ohm ____ ground ______|

... or what??

This is what I am using... http://www.instructables.com/image/F4M09OIF6S98GAG/IR-Detector-Circuit.jpg

delay(.5);

What makes you think you can delay 500 microseconds like that?

njs707:
VCC ____ - Receiver + _____ A0 pin __________________
|

  • _____ 10k ohm ____ ground ______|

Don't connect A0 to Ground. The diagram is a bit confusing because it uses the same type of line to show the edge of the Arduino and the connections TO the Arduino. The resistor and Arduino are connected to a common ground. The A0 input is only connected to the junction between the phototransistor emitter and the load resistor.

njs707:
http://www.instructables.com/image/F4M09OIF6S98GAG/IR-Detector-Circuit.jpg

That example uses a digital input with the internal pull-up resistor enabled. Current through the phototransistor drags the signal down enough to register as a 0. The analog inputs don't have the option of internal pull-up resistors (as far as I know) so you need to provide the +5v source. Current through the phototransistor and resistor raises the voltage across the resistor and this voltage is measured by A0.

What should I be pulsing the IR LED at?

digitalWrite(13,HIGH);
delay( x );
digitalWrite(13,LOW);
delay( x );

what is a good value or range of values for x?

I did not put delays into the code I provided and I do not recommend you change the code randomly without understanding exactly why you are doing it. Perhaps you can explain exactly WHY you want to 'pulse' the IR LED?

If you are at a strange house and you want to see what light switch does what you flip the switch on and off, perhaps a few times, and look for any light changes. If you look out the window, close your eyes, start flipping the switch madly up and down, then put it back in the initial position and look again you will not sense any light changes, no matter how quickly and how long you flip the switch up and down.

oh, I guess I misunderstood something I read about having the IR emitter pulsed so the receiver can take it in.

" We write an Arduino sketch to make this LED flash 38,000 times per second so it can be detected by the PNA4602M. " I've read similar statements like this from different sources. It is flashed at 38 khz because that is what the receiver looks for, so it takes light with different frequencies out of the equation.... is this wrong or idk? haha. sounds right. maybe. Anyway that is why I am pulsing it.

This statement was taken from a powerpoint found online here.... web.cecs.pdx.edu/eas199/C/notes/1/1_IR_object_detection.ppt

please help. let me know if this is wrong and why, or visa versa

If you had a three-terminal IR Remote Control receiver, they have a bandpass filter to select lights pulsed at 38-40KHz. They also have an analog comparator to get an on/off output. You hook them up to +5v and Ground and the output signal turns ON if you hit the sensor with 38-40KHz modulated IR. If that's the kind of sensor you had you wouldn't be hooking it up to an analog input.

You have a bare IR phototransistor. It does not have a bandpass filter or comparator.

ok so I tried your code john and experimented with different CompareThreshold values and determined it works the best at 250. Here is the problem, it only works in the dark. When a light is on there is too much IR being emitted from the light and causes the LED to always be on. Not sure how to get around that...

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);
}

}

Please do not forget to look at my previous post as well as this one....

but one more dumb question... If the IR receiver reads a 0, does that mean it is reading no infrared light or it is reading the max it can handle? Because When I ran this code... It read zeros when my light was on, and really high numbers when the light was off(almost in complete darkness), so that must mean a higher number means a lower amount of infrared being read right??

const int IRInput = A0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int onVal = analogRead(A0);
Serial.println(onVal);
}

The phototransistor should conduct more electricity when light hits it. If it is hooked up as in the diagram I referenced:

+5v ----- (c)Phototransistor(e) ----- [A0] ----- Resistor ----- Ground

the voltage across the resistor should go UP as the light increases. I don't understand why you are getting the opposite effect unless you have wired it differently. you are getting the opposite effect then I think the wiring is done differently.

Try different values fro the resistor to see what effect that has on the output.

I just realized I worded my last post poorly, when I say " light was on "... I did not mean IR led, I meant incandescent light only, just to test how much the values change when someone would turn a light switch on or off. The room was completely dark, then I switch an incandescent light on, then off. Not ir led, sorry for the confusion if there was any.

Man this stuff is frustrating.

Ok, I want to set up an IR receiver that will calibrate to the environment... ( check to see how much ir light is in the environment) and then set that as the new zero level, so if the other IR receiver receives a little more ir light than that new zero ( threshold), then an led will turn on. To test this, I simply have 2 of the exact same infrared receivers hooked up the exact same way with the exact same resistors to analog 0 and 5 on the arduino. The 2 receivers are not more than 1 inch apart and are at the same level vertically. The problem is they are receiving vastly different ir values?? why?

here is the code and the resulting values.....

int Calibration;
int Receiver;

void setup()

{
int Receiver = analogRead(A5);
int Calibration = analogRead(A0);
Serial.begin(9600);
Serial.println("Calibration Input = ");
Serial.println(Calibration);
delay(1000);
Serial.println("Receiver Input = ");
Serial.println(Receiver);
}

void loop()
{}

here are the values..

Calibration Input =
133
Receiver Input =
359
Calibration Input =
141
Receiver Input =
377...

HOW COULD THEY BE SO DIFFERENT?