# Sine wave arduino

#### Tiago NET

Joined Oct 27, 2015
3
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 and google doesn't help me!

#### Attachments

• 13.4 KB Views: 16

#### MikeML

Joined Oct 2, 2009
5,444
It will be a crude approximation of a sine wave. It will be tough to get exactly 60Hz

An external low-pass filter tied to a pin controlled by analogWrite().
60Hz has a period of 16.6ms. You could update the PWM duty cycle 17 times per period using the millis() function as a time base. The 17 pwm values can be determined apriori and stored as constants.

#### MrAl

Joined Jun 17, 2014
11,716
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 and google doesn't help me!
Hi,

60Hz is not very high frequency at all, and because it is a constant frequency, this should be doable.

There are several possibilities...

You could use pure bit banged PWM using direct pin access. You would have to precalculate all the pulse widths using say the "sin()" function. The output could be well filtered because the frequency is constant. Do the same for the 2nd pin, except change the phase relationship in the code.

You could also create an N bit 1R2R binary ladder using resistors and feed it with either one port (8 bit amplitude resolution) or two ports (16 bit resolution but requires more resistors). That would get a faster speed, but you probably dont need that.

If you have the option, you could get a digital to analog converter (DAC) for whatever bits you deem necessary, and feed that with one or two direct write ports. Either use a second one or use an op amp to invert the signal as 180 degrees is a simple invert which is a multiplication by -1.

The PWM method requires the least amount of hardware.

#### Wendy

Joined Mar 24, 2008
23,476
Greetings! And welcome to AAC!

The Projects Collection (as opposed to The Projects Forum) is for sharing completed projects.

I've moved your post into The Projects Forum, where it will draw more responses.

#### GopherT

Joined Nov 23, 2012
8,009
The arduino code is a bit bloated so getting 100 update per cycle will be difficult. You may have to settle for 32 to 64 or so per cycle. It all depends on which commands you use and how you implement the software. Floating point math takes forever on arduino (as expected). Certain loop functions can take a lot of time as well.

You might just want to write out all 16 updates you want per cycle in sequence and then restart the loop.

#### ScottWang

Joined Aug 23, 2012
7,414
For a eeprom and dac to converting binary to sine wave, the frequencies similar as below:
1. 60Hz, 64 dots for a cycle, F=60Hz x64=3840Hz.
2. 60Hz, 128 dots for a cycle, F=60Hz x128=7680Hz.
3. 60Hz, 256 dots for a cycle, F=60Hz x256=15360Hz.
The frequencies as above are more pure, if you using arduino then you have to adding the program execution time, why you need to using the arduino to do this?