# Square Wave Frequency Generation

Hi i feel a bit overwhelmed now I'm having to ask so many questions on here and probably a newbie question but got to ask it so.

I have been trying to understand infrared signals and am struggling to understand how you can generate a signal by using a delay in milliseconds, someone on one website says 36khz is a loop of 13 milliseconds however from what i learn by reading Wikipedia and the like.

1 kHz is equal to 1 milliseconds.

so that would make it 36 milliseconds?

My question is can you provide me with the know how to code and calculate how to generate frequency's i.e 56khz, 36khz, 44khz.

How are people knowing the correct delay to make generate these links will help but i really would prefer a answer on here instead of pointing me at a webpage.

You went the wrong way.

36 kHz has a cycle of 28 us, or .028 ms.

If you want a 50 % duty cycle (a square wave) it would be 14 us on and 14 us off.

But for this sort of signal you might want to directly program a hardware timer, try looking at the TimerOne library and setPeriod(), setPwmDuty() methods.

1 kHz is equal to 1 milliseconds.

No it doesn't.

A signal with a frequency of 1KHz has a period of 1mS.

The frequency of a signal is the reciprocal of the period, and the period of a signal is the reciprocal of the frequency. The reciprocal is simply one over.

Frequency equals 1/time period, so a frequency of 50kHz would have a time period (time for a complete cycle, 1 high and 1 low) of 1/50000, or 0.0002s (0.2ms or 200us).

With an 50% duty cycle (high time exactly half the waveform), you would have a 100uS on time, and 100us off time.

It is worth setting up an arduino to output a known time period, and using an oscilloscope to measure the actual time period. You might see that the exhibited waveform is slightly bigger than expected. This is particularly noticable when you head down to ~20us time periods. You will be able to see that the actual waveform is a few microseconds longer on each half.

[this is because of the time it takes for the arduino to fetch, interpret and execute the lines of code in your program.]

combat this by reducing the delay by a few microseconds, i.e. having a 17us delay may give you a 20us waveform on the scope.

hope this helps

Thank you bushwookie this makes alot of sense, also anyone recommend any pages on hardware style frequency I.e 36khz carrier for infrared.

Thanks, Chris.

There is a library that does it, you can look at the source code.

Shandy: .......... also anyone recommend any pages on hardware style frequency I.e 36khz carrier for infrared.

You can use the enableIROut(36); & Mark functions of IRremote to achieve this directly.

The timerOne library already posted will achieve this too!

Doesn't the IR transmitter chip generate the carrier frequency? You only need to modulate it with the data bits.

I guess you could use a raw IR diode but it would be better to get a dedicated transmitter. They aren't expensive.

MorganS: Doesn't the IR transmitter chip generate the carrier frequency? You only need to modulate it with the data bits.

I guess you could use a raw IR diode but it would be better to get a dedicated transmitter. They aren't expensive.

Normally the modulated IR signal is generated by the PWM of an MCU which feeds into an IR Led driver circuit.

In Arduinoland, the IRremote or IRLib libraries do a very good job for this with an AVR MCU.

I haven't seen many dedicated devices to generate modulated IR*,* without MCUs. [Coincidently, we plan to release one with and one without an MCU in the coming months, as part of a series of IR related modules :) ]

You can generate a 38KHz signal with the PWM timers, here is some code that uses pin 3 to call an ISR. This just modulates it with an 8 pulse on 8 pulse off. You can change it to what ever you want.

``````/* Code to pulse pin 3 with a modulated signal
* Can be used to drive an IR LED to keep a TSOP IR receiver happy
* This allows you to use a modulated receiver and a continuous beam detector
* By Mike Cook Nov 2011 - Released under the Open Source license
*/
volatile byte pulse = 0;

ISR(TIMER2_COMPB_vect){  // Interrupt service routine to pulse the modulated pin 3
pulse++;
if(pulse >= 8) { // change number for number of modulation cycles in a pulse
pulse =0;
TCCR2A ^= _BV(COM2B1); // toggle pin 3 enable, turning the pin on and off
}
}

void setIrModOutput(){  // sets pin 3 going at the IR modulation rate
pinMode(3, OUTPUT);
TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // Just enable output on Pin 3 and disable it on Pin 11
TCCR2B = _BV(WGM22) | _BV(CS22);
OCR2A = 51; // defines the frequency 51 = 38.4 KHz, 54 = 36.2 KHz, 58 = 34 KHz, 62 = 32 KHz
OCR2B = 26;  // deines the duty cycle - Half the OCR2A value for 50%
TCCR2B = TCCR2B & 0b00111000 | 0x2; // select a prescale value of 8:1 of the system clock
}

void setup(){
setIrModOutput();
TIMSK2 = _BV(OCIE2B); // Output Compare Match B Interrupt Enable
}

void loop(){
// do something here
}
``````