Pages: [1] 2 3   Go Down
Author Topic: HELP with Infrared emitter + sensor problems  (Read 8941 times)
0 Members and 1 Guest are viewing this topic.
Sioux Falls
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26531
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Sioux Falls
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26531
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could print the value read from the analogue input.
Logged

"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.

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
   }
   
Logged

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

Sioux Falls
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is pulse in and would that help me here? 
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26531
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Sioux Falls
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Sioux Falls
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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... smiley-sad 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);
  }
 
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Sioux Falls
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-sad
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26531
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

}
Logged

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

Pages: [1] 2 3   Go Up
Jump to: