Problem with using delay function

Hello everyone

Well actually I am trying to generate a 38KHz pulse train using Arduino UNO board. The problem I am facing is in the delay. I have my IR LED connected to digital output pin 2. I am probing the IR led using a saleae logic analyzer to see how the pulses looks like after uploading my code on the Arduino board.

Problem I am facing is in the delay. What I want is a high pulse of 15.63us and a low pulse of 10.5us at the logic analyzer. So the period should be 26.13us. The delayfunction I am using in my code is delayMicroseconds.

When I checked the pulse width on the logic analyzer I observed that the high pulse width is increased to 21.4us and the low pulse width is increased to 16.2u. Which increases the period to 37.6u and reduce the frequency to 26.6KHz.

Please guide me about this delay. From where does this delay coming from and how can I reduce it .

Thank You

From where does this delay coming from and how can I reduce it .

From your code, obviously. Fix your code. If you need help, you do need to post your code.

Here is my code for one high and low pulse,

int IRledpin =3;
void setup()
{

pinMode(IRledpin,OUTPUT);

}
void loop ()
{

digitalWrite(IRledpin,HIGH);
delayMicroseconds(15.63);

digitalWrite(IRledpin,LOW);
delayMicroseconds(10.5);

}

Each digitalWrite() takes 3-4 us, so reduce the delays by that amount.

delayMicroseconds takes an integer argument. Those decimals will be truncated. Furthermore, the resolution of delayMicroseconds is only 4us. Using delay of any flavor is not a good way to achieve what you want. I would suggest that you investigate using the hardware timers on the chip to get the sort of resolution you are after.

Dear Delta_G

Can you tell me how can I get access to those timers and which tiemrs are the best fit in my case as I am new to Arduino platform.

musclesinwood:
Dear Delta_G

Can you tell me how can I get access to those timers and which tiemrs are the best fit in my case as I am new to Arduino platform.

It's not really a newbie topic. It's not as simple as a couple of lines of code. A considerable amount of thought and understanding needs to go into it. You should hit up google and find some tutorials on the timers (specifically timer 1) and try to learn a bit about how they work. Once you have the basics to work from then it will be much easier to put together your pulse train.

This will probably take a considerable amount of research on your part. Don't be intimidated, but it might take a day or two of reading and learning.

You are obviously not the first person to want to modulate an IR beam. Why not leverage what someone who knows what they are doing has already done?

PaulS:
Why not leverage what someone who knows what they are doing has already done?

Like adafruit here which is where I got the 3us for the write.

Also the inimitable Dave at EEV Blog has a number of articles about this stuff.

Have a look at this IR comms Thread

Don't use delay() in any serious program. Use millis() or micros() as illustrated in several things at a time.

...R

Best approach is to use the existing libraries - IRremote or IRLib or use the PWM feature of the AVR/Arduino.

If you really want to generate it manually in code the check these blog posts out.