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

• Application: IR light barrier over a distance of 1m

• Receiver: e.g. Vishay TSSP4038 IR sensor (tuned to 38 kHz)

• Ambient temperature range: -10 to 40° C

Here I’ve found a circuit with a Schmidt trigger that should do the trick, if there was a crystal oscillating at 38 kHz. But I can’t find any. Digikey lists 38 kHz crystals, but – looking at the spec sheets – it turns out that they are actually 32,768 Hz crystals.

How do I get 38 kHz?

I’ve also considered an NE555 based circuit, but that’s not temperature stable, and it requires trimming. Another option, I’ve been told, would be using an Arduino to generate the 38 kHz, but that looks like overkill.

# Update

Following the recommendation in the application note mentioned in Ali Chen's answer, I finally decided to use an Arduino Nano. For generating the 38 kHz, I used the timer as described in an Arduino forum post by Nick Gammon to the Arduino forum. His code example:

const byte LED = 11;  // Timer 2 "A" output: OC2A

void setup() {
pinMode (LED, OUTPUT);

// set up Timer 2
TCCR2A = _BV (COM2A0) | _BV(WGM21);  // CTC, toggle OC2A on Compare Match
TCCR2B = _BV (CS20);   // No prescaler
OCR2A =  209;          // compare A register value (210 * clock speed)
//  = 13.125 uS, so frequency is 1/(2 * 13.125) = 38095 Hz

}  // end of setup

void loop() { }


Via an interrupt pin, I hooked the Nano up to a Yún, which runs the main logic and which tells the Nano to turn the signal on or off. Keep in mind that the Vishay application note is about measuring the distance to an object, whereas my application is simply about measuring whether a beam is interrupted or not.

Next steps: Get rid of the Nano; Upgrade from 38 to 56 kHz for slightly faster sensing.

• MCU. More characters to fill out the minimum. Feb 7, 2017 at 18:13
• Consider how "temperature stable" a highly cost-optimised, mass-produced, nasty, generic TV remote-control's IR generator might be. I would not be surprised if a 32,768 Hz crystal would be ideal for this application.
– user98663
Feb 7, 2017 at 18:37
• It it for reflective sensing, or for a direct sight of view application? Feb 7, 2017 at 18:43
• @AliChen through beam Feb 7, 2017 at 20:35
• Your challenge is not frequency accuracy, but rather that the receiver will quickly ignore a signal if the modulation is not itself turning on and off at a rate within expectation. Note the mention of "burst" in the data sheet. Feb 8, 2017 at 5:58

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.

• You are right, the demodulator in this kind of receivers is "engineered" with a need to have some minimal gap between active bursts of carrier pulses. Specifications for this particular part does not mention this, but other similar Vishay products do mention the minimal gap explicitly. But I would not say it is "engineered" that way, I think this is a design deficiency, a bug. Products from Sharp have similar feature. Feb 8, 2017 at 19:46
• @AliChen Amplitude detection requires automatic-gain-control (AGC) to accept huge carrier amplitude range. Gain control time constant is engineered-in and is fixed to keep these chips simple. Most have fairly short time constant, a few are longer. But all must with high-gain after awhile. Feb 8, 2017 at 20:45
• @glen_geek, on a second reflection, yes, the AGC can explain why do you need a gap in modulation. It is needed when you want to receive successive bits of modulation. But how it explains why the receiver can't produce a sustained output with non-modulated carrier input? The Vishay TSSP4038 specifications could be more clear on the signal limitations. Feb 8, 2017 at 23:24
• You persist in trying to invert the meaning. Constant modulated signal is exactly what was meant, and is obviously distinct from an unmodulated signal. If you don't understand, you had best not try to change other people's words! Feb 9, 2017 at 3:05
• Language has the property of allowing us to add modifiers to terms to extend their meaning. "Constant" is a clear example thereof; it also happens not to be my original choice of word, but rather the word used in the question itself to describe the particular goal that would be counterproductive here - a constant modulated signal will not work, what is needed is a modulated signal that is not constant. Feb 9, 2017 at 6:51

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


The Vishay TSSP4038 IR sensor spec shows responsivity vs relative frequency. A 555 with temperature stable external components should keep you within a reasonable range of the responsivity, but you need to figure out what percentage responsivity you can suffer over the full range (a spec of 0% is never realistic, but the actual percent is very application specific). You just have to do some math to figure out worst case drift vs temperature on the RC components and figure out if you can take the responsivity hit.

Alternatively, a microcontroller with a crystal for timing utilizing a timer output to toggle a pin at the correct time would work and would no doubt be more stable.

The Vishay receiver specs show the filter characteristic as +-5% at 3dB attenuation relative to nominal frequency (38kHz).

The best (SE555 TI timer) has internal sensitivity of 90ppm/C typical, which over 50 C span results in 4500ppm, or 0.45%.

To get to 38kHz, you will need to use a 10nF capacitor with 2k resistor. You can use NPO 1% tolerant ceramic caps (say, from Murata), and if you get a stable power supply as well, you should be able to get something like 2-3% stability over 50 C range.

However, the above is more like a wishful thinking, since TI itself doesn't guarantee anything, see this blog entry.

In addition, as Chris Stratton mentions, the Vishay receiver part does not guarantee a steady output if a steady optical signal at carrier frequency is applied, the receiver needs some "breaks" between burst of carrier pulses, see specifications for a similar part as TSOP6238.

UPDATE: I just did a quick experiment with a similar IR receiver, GP1UV700QS (36kHz, by Sharp). With an IR LED driven from a signal generator as source, the behavior is as follows: if no IR signal, the receiver output is HIGH; if signal is started and continues, the receiver goes LOW, and then goes HIGH by itself. The LOW pulse duration depends on proximity to emitter. At close distance (20cm) the pulse can be as long as 300 ms, while at a longer distance (1m) it shrinks to 1-2 ms, down to toggling at 250 us. Since the carrier frequency response characteristic is rather broad (5% at -3dB), and the dynamic range is huge, it really does not matter much if the carrier gets deviated by +-5kHz, results look the same.

BOTTOM LINE: If the IR emitter signal is unmodulated, just a constant-amplitude 38 kHz carrier, the class of IR receivers as Vishay TSSP40xx or Sharp GP1UV70xx will not sustain the logic detector output. To get these ICs to function as "light barrier", the carrier amplitude must be modulated into distinctive bursts of about 1 ms ON, and ~1 ms carrier OFF. Unfortunately, the output will toggle accordingly, so to use the receiver as "go-nogo" detector the processing circuit must detect the toggling.

So it might be easier to use a crystal with a microprocessor, and use internal programmable timers to get the right frequency and right modulation, as the original Vishay appnote suggests.

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.

• @JMRM22 Thanks for catching my error. It is important to provide correct information to the readers. Feb 9, 2017 at 16:05

Two simple options for a 38kHz square wave:

1. Use the Voltage Controlled Oscillator built into this PLL chip: http://www.ti.com/lit/an/scha002a/scha002a.pdf (See p. 16 of the PDF for how to choose components to set the output frequency)

2. Build a relaxation oscillator with an op-amp and temperature-stable resistors: http://www.falstad.com/circuit/e-relaxosc.html

A 555 would likely be the easiest solution, and it might be stable enough across a large temperature range.

38kHz crystal on Ebay Just use the selection box for the right frequency.

BUT! considering these exact sensors are used with Arduinos, that usually have 16MHz clocks, I would consider doing some research on Arduino code for these sensor kits, as there might be some finished code for your project!