generation of 40kHz square wave using PWM

Can i generate 40kHz square wave using PWM of Arduino??

You can generate a 40KHz square wave with one of the ATmega timers. Since the pulse width of a square wave does not change it is not a task for PWM. The frequency may not be exactly 40KHz but should be quite close.

msdt: Can i generate 40kHz square wave using PWM of Arduino??

Ken Sharriff's excellent IRremote library has a function that can create and modulate a 40 KHz square wave on Arduino pin 3 (using Timer 2) for driving an IR LED to talk to IR Remote devices.

If your application is something else, you still might benefit from looking at the code to see how he does it.



actually i wanted to generate 40kHz square wave to be given to the transmitter of GL ultasonic sensor which is designed for 40kHz. I din't get the code you mentioned. I dint get the basic idea how to proceed.

You probably want to read Ken’s “PWM Secrets” article. In particular, you’ll want to read the section on Fast PWM.

msdt: actually i wanted to generate 40kHz square wave to be given...

If I thought I could explain it better than the awesome Mr. Sharriff, I might try. Alas, I don't think I can accomplish that. Maybe someone else here feels like giving it a shot.

If you are interested in learning, you can check his code, as I suggested. In particular:

void IRsend::enableIROut(int khz) {
  // Enables IR output.  The khz value controls the modulation frequency in kilohertz.
  // The IR output will be on pin 3 (OC2B).
  // This routine is designed for 36-40KHz; if you use it for other values, it's up to you
  // to make sure it gives reasonable results.  (Watch out for overflow / underflow / rounding.)
  // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B
  // controlling the duty cycle.
  // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A)
  // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin.
  // A few hours staring at the ATmega documentation and this will all make sense.
  // See my Secrets of Arduino PWM at for details.

The "IR output" to which he is referring is, simply, a square wave on Arduino pin 3. Use it for whatever you want to. He also shows how to turn the output signal on and off with a single instruction (without starting and stopping the timer each time), so it can be useful for lots of stuff. Maybe even your application.

If you have specific questions, I'm betting that someone here is willing and able to try to help...



Since nobody gave a definitive answer, this simple sketch will do. I created this after reading Ken's excellent introduction to PWM, and you should too. Details and scope traces on my youtube channel "softweyr".

 40 KHz via fast pwm mode on pin 3.
 I think. 

void setup()  { 
  pinMode(3, OUTPUT);
  pinMode(11, OUTPUT);
  TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(WGM22) | _BV(CS20);
  OCR2A = 199;
  OCR2B = 100;

void loop()  { 
  // Nothing to see here.
  // These are not the I/O pins you are looking for.