I want to produce Digital Sine wave send it to DAC0 and then receive it back as input on A0 and plot the graph. I am using Arduino Due. I wrote the following code:

#define maxSamplesNum 120
#define Sample 50000000/maxSamplesNum  // sample for the 50Hz signal expressed in microseconds

static int Table[maxSamplesNum] =                        // lookup table  for sine wave table

    0x7ff, 0x86a, 0x8d5, 0x93f, 0x9a9, 0xa11, 0xa78, 0xadd, 0xb40, 0xba1,
    0xbff, 0xc5a, 0xcb2, 0xd08, 0xd59, 0xda7, 0xdf1, 0xe36, 0xe77, 0xeb4,
    0xeec, 0xf1f, 0xf4d, 0xf77, 0xf9a, 0xfb9, 0xfd2, 0xfe5, 0xff3, 0xffc,
    0xfff, 0xffc, 0xff3, 0xfe5, 0xfd2, 0xfb9, 0xf9a, 0xf77, 0xf4d, 0xf1f,
    0xeec, 0xeb4, 0xe77, 0xe36, 0xdf1, 0xda7, 0xd59, 0xd08, 0xcb2, 0xc5a,
    0xbff, 0xba1, 0xb40, 0xadd, 0xa78, 0xa11, 0x9a9, 0x93f, 0x8d5, 0x86a,
    0x7ff, 0x794, 0x729, 0x6bf, 0x655, 0x5ed, 0x586, 0x521, 0x4be, 0x45d,
    0x3ff, 0x3a4, 0x34c, 0x2f6, 0x2a5, 0x257, 0x20d, 0x1c8, 0x187, 0x14a,
    0x112, 0xdf, 0xb1, 0x87, 0x64, 0x45, 0x2c, 0x19, 0xb, 0x2,
    0x0, 0x2, 0xb, 0x19, 0x2c, 0x45, 0x64, 0x87, 0xb1, 0xdf,
    0x112, 0x14a, 0x187, 0x1c8, 0x20d, 0x257, 0x2a5, 0x2f6, 0x34c, 0x3a4,
    0x3ff, 0x45d, 0x4be, 0x521, 0x586, 0x5ed, 0x655, 0x6bf, 0x729, 0x794

int i = 0;
int sample;
int sensorPin = A0;
int sensorValue = 0;

void setup() {
  analogWriteResolution(12);  // set the analog output resolution to 12 bit (4096 levels)
  analogReadResolution(12);   // set the analog input resolution to 12 bit


void loop() {

  sample = 50;

  analogWrite(DAC0, Table[i]);  // write the selected waveform on DAC0

  if(i == maxSamplesNum)  // Reset the counter to repeat the wave
    i = 0;

  delayMicroseconds(sample);  // Hold the sample value for the sample time
  sensorValue = analogRead(sensorPin);

I want to create 3.3V (P-P) sine wave but I don't how to set the amplitude in this wave.

Also I would like to redirect the sine wave output from DAC0 to A0 Pin and plot the graph to check if the sine wave generated was successful. How do I plot the graph ?

Update : I uploaded the program but there was no result. I tried to read data from A0 but there wasn't any output. What am I doing wrong ?

  • 1
    You just have to Serial.print() the ADC reading, either immediately (which will slow your signal generation), or after the fact (buffer the values, then print them all). Jun 9 '17 at 9:54
  • Possible duplicate of No Voltage output on DAC0 Jun 9 '17 at 11:21

I ran your code unmodified and plugged in an oscilloscope to look at DAC0 (looks an OK sine wave) and A0 (looks flatline). Hope this helps.

A0 Screenshot DAC0 Sceenshot

  • Further to @Edgar's suggestion there is more information about the Arduino IDE [Serial Plotter] [arduinogetstarted.com/tutorials/…. Which I think resolves the "How do I plot the graph" part of your question. The oscilloscope test shows that your wave generation is working OK. If you would like further help about a loopback on A0, I suggest you show your wiring. Regards.
    – RowanP
    Jan 10 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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