# Arduino and wave generation

I'm trying to generate a combination of 2 waves with different frequencies. For example 1Khz and 5Khz - so I could recognize it on my PC using FFT. It seems that feat is not possible using arduino.

Any one succeeded? is it possible with any other way? physically possible?

Thanks!

• are you summing the waves in an op-amp or software? – kenny Jul 19 '11 at 12:33
• Software only - from the arduino to a speaker (standard 8ohm) – roman Jul 19 '11 at 12:37
• The other day we generated a 6kHz wave table for an experiment on a 8 bit PIC, so you might be past the limit of that Atmel on the board. – kenny Jul 19 '11 at 12:39

If 5kHz is the highest frequency you need, then according to Nyquist (some say Shannon) you need at least a 10kHz sample frequency. That's a sample periode of 100$\mu$s, which should be doable for any modern microcontroller. You'll have to make lookup tables for your waveforms. Everything depends on the frequencies you want (for example, only 1kHz and multiples?), and the waveform (square wave, sine?). Keep in mind that for the Nyquist sampling frequency you also have to take harmonics into account. So you can reconstruct a 5kHz sine when sampled at 10kHz, because it doesn't have any higher harmonics, but a 5kHz square wave needs quite a few harmonics to get a decent approximation of the wave form, so that 10kHz will be insufficient.
You'll also want an analog low-pass filter on the output signal, to filter out the sampling.

• Wow. Lots of stuff going there :) Thanks for the great info. Gonna dig into it right away... – roman Jul 19 '11 at 12:55
• I wonder whether summing two Nyquist frequency sample wave tables in software would be accurate enough? But that's just my paranoia seeping in... It seems to me that you would be introduce a lot of different and higher frequencies, but I"m pretty ignorant on that. – kenny Jul 19 '11 at 13:22
• @Kenny - No, you're right. As I tried to explain the 10kHz is by no means sufficient for other wave forms than sines at 5kHz. It may be necessary to sample at, say, 100kHz, and I have no experience with Arduino, so I don't know what it's capable of. – stevenvh Jul 19 '11 at 13:28
• Guys, I don't really need 100% accuracy - I have for example 10 frequencies I need to be able to recognize. I'm almost not constrained on the decoding side (say it's a PC) - only on the arduino side. As I'm researching it now - it seems that I could "engineer" the square wave that the arduino will produce so that using the Nyquist theorem I could sample it and decode the original frequencies. Making sense? – roman Jul 19 '11 at 13:32
• @roman - If they're square waves, can't you use the timers of your controller to generate them? You can attain much higher frequencies than when you do it in software. You'll have to add them externally though. A couple of resistors will do. – stevenvh Jul 19 '11 at 13:37

If you just want to test fft just use a square wave the frequency an amplitude of the components are well know, and the waves are easy to generate.

I recommend using a lookup table that maps to values between 0 and 2$^N$ which correspond to the inputs on an R-2R DAC. The larger you make $N$, the more precise your waveform will be but the more i/o pins you will have to use. This is called the resolution of a DAC.

Then use the timers library for arduino to write a little function which periodically changes the output pins which are the inputs to the DAC you build.

Someone has already done something similar here. The only thing you would have to add is the timer interrupt, and of course calculation of your wave tables.

• Went through the details - seems like a great resource! many thanks :) – roman Jul 21 '11 at 7:24

I don't know aurdino but this is fairly easy to do on the processors I work with.. The answer is the timers.. You can program your GPIO to toggle at a specific frequency using timers.. On the arm processors we use, I do it all the time. The problem is that the GPIOs will provide a square clock, if you need sine wave etc, you need additional hardware.

What you can also do is that you can get two timers using GPIO and use a simple or gate to combine the clocks.

You didn't ask but if you want them to be in some sort of phase with each other, you have no choice but to use advanced timer hardware as with software getting exact phase lock would be challenging.