# How can I have 2 differents frequency on the 2 DAC outputs on an Arduino Due?

I would like to blink, using specific waveforms, 2 LED controlled by the 2 DAC output of the Arduino Due.

I wrote this :

``````    #include "Waveforms.h"

int i = 0;
int sample;

void setup()
{
analogWriteResolution(12);
}

void test(float fq, int wave1, int wave2)
{

sample = ((1/fq)*1000000)/120;

analogWrite(DAC0, waveformsTable[wave1][i]);
analogWrite(DAC1, waveformsTable[wave2][i]);

i++;

if(i == maxSamplesNum)
{
i = 0;
}

delayMicroseconds(sample);

}

void loop() {
// 10 Hz, 0 is a sine wave on DAC0, 3 is a square wave on DAC1

test(10, 0, 3);
``````

I would like to be able to change the frequency of each output (1Hz - 100 Hz) using differents waveforms. Changing waveforms is ok and it works, but is it possible to also change the frequency so to have two differents output frequency on each DAC output ?

The goal is to have, for example, simultaneously :

• 1 Hz, sine wave on DAC0.
• 10Hz, square wave on DAC1.

Thanks a lot.

• Properly format the code. It's hard to read. Nov 24, 2017 at 13:06

I don't have experience with this, but what you can do is:

• If the frequency of DAC1 is x times faster than DAC2 to calculate the delay time based on the fastest and for DAC1 changing in every loop, while changing DAC2 only every DAC1/DAC2 times.

• Vice versa if DAC2 is x times faster than DAC1

• However, if there is not a simple ratio between DAC1 and DAC2 than you have to use the fastest speed of the two and use for the slowest an approximation...

• Or use a higher significant fixed speed for i use a simple linear approximation between two points of the wavetable.

E.g. assume the speed is 10 times too fast for DAC1. Suppose i = 100.. you should use the following points in the waveform: wave[100/10] and wave[100/10 + 1], so two consecutive points. Assume the first is 10.0 and the second is 20.0... for the 10 increases of i (because i is running 10 times too fast) you can calculate for values of

`````` i     : 10.0 +   0% of (20.0 - 10.0)
i + 1 : 10.0 +  10% of (20.0 - 10.0)
...
i + 10: 10.0 + 100% of (20.0 - 10.0)
``````

etc.

Note this is called linear interpolation.

You would have to check how many numbers per wave form table you need to get good values for different frequencies/speeds and what 'fixed' frequency you need to use for the delay.