Infrared trigger code

I am quiet confused with the codes given on this site:
Arduino Playground - PanasonicIrSensor

#define irLedPin 4
#define irSensorPin 5

int irRead(int readPin, int triggerPin);

void setup()
{
pinMode(irSensorPin, INPUT);
pinMode(irLedPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{
Serial.println(irRead(irSensorPin, irLedPin));
delay(10);
}

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

The irRead() function generates the standard frequency required for IR LED.
I don't understand why is there a need to loop.
Having only 1 cycle: HIGH(13us) + LOW(13us) = ~38.5khz
Why is there a need to loop the for up to 38 cycles?
Will it have different result using the following code:

digitalWrite(triggerPin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(triggerPin, LOW);
delayMicroseconds(halfPeriod - 1); // - 1 to make up for digitaWrite overhead
return digitalRead(readPin);

Another thing, what is meant by comment: "- 1 to make up for digitaWrite overhead"
A digitalWrite command takes 1us to execute?

Hi,

Why is there a need to loop the for up to 38 cycles?

Cos..

In a nutshell, you have to output a 38.5khZ square wave to the IR led for 1 millisecond

Without the for loop, the output will be one cycle at 38.5kHz.

Tom.... :slight_smile:

Hi,

  • 1 to make up for digitaWrite overhead

The group of instructions produces the full cycle of the square wave.

Because the halfPeriod is an integer value you cannot get a half value, so halfperiod may be higher r lower than what it really should be.
So by subtracting from 1 you have the remaining time corrected to give the proper period of the squarewave.

Hope it was clear enough..

Tom... :slight_smile:

"In a nutshell, you have to output a 38.5khZ square wave to the IR led for 1 millisecond"
Implicitly that is what I am questioning...
Why is there a need to produce the 38.5khz for 1millisecond?
Why is 1 cycle not enough to send as trigger?
I am creating a project where time is of the essence. 1ms I think is huge in terms of processing.

"So by subtracting from 1 you have the remaining time corrected to give the proper period of the squarewave."
I really don't get it.
The only reason I can think of is to offset the period by subracting -1 as a result of having a code before the delay which is: digitalWrite(triggerPin, LOW);

Hi,
1 millisecond, because it takes time for the the system to respond, a single cycle could be thought of as noise.

Where as a signal 38 times longer would be detected as a valid signal.

What you have to realise is that the Rx will be looking at a noisy environment, everything around it emits IR energy.
So if it sees 1 millisecond of 38kHz it knows that is the Tx IR energy.
The Rx circuitry is tuned to 38.5kHz.

As for the -1, yes, there are delays due to stepping from instruction to instruction,

Tom..... :slight_smile:

But where can I find in specs that says it should transmit 38 pulses for 1ms for accuracy in RX?

"As for the -1, yes, there are delays due to stepping from instruction to instruction,"
A line of code consumes 1us? why did he subtract only during LOW, why is there no -1 for HIGH?
Can you point me to some site where it proves the execution of digitalWrite() is 1us...


I checked the specs:
http://www.semicon.panasonic.co.jp/ds4/PNA4602M_AEK_discon.pdf

The Center Frequency is 38khz, but low/high pulse width is 400us.
I don't get it how it become 400, i expect it just 26us similar to the code.

Based also there that:
"when constant 16 pulses are transmitted with the output of the transmitter unit corresponded to the maximum detection distance"
But the pulses they are talking should be 400us for both HIGH and LOW.

This becomes weird.
But the pulses may be for the sensing distance that the receiver can detect.

I also studying ultrasonic sensor, based from some forum, this codes work.
It only transmit 1 cycle of trigger 10us.
But inside the ultrasonic transmitter, after receiving that 1 cycle, it burst out 8pulses.
So maybe the 38cycles in IR is right, but I just need some proof to implement that.
As I said 1ms is I think a lot of time.

specs:

http://www.robot-electronics.co.uk/htm/srf05tech.htm

Code from arduino forum:

unsigned int measure_distance()
{
// Trigger the SRF05:
digitalWrite(SONAR_TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(SONAR_TRIGGER_PIN, LOW);

unsigned long pulse_length = pulseIn(SONAR_ECHO_PIN, HIGH);

delay(50);

// Convert Pulse to Distance (inches)
// pulse_length/58 = cm or pulse_length/148 = inches
return( (unsigned int) (pulse_length / 148) );
}