dac-analogique.ino

Arduino UNO R4 Minima · File : dac-analogique.ino · Language : Arduino · Use case : Générer une tension continue avec le DAC 12 bits sur A0

The R4 carries a true 12-bit DAC on A0: it outputs a clean continuous voltage, unlike filtered PWM on AVRs. This script generates a sinusoidal ramp steerable on the fly from the serial monitor.

Hardware

An Arduino UNO R4 Minima (Renesas RA4M1, Cortex-M4 @ 14 MHz, 5 V, 12-bit DAC, no radio) powered by USB or 6-24 V. A multimeter to verify, or an analog input to drive (0-3.3 V).

Wiring

A0 to the input to drive; common grounds. 3.3 V full-scale output, low output current (buffer it to drive a load).

How it works

analogWriteDAC0() writes straight into the RA4M1 integrated digital-to-analog converter; the output voltage follows the value in real time, with no PWM ripple.

Running it

Arduino IDE 2.x (with the « Arduino UNO R4 Boards » core installed), pick « Arduino UNO R4 Minima », select the port and Upload. The serial monitor runs at 9600.

Variants

Generate waveforms (triangle, sawtooth) for a synth, or drive a motor setpoint through an amplifier stage.

dac-analogique.ino

// DAC 12 bits integration du R4 : tension continue sur A0.
// analogWriteDAC0(valeur) : 0..4095 pour 0..3,3 V.
// A verifier a l'ohmmetre/metre : A0 -> metre, GND commun.

void setup() {
  Serial.begin(9600);
  Serial.println(F("DAC0 sur A0 : 0..4095 -> 0..3,3 V"));
  Serial.println(F("Envoyez une valeur, ou 'r' pour un rampe."));
}

void loop() {
  static float phase = 0;

  // Petite rampe triangulaire 0..3,3 V visible au metre.
  phase += 0.02;
  float x = 0.5 + 0.5 * sin(phase);
  int valeur = (int)(x * 4095);
  analogWriteDAC0(valeur);

  static unsigned long t0 = 0;
  if (millis() - t0 >= 500) {
    t0 = millis();
    Serial.printf("DAC=%d  U=%.3f V\n", valeur, valeur * 3.3 / 4095.0);
  }

  // Commande serie : 's' suit une consigne saisie.
  if (Serial.available()) {
    int consigne = Serial.parseInt();
    if (consigne >= 0 && consigne <= 4095) {
      analogWriteDAC0(consigne);
      Serial.printf("force a %d (%.3f V)\n", consigne, consigne * 3.3 / 4095.0);
    }
  }
}

← Back to the board