Sioux Falls
Offline
Jr. Member
Karma: 0
Posts: 77
|
 |
« on: June 02, 2011, 10:59:37 am » |
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
Brattain Member
Karma: 143
Posts: 19367
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: June 02, 2011, 12:07:14 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. 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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Sioux Falls
Offline
Jr. Member
Karma: 0
Posts: 77
|
 |
« Reply #2 on: June 02, 2011, 12:27:40 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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19367
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: June 02, 2011, 12:41:01 pm » |
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.
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6607
|
 |
« Reply #4 on: June 02, 2011, 05:12:24 pm » |
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 }
|
|
|
|
|
Logged
|
|
|
|
|
Sioux Falls
Offline
Jr. Member
Karma: 0
Posts: 77
|
 |
« Reply #5 on: June 02, 2011, 10:57:54 pm » |
What is pulse in and would that help me here?
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6607
|
 |
« Reply #6 on: June 03, 2011, 09:00:18 am » |
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
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19367
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: June 03, 2011, 09:03:21 am » |
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.
|
|
|
|
Sioux Falls
Offline
Jr. Member
Karma: 0
Posts: 77
|
 |
« Reply #8 on: June 03, 2011, 01:28:14 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?
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6607
|
 |
« Reply #9 on: June 03, 2011, 01:56:12 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.
|
|
|
|
|
Logged
|
|
|
|
|
Sioux Falls
Offline
Jr. Member
Karma: 0
Posts: 77
|
 |
« Reply #10 on: June 03, 2011, 02:14:57 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); } }
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6607
|
 |
« Reply #11 on: June 03, 2011, 06:13:27 pm » |
No wonder you aren't getting results... You are taking a reading before you turn on the IR LED.
|
|
|
|
|
Logged
|
|
|
|
|
Sioux Falls
Offline
Jr. Member
Karma: 0
Posts: 77
|
 |
« Reply #12 on: June 03, 2011, 10:53:52 pm » |
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. 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19367
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: June 04, 2011, 03:43:51 am » |
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.
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6607
|
 |
« Reply #14 on: June 04, 2011, 08:26:19 am » |
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); }
}
|
|
|
|
|
Logged
|
|
|
|
|
|