Sine wave

Hello my friends,

I need to generate two sine waves at 60Hz and the second wave will be 180 degrees out of phase using arduino with atmega2560! Does anyone have any code because I have no idea where to start!
In the attachment, the waves were generated in 12V 60Hz for sampling in oscilloscope using Proteus Isis.

sine wave.png

Do they have to be nice pretty sine waves like that, or can they be a little more square-wavish that you filter with RC Low pass filter and be more or less sine-wavish?

My friend thanks for answer! But i need pure sine wave!

Okay, you can make an array of PWM values that are the sine wave values over time, and output them with PWM thru an RC lowpass filter (this was with 4.7K and 10uF) to make cleaner version of this. Do it with 2 PWM outputs with appropriate values for the opposing waves.
This was just stepping the PWM output but 25 every so many mS as a test with analogWrite, so limited to 256 steps of resolution. Try that as a starting point.

If you are in the U.S., why not use a transformer dropping the mains voltage to 5 v. or 12 v., as needed. That gives you one very accurate sine wave. You would want to use a line filter, readily available, on the mains side of the transformer to filter out spikes. The other sine wave you get with a simple transistor or opamp amplifier.

A sine wave from an analog out won’t be a pure sine wave - it will be pulse width modulated. On top of that, the frequency will be off because the arduino uses a resonator rather than a crystal (although this depends). If you need a real sine wave with an accurate frequency, then you’ll need some electronics.

for (float theta = 0.0; theta < 360.0; theta++) {
    somehowOutput(sin(theta));
    somehowOutput(sin(theta+180.0));
}

That’s pretty wrong, since sin() takes an argument in radians, and you have to figure out “somehowOutput()”, which is pretty non-trivial given the hardware setup of a MEGA, and it’s also not a particularly FAST way to do it, but… that would be the basic idea…

tiagomega: Hello my friends,

I need to generate two sine waves at 60Hz and the second wave will be 180 degrees out of phase using arduino with atmega2560! Does anyone have any code because I have no idea where to start! In the attachment, the waves were generated in 12V 60Hz for sampling in oscilloscope using Proteus Isis.

How pure is "pure"?

For instance, there is no such thing as a perfect sphere. But many of them are "good enough".

You could make an R-2R ladder DAC and pipe in calculated sine values from 0 to 255 into it. At 5 volts, that gives you about 20 millivolts per step (that is, if you looked at the sine wave on a scope, it would be composed of small, but discrete steps as opposed to a "pure" sine wave coming from a rotating device or a signal generator.

If you use the Arduino PWM and filter it, I doubt you will be able to change the PWM rate fast enough to get a sine output.

As far as 180 degrees out of phase... all you need is a unity gain inverting op-amp (if you don't want to bother building two DAC's).