🛠 Lire un capteur analogique avec l'ADC 12 bits

Arduino MKR WAN 1300 · debutant · Schéma : IMG-mkr-wan-1300-debutant-3-mkrwan-capteur-analogique.svg — potentiomètre relié à l'entrée analogique A1 et à l'alimentation 3,3 V

Objectives

Use the SAMD21 ADC (native 12-bit, configurable 8/10/12 bits, 3.3 V reference) to read an analog sensor, calibrate the measurement, and display it on the serial console.

Hardware

Complete code

// ADC 12 bits : 0..4095 pour 0..3,3 V. Broche A1.
const uint8_t CAPTEUR = A1;

void setup {
  Serial.begin(115200);
  while (!Serial) {}
  analogReadResolution(12);    // explicite : 0..4095
}

void loop {
  uint32_t brut = 0;
  for (uint8_t i = 0; i < 16; i++) {
    brut += analogRead(CAPTEUR);   // moyenne mobile
    delay(2);
  }
  brut /= 16;
  float volts = brut * 3.3f / 4095.0f;
  Serial.print("brut="); Serial.print(brut);
  Serial.print(" V="); Serial.println(volts, 3);
  delay(500);
}

Expected verification

When turning the potentiometer (wiper towards 3.3 V, the other two pins towards GND and 3.3 V), the raw value follows 0..4095 and the voltage 0.000..3.300 V, with only a few units of residual noise thanks to averaging.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials