How to generate constant 38 kHz square wave for IR light source?

Your effort is misdirected; these receivers are not all that sensitive to precise frequency but they will not maintain output with a constant modulated signal.

Instead, they are engineered to look for "bursts" of modulation and will reject noise that does not look like a train of pulses of modulated carrier; to get them to continue to work you will need not one signal generator but two, one for the modulation and the other to create the bursts in the envelope of modulation.

The first generator produces 38 KHz modulation.

The second generator keys the first generator on and off at a rate within the window of the types of remote signals the receiver is designed to accept.

Typically, you use a microcontroller; use a timer channel divided from the MCU clock to the produce the 38 KHz to closest approximation you can. It does not need to be exact.

Then either have software, or another timer channel key this on and off.

(You could probably use 556 dual timer; but MCUs that can do this are well under a dollar in quantity, require fewer support components, and can do other things as well).

If you were expecting constant output from the detector, you will need to rethink your system design; instead, you probably need to follow the detector with a pulse-stretcher that can fill in the gaps between your transmit pulses; should your transmission cease, the pulse strether will extend the last pulse from the receiver, then stop.


Grab an ATMega328p and put a 7.6MHz quartz crystal on it. Then with Phase Frequency Correct mode with ICR1 as 100 you will output an exact 38kHz signal.

To do this:

Set the MCU fuses when uploading the code, to these values:

low_fuses= 0x7D = 1111 1101
bit 7 = 1 = CKDIV8  = Divide clock by 8
bit 6 = 1 = CKOUT = clock output
bit 5 = 1 = SUT1 = Select start-up time
bit 4 = 1 = SUT0
bit 3 = 1 = CKSEL3 = Select clock source
bit 2 = 1 = CKSEL2
bit 2 = 0 = CKSEL1 
bit 0 = 1 = CKSEL0

The code to produce the 38kHz on pins PB1/OSC1A and PB2/OSC1B pins will be:

// Set Timer1 to phase and frequency correct mode. NON-inverted mode
TCCR1A = _BV(COM1A1) | _BV(COM1B1); 

// Set prescaler to clk/1
TCCR1B = _BV(WGM13) | _BV(CS10);

//ICR Register, which controls the total pulse length
ICR1 = 100; // Divides clock by 100/2, so 7.6MHz/100/2 = 38kHz
//OCR Registers, which control the duty cycle.
// OCR1A + OCR1B must be = IRC1.
OCR1A = 50; // 50% of the pulse will be LOW state
OCR1B = 50; // 50% of the pulse will be HIGH state

If the frequency is all that critical you may want to consider a "Phase Locked Loop" (PLL). A crystal oscillator having a "Smallest Common Denominator" with the frequency you are trying to produce should produce less jitter (if that is even a factor). Just as an example 1.9 MHz divided by 38 kHz = 50 times. So a 1.9 MHz divided by 50 would be your 38 kHz. I hope this helps.