🛠 Lire un capteur analogique sur A0-A7

Arduino Nano Every · debutant · Schéma : IMG-nano-every-debutant-3-nanoevery-capteur-analogique.svg — Potentiomètre relié au 5 V, à la broche A0 et à GND sur une Nano Every, avec l'échelle ADC 10 bits (0-1023) annotée

Objectives

Read an analog voltage using the ATmega4809's 10-bit ADC. The Nano Every exposes 8 analog inputs A0 to A7 on its connector (the chip supports 14 channels in total), converted between 0 and the reference voltage (5 V by default).

Hardware

Complete code

// Lecture du potentiometre sur A0 + allumage seuil sur LED 13
const uint8_t CAPTEUR = A0;
const uint16_t SEUIL = 512;
uint32_t derniereImpression = 0;

void setup {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}

void loop {
  uint16_t brut = analogRead(CAPTEUR);          // 0..1023
  float tension = brut * (5.0 / 1023.0);        // 0.00..5.00 V

  if (millis - derniereImpression >= 100) {   // 10 Hz
    derniereImpression = millis;
    Serial.print("brut="); Serial.print(brut);
    Serial.print("  U="); Serial.print(tension);
    Serial.println(" V");
  }
  digitalWrite(LED_BUILTIN, brut > SEUIL ? HIGH : LOW);
}

Expected verification

Potentiometer: cursor towards GND → reading close to 0; towards 5 V → close to 1023. LED 13 turns on above half the range. The serial monitor displays raw values and voltage at 10 Hz.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials