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