🛠 Lire un capteur analogique (potentiomètre)

Arduino Pro Mini · debutant · Schéma : IMG-pro-mini-debutant-3-promini-capteur-analogique.svg — potentiomètre 10 kΩ en diviseur de tension sur A0 de l'Arduino Pro Mini, affichage série de la valeur 0-1023 et de la tension relative à VCC

Objectives

Discover the 10-bit ADC of the ATmega328P on the Pro Mini: the board exposes inputs A0-A5 on the pin header, and the latest SparkFun revision even breaks out the additional ADC6/ADC7 channels (beyond the 6 channels of the Uno). A crucial point specific to the Pro Mini: the ADC reference is VCC, so 5 V on the 5 V/16 MHz model but 3.3 V on the 3.3 V/8 MHz model — the conversion to volts depends on your board. We wire a potentiometer as a voltage divider on A0 and display the raw value and millivolts.

Hardware

Complete Code

// Lecture analogique 10 bits sur A0 — pleine echelle = VCC
const uint8_t CAPTEUR = A0;
const uint32_t FULLSCALE_MV = 5000;  // 3300 pour le modele 3,3 V/8 MHz
uint32_t derniere = 0;

void setup {
  Serial.begin(57600);         // A0 en entrée implicite pour analogRead
}

void loop {
  if (millis - derniere >= 50) {          // 20 affichages/s
    derniere = millis;
    int brut = analogRead(CAPTEUR);         // 0..1023
    uint32_t mv = (uint32_t)brut * FULLSCALE_MV / 1024UL;
    Serial.print(brut);
    Serial.print(F(" -> "));
    Serial.print(mv);
    Serial.println(F(" mV"));
  }
}

Expected Verification

Turning the potentiometer, the raw value follows 0 (wiper towards ground) to 1023 (wiper towards VCC) and the millivolts follow 0-VCC. The serial plotter draws a continuous line. Values at rest are stable within ±1, proof of a good ground contact. On the 3.3 V model, set FULLSCALE_MV to 3300: otherwise the displayed voltage is wrong by +50 %.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials