Arduino Forum

Topics => Interactive Art => Topic started by: njs707 on Jun 02, 2011, 05:59 pm

Title: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 02, 2011, 05:59 pm
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!!
Title: Re: HELP with Infrared emitter + sensor problems
Post by: AWOL on Jun 02, 2011, 07:07 pm
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.

Quote
The infrared LED is hooked up to digital pin 13

...via a suitable resistor, I assume?
Quote
I have the regular LED attached to digital pin 12

...ditto.

I think you may be a little optimistic expecting a 20cm range.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 02, 2011, 07:27 pm
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.

Title: Re: HELP with Infrared emitter + sensor problems
Post by: AWOL on Jun 02, 2011, 07:41 pm
You could print the value read from the analogue input.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: johnwasser on Jun 03, 2011, 12:12 am
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.

Code: [Select]

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
   }

   
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 03, 2011, 05:57 am
What is pulse in and would that help me here? 
Title: Re: HELP with Infrared emitter + sensor problems
Post by: johnwasser on Jun 03, 2011, 04:00 pm

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.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: AWOL on Jun 03, 2011, 04:03 pm
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.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 03, 2011, 08:28 pm
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?
Title: Re: HELP with Infrared emitter + sensor problems
Post by: johnwasser on Jun 03, 2011, 08:56 pm

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.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 03, 2011, 09:14 pm
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... :( 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);
  }
 
}
Title: Re: HELP with Infrared emitter + sensor problems
Post by: johnwasser on Jun 04, 2011, 01:13 am
No wonder you aren't getting results...  You are taking a reading before you turn on the IR LED.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 04, 2011, 05:53 am
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. :(
Title: Re: HELP with Infrared emitter + sensor problems
Post by: AWOL on Jun 04, 2011, 10:43 am
Hint: take a reading when the LED is ON, and take a reading when the LED is OFF.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: johnwasser on Jun 04, 2011, 03:26 pm
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.

Code: [Select]

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

}
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 04, 2011, 06:11 pm
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?
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 04, 2011, 08:10 pm
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.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 04, 2011, 08:13 pm
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.  :( :( :( PLEASE HELP
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 04, 2011, 09:23 pm
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??
Title: Re: HELP with Infrared emitter + sensor problems
Post by: johnwasser on Jun 04, 2011, 09:26 pm

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:

http://sites.google.com/site/therobotronics/arduino/connect-a-phototransistor-to-arduino
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 04, 2011, 10:17 pm
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
Title: Re: HELP with Infrared emitter + sensor problems
Post by: AWOL on Jun 04, 2011, 11:04 pm
Code: [Select]
delay(.5);
What makes you think you can delay 500 microseconds like that?
Title: Re: HELP with Infrared emitter + sensor problems
Post by: johnwasser on Jun 04, 2011, 11:43 pm

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.



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.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 05, 2011, 01:51 am
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?   
Title: Re: HELP with Infrared emitter + sensor problems
Post by: johnwasser on Jun 05, 2011, 02:49 am
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.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 05, 2011, 03:21 am
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
Title: Re: HELP with Infrared emitter + sensor problems
Post by: johnwasser on Jun 05, 2011, 04:23 am
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.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 05, 2011, 07:43 am
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);
  }

}
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 05, 2011, 07:53 am
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);
}
 
Title: Re: HELP with Infrared emitter + sensor problems
Post by: johnwasser on Jun 05, 2011, 02:18 pm
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.

Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 05, 2011, 06:40 pm
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.
Title: Re: HELP with Infrared emitter + sensor problems
Post by: njs707 on Jun 06, 2011, 01:28 am
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?