Go Down

Topic: HELP with Infrared emitter + sensor problems (Read 9 times) previous topic - next topic

njs707

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

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

njs707

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.


AWOL

You could print the value read from the analogue input.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

johnwasser

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
   }

   
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up