🛠 Générer une vraie tension analogique avec les DAC 12 bits (broches 66 et 67)

Arduino Due · intermediaire · Schéma : IMG-due-intermediaire-1-dac-veritable-sortie-analogique.svg — Arduino Due, broche DAC0 (66) sortant vers l'entrée d'un oscilloscope affichant une rampe en dents de scie, puce DACC 12 bits annotée, broche DAC1 (67) disponible à côté.

Objectives

Use what almost no other classic Arduino has: TWO true 12-bit analog outputs (the DACC peripheral), on pins DAC0 = digital pin 66 and DAC1 = pin 67. No more PWM that needs filtering: here the output voltage directly follows the written value, between 0 and 3.3 V. You will generate a ramp and then a sine wave, and uncover the issue with the default resolution (8 bits when writing!).

Hardware

Complete code

// Rampe + sinusoïde sur DAC0 (broche 66) — DACC 12 bits
#include <math.h>

const uint8_t DAC0_PIN = DAC0;   // 66 (PB15). DAC1 = 67 (PB16)

void setup {
  analogWriteResolution(12);     // sans ça : écritures 8 bits (0..255) par défaut !
  SerialUSB.begin(115200);
}

void loop {
  // 1) Rampe en dents de scie 0..4095
  for (int v = 0; v <= 4095; v += 32) {
    analogWrite(DAC0_PIN, v);
    delay(2);
  }
  // 2) Sinusoïde centrée 0..4095 autour de 1,65 V
  for (int i = 0; i < 64; i++) {
    float t = i * 2.0f * PI / 64.0f;
    int v = 2048 + (int)(2000.0f * sinf(t));
    analogWrite(DAC0_PIN, v);
    delay(2);
  }
  SerialUSB.println("cycle DAC0 terminé");
}

Expected verification

On the oscilloscope (DC coupling, time base ~50 ms/div), pin 66 shows a true continuous ramp — not PWM square waves — rising from 0 to ~3.3 V, then a sine wave sampled in steps of 32/4096 ≈ 25 mV. With a multimeter in DC mode, a fixed written value (e.g., 2048) gives ~1.65 V immediately, without an RC filter.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials