Pages: [1]   Go Down
Author Topic: generation of 40kHz square wave using PWM  (Read 2876 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8687
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Regards,

Dave
« Last Edit: July 01, 2011, 11:19:24 am by davekw7x » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6119
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://www.arcfn.com/2009/07/secrets-of-arduino-pwm.html
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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 http://arcfn.com/2009/07/secrets-of-arduino-pwm.html 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...



Regards,

Dave


« Last Edit: July 01, 2011, 12:33:55 pm by davekw7x » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".

Code:
/*
 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.
}
Logged

Pages: [1]   Go Up
Jump to: