Sine wave arduino

Discussion in 'Embedded Systems and Microcontrollers' started by Tiago NET, Oct 27, 2015.

  1. Tiago NET

    Thread Starter New Member

    Oct 27, 2015
    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!
  2. MikeML

    AAC Fanatic!

    Oct 2, 2009
    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.
  3. MrAl

    Well-Known Member

    Jun 17, 2014

    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.
  4. Wendy


    Mar 24, 2008
    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.

    Good luck with your project!
  5. GopherT

    AAC Fanatic!

    Nov 23, 2012
    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.
  6. ScottWang


    Aug 23, 2012
    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?