Sine Wave.

I want to generate a DC sine wave and this is the code I have so far. However i’m quite confused because I don’t think this will work. Does anyone know how to improve the code so that the Arduino can produce a pure sine wave.

SineWaveGenerator2.ino (473 Bytes)

However i'm quite confused because I don't think this will work.

You are right. It won't.

Your code was 21 lines. That could easily have been pasted in a code window, and not require downloading.

Think about what value sineWave will have on each pass through loop. Then, think about what that value is going to do when you send it to a function that generates square waves.

Does anyone know how to improve the code so that the Arduino can produce a pure sine wave.

The Arduino is not a signal generator. It will not produce a pure sine wave. You are barking up the wrong tree.

I think the Arduino analog pins produce a PWM output (see Pulse-width modulation - Wikipedia) if PWM is new to you.

I'm not a circuits person, but it seems to me that you could use square/rectangular wave output into a tank circuit (see LC circuit - Wikipedia), but I'm not sure how well that approach would work if you want to vary the frequency.

I want to generate a DC sine wave

That is what I would call a self-canceling phrase. DC is well direct current at some specific voltage, where as a sine wave is an AC voltage of some specific frequency and voltage. Perhaps you mean something other then just a vanilla sine-wave generator?

As others have said you will need some kind of external components and/or modules to generate a sine wave under programmable control, however which might be the best method kind of depends on the specific requirements you need.

  1. What frequency or range of frequencies do you require?
  2. What voltage or range of voltages do you require of the generated sine-wave?
  3. How much current must the sine-wave signal be able to provide?

To me the cadillac of sine-wave generation methods using an arduino is with a DDS module. They have come down to a pretty affordable price and offer very good frequency range and stability. I use one of the below DDS modules as a VFO (variable frequency oscillator) for a ham radio tranceiver project I'm working on.