Go Down

Topic: Creating a sinewave with an Arduino Duemilanove (Read 3815 times) previous topic - next topic


hi guys,

iam a bit confused that is because i ask this question.
for the university i should build a sine wave generator using an arduino board and pulse width modulation.
so i know what pwm is and i know what a sine wave should look like  :)
the question is do i need a table to store the sine wave or is there another possibilty to generate sine values on demand?
and if i use a table is it the same as direct digital synthesis?
do i have to live with the disadvantage of dds when i want to speed up the frequency that the tone gets ugly, beacause iam missing a lot sine values?
is this a problem? is it recognizeable?
i hope you know what i mean.
i want to control the frequency of the sine wave without getting a bad audible result by leaving out multiple sine values.



Sep 14, 2010, 02:30 am Last Edit: Sep 14, 2010, 02:40 am by mpeuser Reason: 1
You will always leave out a terrible amount of sine values!
What is more, you will always have a bad amplitude discretisation using PWM.
The worst thing of all is, that this is really DDS, which seems to disturb you for some reason!?

Before any of your questions can be answered in a sensible way, please state the required frequency range and acceptable distortion. You mention Audio... There is something like e.g. the HiFi standard....

I should also recommend you just read a little bit about DDS....


the question is do i need a table to store the sine wave or is there another possibilty to generate sine values on demand?

You can generate a sinewave using a digital resonator structure (a.k.a. IIR oscillator, IIR resonator). If you're not familiar with DSP, then perhaps stay away :)  If you are, I am talking about a two-pole (complex conjugate) system with poles right on the unit circle so it is marginally stable. Kick it and it oscillates. The angle of the poles determines the oscillation frequency and because it's second order the implementation equations are pretty simple.

You could generate sinusoid values on-the-fly using Taylor series and/or CORDIC algorithms.

If you do stick with the simpler lookup table, at high frequencies you can improve the "ugly" factor by interpolating between adjacent values in the table. Linear interpolation is pretty good, quadratic will be even better.

The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons


Yeah, DSS is the way to go if you wanted to create sinewave. Take a look at this post (http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/) to see how it is done.


Sep 14, 2010, 03:29 am Last Edit: Sep 14, 2010, 03:30 am by mpeuser Reason: 1
Direct Digital Signal Synthesis is generally shortcut to DDS; but I do hate three letter acronyms and also get confused from time to time..  DDSS would have been much better..

P.S. I do like the "flying filter" in the picture


thanks for your answers.
i read a bit more about dds and i altough wnat to understand the program which is posted under http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/ (thanks for that, saw it before, but didn't really pay attention to that).
the only problem i have is why is the interupt used to increment the accumulator so the next value out of the sine table can be loaded. isnt it possible to just increment in the while loop?
i didnt get the timing thing right...



is why is the interupt used to increment the accumulator

So that the increment is timed to the sample rate. If you did it in the loop it wouldn't be synchronised.


ok now i have a working arduino sketch and build the circuit on a breadboard.
now i want to control the frequency with a potentiometer.

there is a lin in the code like this dfreq=analogRead(0); // reading on analog pin 0

so my question is, the value is written every second, but i want to have a continuous frequency control, so i got rid of these lines

if(icnt1++ == 10) {


and incremented c4ms++ every time the interupt gets called, instaed of increment it every 4 milliseconds.
but the output sounds strange.

http://webuser.uni-weimar.de/~atix5565/arduino/test_sound.wav (dont watch the first 2 seconds)

there are these interupt sound.
how can i get rid of them?

thanks for your answers.
greetings peter.


Thake a look here:


how can i get rid of them?

Same way you caused them, don't read the analogue port so often.

There is too much going on for the processor to cope with. I assume that is why the original coder scaled back the frequency of analogue reads.

You could find another way of reading the pot like replacing it with a rotary encoder and only reading it when it changes. Or feeding the pitch control information in from a PC or other arduino.

Go Up