🛠 Lire un capteur analogique et générer une tension avec le DAC

Arduino MKR WiFi 1010 · debutant · Schéma : IMG-mkr-wifi-1010-debutant-3-mkrwifi-capteur-analogique-dac.svg — Potentiomètre alimenté en 3,3 V relié à l'entrée A1 (ADC 12 bits) et sortie DAC A0 (10 bits) vers un multimètre, sur la MKR WiFi 1010.

Objectives

Use the real analog-to-digital converter (7 inputs, configurable resolution 8/10/12 bits) and the real 10-bit DAC analog output on A0 — a true luxury absent from the Arduino Uno. We read a potentiometer on A1 and generate a continuous voltage or a ramp on A0.

Hardware

Complete code

// ADC sur A1 (12 bits), DAC sur A0 (10 bits).
const uint8_t PIN_ANA = A1;
const uint8_t PIN_DAC = A0;

void setup {
  Serial.begin(115200);
  analogReadResolution(12);   // ADC: 8/10/12 bits configurables
  analogWriteResolution(10);  // DAC: 10 bits
  pinMode(PIN_DAC, OUTPUT);   // A0 = sortie analogique vraie (DAC)
}

void loop {
  int raw = analogRead(PIN_ANA);
  float volts = raw * 3.3f / 4095.0f;
  analogWrite(PIN_DAC, map(raw, 0, 4095, 0, 1023)); // miroir 10 bits
  Serial.printf("raw=%u (%.2f V) -> DAC %u\n", raw, volts,
                map(raw, 0, 4095, 0, 1023));
  delay(100);
}

Expected verification

When turning the potentiometer, the raw value follows 0–4095 (12-bit resolution) and the calculated voltage 0–3.3 V. The multimeter connected to A0 displays the same voltage as the wiper: the DAC produces a true smooth voltage, not a filtered PWM chopped signal. Serial.printf works natively on SAMD.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials