So You Want To Make A Sine Wave In Arduino

So a bunch of blog posts back I posted how easy it was to get a square wave out of an arduino to do things with like generate your 600hz tone for CW etc. But we can go 1 further than that and use the arduino to generate sine waves. Of course you can filter the crap out of a 600hz square wave and make a sine, but to be honest, who really wants to wind inductors in the mH range or for that matter build a suitable LC filter to cut out the harmonics sufficiently to make a sine wave. But alas poor Yorick, there is a way and its called PWM.

 

So above we have a 31khz pwm signal that is being used to generate a sine wave. Through the wonders of mathematics and other nerd endeavours that PWM signal can be used to make sine waves, in my case a 600hz sine wave. Now obviously you still need to filter off the crap to see the sine wave, but its there and below is the filtered output. Now the good thing is you can filter with RC filters and make life simple. In my case i am using a 2 pole RC filter using 100nf caps and 2K resistors, and as you can see it looks rather sinusoidal.

So the following software is not mine, its something i found on the net and did some simple modifications to make work with an arduino UNO. Output is from PIN 11. Take a read, you should be able to work out how it works, using interrupts and timers to get some semblance of accuracy. Anyway, enjoy and have fun.

So I have cleaned up the original code to make it easier to read and to remove the stuff in there that was really doing nothing. And here is a link back to the original article. http://interface.khm.de/index.php/lab/interfaces-advanced/arduino-dds-sinewave-generator/

Facebooktwittergoogle_plusredditpinterestlinkedin

Leave a Reply

Your email address will not be published. Required fields are marked *