Is there a way to output a sinusoidal wave with an arduino? Perhaps using PWM? It would be nice to be able to simulate something around 10kHz. Is this even possible with the hardware on the Uno?

up vote 6 down vote accepted

Look for DDS (Digital Direct Synthesis) which uses a low pass filter as mentioned in the other answers:

low pass filter

It then uses a varying PWM signal to create a sine wave:

scope trace

All you need in order to implement it with an Arduino, including the source code for the PAM generator, can be found in this article.

  • right on, they claim to be able to generate up to 16kHz sine waves... not too shabby – vicatcu Sep 20 '12 at 4:09

Yes, you use a low pass filter combined with PWM. The lower the duty cycle the lower the value of your output. So you modulate your duty cycle like a sinusoid and pass the PWM output through a LPF. The output of the LPF will then be a sinusoidal waveform, though it will have a DC offset.

Not quite an Arduino, but I remember a specific lab exercise that required me to do this in my university microcontrollers course using an AT90S8515 - and as I recall, it required some assembly programming to get the frequency into the tens of kHz... in fact here's the lab description.

Other than PWM + Low Pass Filtering, another way is to use an R-2R ladder DAC, and drive the output with a PORT. But you're not going to get much frequency out of it using the digitalWrite function. Like I said, at a minimum you'll need to do direct port assignments.

  • digitalWrite() won't provide the frequency for the R-2R ladder? Is it capable of doing so for PWM + low pass filtering method, or is there a limit on the frequency digitalWrite can operate on? – Fulluphigh Sep 20 '12 at 3:06
  • digitalWrite() is very very slow... and is a well-documented "issue" with the Arduino core. There's been a lot of discussion surrounding it on the developers mailing list of late. It's unsuitability for the R-2R ladder is that you would have to set each pin with a separate call instead of changing the R-2R ladder's control points all at once. You could use it to do "soft" PWM, but again, at very slow speeds as compared to the hardware PWM (i.e. using the hardware timers as is done in the analogWrite() function) – vicatcu Sep 20 '12 at 4:06
  • And the analogWrite function is itself only at 490 Hz, right? So that means neither are a valid option for the speeds we need... (Or am I mistaken?) – Fulluphigh Sep 20 '12 at 4:11

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.