creating an IR remote with arduino


I’m having a tough time figuring out how to power an infrared LED with a 40kHz blink.

I’m trying to create a remote to trigger a digital camera (exactly like this example: Nikon IR Remote Shutter Release).

With delayMicroseconds() I think I can create the trigger sequence that the camera needs, but the camera’s IR sensor requires the ir light to be modulated at 40kHz. delayMicroseconds() does not seem accurate enough in itself to create the 25 microsecond pulse.

Outputs 9, 10, and 11 are pulse width modulation outputs so it seems they might be usable, but I don’t know how to set the modulation rate. Also, I’ve bumped into pulse.h but I don’t know how to use it.

Could somebody point me to a tutorial or a teeny code snippet or a description of 9, 10, and 11, pulse.h or any other way to modulate a digital or pwm output.

Thanks a ton,


the trick is farily simple…

at the beginning of your init() function you can add a couple of instruction to change the PWM frequency (see atmega8 datasheet) then conect an IR diode between the PWM pin and a regular pin that you’re going to use to transmit data. the anode of the diode is connected to the data pin and the pwm pin will be connected to the cathode.

set the pwm duty cycle to 50% (127)

this trick allows you to send data modulated at whatever frequency is produced by the pwm without having to code complex assembler loops…


Hello massimo,

Thanks for the help. I looked through the ATmega8 docs and I think I found the basics of what I need to know. It tells me that I can set the PWM frequency with the following equation: fclk_I/O / N * (1 TOP)

The docs also state that “However, if the base PWM frequency is actively changed (by changing the TOP value), using the OCR1A as TOP is clearly a better choice due to its double buffer feature.”

In the end I hope that its as simple as #1 determining the clock I/O speed, #2 setting the TOP value to the divisor that will yeild the correct frequency (using the OCR1A register), and #3 setting the PWM duty cycle to 50% (127) to ensure an equal length of time off and on. I hope that once I have finished steps #1 and #2, that I can analogWrite(pin9, 127) and end up with a PWM output modulated at 40kHz.

The tricky thing for me is that I can play with predefined methods fine, but I don’t have assembly experience. I don’t know what the value of the clk_I/O is and I don’t know how to set TOP to the value I want using the OCR1A register.

Could you please tell me if I’m on the right track or not? If I am on the right track, could you tell me what the clk_I/O is for arduino and how to set the TOP value using the OCR1A register?

This is my first embedded experience, so although I read through the PWM sections of the ATmega8 documentation there was alot I was not understanding.

Thanks a ton for the help,

Arduino runs at (clock i/o) is 16 MHz (16,000,000). To set OCR1A, I think
you can simply assign to it as a variable:

OCR1A = 0x8000

You might also try ICR1A instead, as I think OCR1A is used to determine
the duty cycle (but the ATmega8 datasheet is very confusing, so I could
be wrong).