I used both IR receivers, IR LEDs and the library to send and receive - so I am kind of familiar with the concept... The thing is that I am trying to convert them into proximity sensors
The idea is simple - you flash the IR LED and if the beam bounces back it is picked up by the IR receiver (tsop1738).
I generate the required frequency using delayMicroseconds and I vary the power using PWM - the idea is that the IR LED continually loops from 0 power to full power and thx to that I can tell how far the object is.
The problem is that I am experiencing a strange phenomena - the receiver almost constantly picks up the beam - I wrapped the IR LED with a couple of layers of sticky tap - that didn't help much. it only works properly at the lowest possible setting (PWM value set at as low as 5) but then the range is as low as 50 cm or so
Does the IR really propagate that well? Even through a couple of layers of sticky tape ? Or maybe sending the impulses interferes somehow with the receiver ?
ohh and before you ask I do know that the receiver output is normally HIGH - and the indicator LED (the one on arduino board) isn't always on - it flashes (check the code)
const int IR = 4; // the number of the IR receiver pin
const int ledPin = 13; // the number of the LED pin (blinks when object detected)
// variables will change:
int IRstate = 0; // variable for reading the IR status
void setup() {Serial.begin(9600);
// initialize the LED and IR LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(6, OUTPUT);
// initialize the IR receiver pin as an input:
pinMode(IR, INPUT);
}
void loop(){
// read the state of the pushbutton value:
for (int i=0; i <= 5; i++){
delayMicroseconds (26);analogWrite(6,i);
delayMicroseconds (26);analogWrite(6,0);
if (digitalRead(IR) == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);delayMicroseconds (3);break;
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}}