PWM of square wave

Hello everyone,

I would like to to write a piece of code for PWM of a square wave, and then pass it through a low pass filter (hardware) to make a 40 KHz sine wave.

My questions:

1) Can I use digital output pins for it ? or it must be analogue output? 2) what is the code for PWM of the square wave ?

Thank you so much

I would like to to write a piece of code for PWM of a square wave,

It is impossible because if it a PWM signal then it is not a square wave. http://www.thebox.myzen.co.uk/Tutorial/PWM.html

to make a 40 KHz sine wave.

So you actually want a sin wave? You can do that with this link:- http://interface.khm.de/index.php/lab/interfaces-advanced/arduino-dds-sinewave-generator/

But I don't think it will go to 40KHz. Why do you need that? Odds are you don't.

It is impossible because if it a PWM signal then it is not a square wave.

That PWM signal in your link looks an awful lot like a square wave. In what ways isn't a PWM signal a square wave?

In what ways isn't a PWM signal a square wave?

Because square means 50% duty cycle. Most of the time a PWM wave is a rectangular wave.

lightt: 1) Can I use digital output pins for it ? or it must be analogue output?

I think you must use "PWM output" pins. Typically these are a few of the digital output pins. Please refer to the board description, which outputs are PWM enabled on which board.

lightt: 2) what is the code for PWM of the square wave ?

Perhaps google for "Arduino sine wave generator" or "Arduino function generator" to find some code and instructions. Besides of code for generating the PWM output you also will need a RC-filter for smothing the PWM output to a sine wave.

A 50% duty cycle PWM signal is a square wave - for 40kHz you'll need to set the timer registers by hand to a suitable mode, prescaler and count.

To get a good sine wave you generally need a multipole analog filter so that the 3rd harmonic is knocked right down, which would need several opamps.

What are you going to use this 40kHz sine wave for?

Grumpy_Mike: Because square means 50% duty cycle. Most of the time a PWM wave is a rectangular wave.

Fair enough.

Grumpy_Mike: Because square means 50% duty cycle. Most of the time a PWM wave is a rectangular wave.

Never heard it described like that but it does have a certain elegance my kindergarten teacher would be proud of. :D

Thank you all for the responses.

I just don't know how to do the PWM to make a 40 KHZ square wave (50% duty cycle) out of arduino board, then pass it through a LP filter to make it sine wave ?

Sorry if it might be a stupid question, but I have not touched these electronics stuff before. :)

Dear MarkT, I want to send the sine wave to the transducer.

Simply filtering a 50% duty-cycle square will giver you a sine-ish waveform, but not a very good one. And if it's a constant 50% duty cycle, then it really isn't PWM, since you aren't doing any modulation.... So, as others have asked, exactly what is it you're trying to achieve? IF we know that, we can likely give you a better way of achieving it. What you've told us so far is more confusing than informative. Are you simply trying to create a 40kHz sine wave? If so, there are far better ways to do it than what you seem to be looking for. IF you're really trying to generate a 40 kHz sine wave actually using PWM, I very much doubt the Arduino can do it, unless that's ALL it's doing, and you're willing to put up with pretty poor quality sine wave.

Regards, Ray L.

lightt: Thank you all for the responses.

Your welcome, pity you did not read any of them, or at least respond.

Now what the hell are you trying to do? Yes I know you want a 40KHz sin wave but what for? It is likely that you do not need one at all.

You can change the PWM frequency to 40KHz but filtering a square wave is a very inefficient way to get a sin wave. This is because you have to take out frequencies as close as the third harmonic which implies a high order filter. They are complex to make and require precise components.

So lets guess here because you are not being much help. Is it to do with driving an ultrasonic transducer? If so then it is quite happy being driven with a square wave because the mechanical resonance of the device is a much better filter than you can easily make.

I just care about the final result which is a 40 KHZ sine wave.

I have 4 ultrasonic transducers and I have to send them each this sine wave, but with delays. For example, a sine wave (40KHz ) with zero delay to first and with 10 us to second transducer and with 20 us to the third and finally 40 us to the last one. That's what I want. Because I don't have 4 signal generators so I have to generate these delayed sine wave by Arduino board. (4 pins generates 4 sine waves, each pin has a certain delay or phase).

I cannot be more clear than this, If you can help!!

I cannot be more clear than this,

And I can not be more clear than this:-

Grumpy_Mike: Is it to do with driving an ultrasonic transducer? If so then it is quite happy being driven with a square wave because the mechanical resonance of the device is a much better filter than you can easily make.

Let me say it again. An ultrasonic transducer does not need to be excited by a sin wave.

Also you now spring on us, 11 posts in, the fact that you want four of these all at different phases, this renders previous answers of little use. No doubt you want the arduino to do something else as well as generate these waves, but you have not said what. You might just about get away with generating four square waves with a variable phase but you will be able to do little else with the Arduino. Generating four sin waves is right out.

Grumpy_Mike: Also you now spring on us, 11 posts in, the fact that you want four of these all at different phases, this renders previous answers of little use.

I think this is a special type of salami tactics that I can detect in several threads: The user doesn't tell the whole story, but he only starts the story with a teaser.

Then after several postings, the story gets a twist into another direction and he then changes the task for his project by adding new sensors, new logic, and adding new conditions.

I really hate that. >:(

I really hate that.

Yes it is a big waste of time, and the OP gets frustrated as well and blames us for his own mistakes.