40KHz pwm square wave

i need to generate a 40KHZ square wave can anybody please provide me the the code for the same because i donot have any idea regarding this, i'm a total beginner in microcontrollers and arduino. and can anyone please tell me if we can generate a sine wave of 40KHz via arduino. please help me its damn urgent.

Sine wave? No.

Square wave? Yes. I think you'd have to manually futz with the timer control registers (use a timer other than timer0, so you don't break millis)

You could probably get 40kHz sine with a Due.

Why do you think you need a sine?

Why do you think you need a sine?

He is probably driving an ultrasonic transmitter and thinks you need a sin wave (which you don't ).

OP is asking for square wave.
yes possible
time is 1250 microseconds.
in the loop
current=micros()
if (current-old>1250){invert output, old=current}

Shooter please read the OP message he said:-

and can anyone please tell me if we can generate a sine wave of 40KHz via arduino.

And that code is never going to work.

Generating 40kHz square wave
http://forum.arduino.cc/index.php?topic=139918.0

const byte LED = 3;  // Timer 2 "B" output: OC2B

const long frequency = 40000L;  // Hz

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

  TCCR2A = _BV (WGM20) | _BV (WGM21) | _BV (COM2B1); // fast PWM, clear OC2B on compare
  TCCR2B = _BV (WGM22) | _BV (CS21);         // fast PWM, prescaler of 8
  OCR2A =  ((F_CPU / 8) / frequency) - 1;    // zero relative  
  OCR2B = ((OCR2A + 1) / 2) - 1;             // 50% duty cycle
  }  // end of setup

void loop() { }

actually my project is a very complicated one i need a 40-60 KHz sine wave with constant current 4mA, my project is IMPEDANCE PLETHYSMOGRAPHY if anyone has heard the name, anyways i can convert square wave into a approx sine wave using caps just to make the squares a bit curvy, please give me the code for 40 KHz PWM, i had tried few of the codes in the previous threads, but they wwere not working.

AWOL:
You could probably get 40kHz sine with a Due.

Why do you think you need a sine?

because i need a sine wave to feed to the disposable electrodes and measure the blood flow by impedance plethysmography i guess i'm right. see i do not have any idea of all this i'm a Production engg student i just don't under why my teachers are making me to do such a rocket science project, but anyways please help me

BillHo:
Generating 40kHz square wave
Generating 40kHz square wave - Programming Questions - Arduino Forum

const byte LED = 3;  // Timer 2 "B" output: OC2B

const long frequency = 40000L;  // Hz

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

TCCR2A = _BV (WGM20) | _BV (WGM21) | _BV (COM2B1); // fast PWM, clear OC2B on compare
  TCCR2B = _BV (WGM22) | _BV (CS21);        // fast PWM, prescaler of 8
  OCR2A =  ((F_CPU / 8) / frequency) - 1;    // zero relative 
  OCR2B = ((OCR2A + 1) / 2) - 1;            // 50% duty cycle
  }  // end of setup

void loop() { }

thank you brother i hope this will work

terrorblade:
thank you brother i hope this will work

sorry but just wanted to ask, which pin will i get the output, i mean as i can see there is nothing inside the void loo(), i mean what should i put inside the void loop()

a quick 555 timer setup for 40kHz follow by a 2 pole filter will probably get you a much cleaner and accurate sine wave. if you just need to be able to control it, can use the arduino to start/stop the 555 timer.

software generated waveform is just always going to be on the mercy of any interrupts or anything else that might be happening in your code.

nice idea but as i said i have got zero idea about electronics, one of my friend from electronics dept told me arduino will do everything what i want. please give the the code for 40 KHz square wave, codes mentioned above i had just tried them and they are not working. also i'm not able to understand what to put inside the void loop() function

You don't put anything in the loop() function. The setup() creates the wave, nothing else needs to be done. However, the loop function (albeit empty) has to be there to compile.

That code DOES work. If it doesn't, you need to share more info about which Arduino you have (Uno, MEGA, Due, Leonardo, etc.) and how you're using the signal. Your signal is coming out of Pin 3. Stick a 22 gauge wire in there. You'll also need a wire in one of the GND pins. Stick a 22 gauge wire in there too. Hook those two wires up to an oscilloscope and there's your wave.

stoutfiles:
You don't put anything in the loop() function. The setup() creates the wave, nothing else needs to be done. However, the loop function (albeit empty) has to be there to compile.

That code DOES work. If it doesn't, you need to share more info about which Arduino you have (Uno, MEGA, Due, Leonardo, etc.) and how you're using the signal. Your signal is coming out of Pin 3. Stick a 22 gauge wire in there. You'll also need a wire in one of the GND pins. Stick a 22 gauge wire in there too. Hook those two wires up to an oscilloscope and there's your wave.

stoutfiles:
You don't put anything in the loop() function. The setup() creates the wave, nothing else needs to be done. However, the loop function (albeit empty) has to be there to compile.

That code DOES work. If it doesn't, you need to share more info about which Arduino you have (Uno, MEGA, Due, Leonardo, etc.) and how you're using the signal. Your signal is coming out of Pin 3. Stick a 22 gauge wire in there. You'll also need a wire in one of the GND pins. Stick a 22 gauge wire in there too. Hook those two wires up to an oscilloscope and there's your wave.

thanks a lot mate, i hope everything will be just fine.

this is the output i'm getting and it doesn't seems to be a square wave at all, i had used sound card as the daq device and my arduino is atmega 328 operating at 5 v with 16MHz crystal and in the board section i had chosen arduino pro.

Are you sure your sound card can accurately sample a frequency that fast? Lower the frequency (change 40000L to 4000L in the code) and see what you get.

Your sound card can not display a 40KHz square wave. The arduino can not produce a signal like you have seen even if you want it to.

You seem way over your head with this project and you would be better off learning a bit of theory first.

Grumpy_Mike:
Your sound card can not display a 40KHz square wave. The arduino can not produce a signal like you have seen even if you want it to.

You seem way over your head with this project and you would be better off learning a bit of theory first.

i reduced that const long frequency = 40000L
to const long frequency = 5000L
i was expecting 5Khz output but i got 14khz outpt, my sound card can sample at 96KHz, also sine wave i had generated upto 10KHz from arduino but don't understand how to go for 40KHz, regarding theory mate i'm totaly zero in this i'm not from electronics and this hecky project from my faculty is driving me crazy.

stoutfiles:
Are you sure your sound card can accurately sample a frequency that fast? Lower the frequency (change 40000L to 4000L in the code) and see what you get.

i had already reduced it to 5000L. i do not understand what's going on, is this due to the filtering ckt and ac coupling of the sound card or something else?