🛠 Lire un capteur analogique et générer une tension avec le DAC
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
- 1 × Arduino MKR WiFi 1010
- 1 × 10 kΩ potentiometer (pins connected to 3.3 V, A1, GND)
- 1 × multimeter on A0 (optional but illustrative)
- Reminder: sensor power supply at 3.3 V maximum, the board is not 5 V tolerant
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
- Confusing
analogWriteResolutionandanalogReadResolution: they are distinct, the DAC is 10 bits, the ADC is configurable up to 12. - Forgetting
pinMode(PIN_DAC, OUTPUT)before using A0 as an output: no voltage comes out. - Powering the sensor at 5 V from the "5V" pin (unregulated pass-through) and exceeding the full scale of the ADC referenced to 3.3 V.
- Expecting exactly 0 or 4095 at the minimum/maximum of the potentiometer: the real rail saturates slightly before, this is normal.
Other tutorials for this board
Code examples for this board
scan-wifi.inoArduinoconnexion-wpa2.inoArduinohttp-get.inoArduinoserveur-web-led.inoArduinohorloge-ntp.inoArduinoudp-diffusion.inoArduino