IR sensor program

Hi,
I'm tring to work on a simple IR emitter-reciver sistem and I've spent on it 5 days now and still is not working :frowning: .The pourpouse is to understand is there is an obstacle between the emitter-reciver, but I have several problems. As a reciver I'm using a TSOP32238 of Vishay, is a 3 pin sensor, 38,5khz and is the only thing that work because I've connected its signal to a led and it's blinking if I use my stereo remote on it. The emitter seems not working properly, dont think because of electrical connection, probably is a code issue. Here's the code I'm using:

//define pins. I used pins 4 and 5
#define irLedPin 4 // IR Led on this pin
#define irSensorPin 5 // IR sensor on this pin

int irRead(int readPin, int triggerPin); //function prototype

void setup()
{
pinMode(irSensorPin, INPUT);
pinMode(irLedPin, OUTPUT);
Serial.begin(9600);
// prints title with ending line break
Serial.println("Program Starting");
// wait for the long string to be sent
delay(100);
}

void loop()
{
Serial.println(irRead(irSensorPin, irLedPin)); //display the results
delay(10); //wait for the string to be sent
}

int irRead(int readPin, int triggerPin)
{
int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
int i;
for (i=0; i <=cycles; i++)
{
digitalWrite(triggerPin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(triggerPin, LOW);
delayMicroseconds(halfPeriod - 1); // - 1 to make up for digitaWrite overhead
}
return digitalRead(readPin);
}

Does anyone know where I made a mistake? :cold_sweat:

ps if it's needed I could post the electrical scheme overhere.

Thanks

Does anyone know where I made a mistake?

Not using an existing IR library.

And while we're at it, the always useful reminder :stuck_out_tongue_winking_eye:

http://arduino.cc/forum/index.php/topic,97455.0.html

Ok, thx PaulS, I have worked with the library you suggested me. First of all it works and helped me to learn more about ir, but now I still have a problem: this library cant allow you to transmit and recive at same time..... so for my project is not complete. Do you have any other suggestion?

Thx

this library cant allow you to transmit and recive at same time.

What, exactly, are you trying to do? You should be able to switch between sending and receiving, since they use different hardware.

Hi!

Anyone can suggest what command can i use if my reading from a sensor (assuming IR distance sensor includes cable (20cm-150cm) [GP2Y0A02YK] : ID 1031 : $15.95 : Adafruit Industries, Unique & fun DIY electronics and kits) produces a certain voltage, then it would convert it into a 1 count?

I am about to create a counter using the said sensor.
Appreciate any ideas :slight_smile:

Anyone can suggest what command can i use if my reading from a sensor (assuming IR distance sensor includes cable (20cm-150cm) [GP2Y0A02YK] : ID 1031 : Adafruit Industries, Unique & fun DIY electronics and kits) produces a certain voltage, then it would convert it into a 1 count?

If your sensor is connected to analog pin 2,

   it = 0;
   int distVal = analogRead(2);
   if(distVal > aCertainVoltage)
   {
      it = 1;
   }

hello,
i am making an automatic street light controller using ir sensor.
in my project i have 4 ir sensor and 5 power LED(will act as street light).I gave the 5v input to the IR sensors from arduino 5v pin (to all 4 sensor). and connected the led to the respective digital pins. but i tried to upload the code the code wasn't uploading.
Next what i did :
i took the supply from external source and gave it 5v and gnd of the sensors. and nly connect the out of ir sensor to arduino. Even the Power led i connected through Transistor such that base was given to transistor, collector to external supply, and emitter the power led and other pin of led to gnd. still i had the same problem

this is my code
#define IR1 1
#define IR2 2
#define IR3 3
#define IR4 4
int detection1; // no obstacle
int detection2;
int detection3;
int detection4;
int ledPin1 = 6;
int ledPin2 = 7;
int ledPin3 = 8;
int ledPin4 = 8;
int ledPin5 = 9;
int Value1=10;
int Value2=50;
void setup()
{
Serial.begin(9600);
pinMode(IR1, INPUT);
pinMode(IR2, INPUT);
pinMode(IR3, INPUT);
pinMode(IR4, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
}
void loop() {
detection1 = digitalRead(IR1);
detection2 = digitalRead(IR2);
detection3 = digitalRead(IR3);
detection4 = digitalRead(IR4);
analogWrite(ledPin1, Value1);
analogWrite(ledPin2, Value1);
analogWrite(ledPin3, Value1);
analogWrite(ledPin4, Value1);
analogWrite(ledPin5, Value1);
if(detection1 == LOW)
{
analogWrite(ledPin1, Value1);
}
else
{
analogWrite(ledPin1, Value2);
analogWrite(ledPin2, Value2);

}
if(detection2 == LOW)
{
//analogWrite(ledPin2, Value1);
}
else
{
analogWrite(ledPin1, Value2);
analogWrite(ledPin3, Value2);
analogWrite(ledPin2, Value2);
}
if(detection3 == LOW)
{
//analogWrite(ledPin2, Value1);
}
else
{
analogWrite(ledPin4, Value2);
analogWrite(ledPin3, Value2);
analogWrite(ledPin2, Value2);
}
if(detection4 == LOW)
{
//analogWrite(ledPin2, Value1);
}
else
{
analogWrite(ledPin4, Value2);
analogWrite(ledPin3, Value2);
analogWrite(ledPin5, Value2);
}
delay(100); // in ms
}

#define IR1 1

Try a different pin, keep 0 & 1 free for Serial use (downloads and Serial.print debug statements)

analogWrite only works on pins 3,5,6,9,10,11
7, 8, 8 won't support it - and you have pin 8 used twice.

Sorry that program i had corrected sent a old program by mistake.
could u please help in why that could is not getting dumpped even when i am apply external power supply(reason being that the sensor and power led of 1w should not drain the current from the arduino on board pins)

i need program for ir senor drowsiness detection using ir sensor pls post me