🛠 Lire un capteur analogique (potentiomètre)

Arduino MKR WAN 1310 · debutant · Schéma : IMG-mkr-wan-1310-debutant-3-mkrwan1310-capteur-analogique.svg — Potentiomètre alimenté en 3,3 V, curseur relié à A0 d'une MKR WAN 1310, valeurs affichées sur le traceur série.

Objectives

Read an analog sensor (potentiometer) on the SAMD21G18A ADC: the MKR WAN 1310 has 7 analog inputs configurable at 8, 10, or 12 bits, and a 10-bit DAC output. You will learn how to change the read resolution and plot the curve in the Serial Plotter.

Hardware

Complete code

// Lecture analogique 12 bits sur A0 — MKR WAN 1310
const int CAPTEUR = A0;

void setup {
  Serial.begin(115200);
  while (!Serial);
  analogReadResolution(12);      // 0..4095 au lieu de 0..1023 par défaut
  analogRead(A0);                // première lecture à jeter
  Serial.println("Tourne le potentiometre");
}

void loop {
  int brut = analogRead(CAPTEUR);            // 0..4095
  float tension = brut * 3.3f / 4095.0f;     // tension réelle sur A0
  Serial.print("brut=");
  Serial.print(brut);
  Serial.print("\tU=");
  Serial.print(tension, 2);
  Serial.println(" V");
  delay(50);
}

Expected verification

Turning the potentiometer, the raw value follows 0 → 4095 and the calculated voltage follows 0 → 3.3 V. The Serial Plotter displays a continuous curve; the values match multimeter measurements.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials