ir led + tsop31238 object detection

i ve used an ir led + a tsop31238 to detect an object but with a 220 ohms rezistor it can detect an object only at the distance of 1 cm. i want at least 8 cm. And i modified the code instead of 13 cycles i ve put 15 cycles. at 13 cycles appear only 0 meanig that it something and this value will not change everything i will do. can you help me pls? thank you anticipated !

#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
}

/******************************************************************************

  • This function can be used with a panasonic pna4602m ir sensor
  • it returns a zero if something is detected by the sensor, and a 1 otherwise
  • The function bit bangs a 38.5khZ waveform to an IR led connected to the
  • triggerPin for 1 millisecond, and then reads the IR sensor pin to see if
  • the reflected IR has been detected
    ******************************************************************************/
    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);
    }

Ok first when posting code ALWAYS post it between the square brackets you get when you click the hash icon in the reply box.

Next is this resistor 220R in the LED? It is a bit high for an IR LED it could do with being smaller.

If you change the halfPeriod from 13 to 15 you are changing the modulating frequency from 38Khz to 30KHz and this will make the performance very much worse. I know you say it makes it better but that only illustrates you have other problems.

These sensors are not designed to work in this way, that is as a reflective sensor so it is little wonder you can't get it to work. What happens is that as soon as you stop sending stuff through the LED the output disappearers, so by the time you come to read it it has all gone.

You could try putting a peak detector on the output of the sensor. That is a diode with the anode to the sensor and cathode to the arduino's input. Then from the same input to ground put a 20K resistor. Finally across that 20K put a 0.1uF capacitor. You can try increasing this capacitor, to up to 47uF if necessary.

So, as well as the peak detector put 'halfPeriod' back to 13 and try increasing the value in the cycles variable. I would start by doubling it and then doubling it again.

Thank you very much ! the led isn t have a rezistor. i saw that mode of object detection in a tutorial for an parllax robot so because is very cheap i tried to copy because a sharp sensor is rather expansive in my country. ok i ll try to do that and i ll come back with an answer if can help me with other method of object detection.... other kind of sensor or code. i have an arduino diecimila