🛠 Lire un capteur analogique (potentiomètre)

Arduino Nano (Classic, ATmega328P) · debutant · Schéma : IMG-nano-classic-debutant-3-nanoc-capteur-analogique.svg — potentiomètre 10 kΩ en diviseur de tension sur A0, Arduino Nano classique, affichage série de la valeur 0-1023

Objectives

Discover the 10-bit ADC of the ATmega328P: the classic Nano has 8 analog inputs (A0-A7, with A6/A7 specific to the Nano form factor) that convert 0-5 V into values from 0-1023. We wire a potentiometer as a voltage divider on A0 and display the value on the Serial Monitor and Serial Plotter.

Hardware

Full code

// Lecture analogique 10 bits sur A0
const uint8_t CAPTEUR = A0;
uint32_t derniere = 0;

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

void loop {
  if (millis - derniere >= 50) {          // 20 affichages/s
    derniere = millis;
    int brut = analogRead(CAPTEUR);         // 0..1023
    float tension = brut * (5.0 / 1023.0);  // conversion en volts
    Serial.print(brut);
    Serial.print(F(" -> "));
    Serial.print(tension);
    Serial.println(F(" V"));
  }
}

Expected verification

When turning the potentiometer, the raw value follows 0 (wiper towards ground) to 1023 (wiper towards 5 V) and the displayed voltage follows 0.00-5.00 V. The Serial Plotter draws a continuous line. Values at rest are stable within ±1, proving good ground contact.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials