Having trouble using Xiao's DAC

I want to use the DAC in a project but I am just not able to make it work correctly. The maximum output voltage of the DAC is somehow limited to 2.2V only. When running the DAC demonstration code from Seeed’s website I get the full voltage swing but when I run my sketch I only get up to 2.2V. The problem is that both the sketches are more or less the same.
I have tried running analogWrite() in a loop and with some delay before sampling but to no avail.
What am I missing?

My code is:

#include <Arduino.h>

void setup() {

  SerialUSB.begin(9600);

  while(!SerialUSB);

  pinMode(A1, INPUT);

  pinMode(A0, OUTPUT);

  analogReadResolution(10);

  analogWriteResolution(10);

  while(1){

    analogWrite(A0, pow((double) 2, 32) - 1);

    delay(1000);

    float y = analogRead(A1) * 3.3 / 1023L;

    SerialUSB.print("DAC Output Voltage: ");

    SerialUSB.println(y);

    delay(1000);

  }

}

void loop() {

  // put your main code here, to run repeatedly:

}

The following code works.

#include <Arduino.h>

void setup() {
  SerialUSB.begin(9600);
  while(!SerialUSB);

  pinMode(A1, INPUT);
//  pinMode(A0, OUTPUT);            // DAC port is not specified as OUTPUT
  analogReadResolution(10);
  analogWriteResolution(10);

  while(1){
//    analogWrite(A0, pow((double) 2, 32) - 1);
    analogWrite(A0, 1023);           // max 1023
    delay(1000);
    float y = analogRead(A1) * 3.3 / 1023L;
    SerialUSB.print("DAC Output Voltage: ");
    SerialUSB.println(y);
    delay(1000);
  }
}

void loop() {
}
1 Like

Thank you for replying. May I ask why the DAC pin should not be specified as an output?
Also in the last I was just trying everything to see if that will change anything so that’s why I gave analogWrite the max value.

See link below.

Thanks for the reply. Such a silly mistake just when I was getting confident in my Arduino skills.