🛠 Lire un capteur analogique sur A0-A7
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
- 1 × Arduino Nano Every
- 1 × 10 kΩ potentiometer (or analog sensor: LDR + resistor, flex sensor…)
- 3 × jumper wires
- Micro-USB cable
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
- Reading a digital pin as analog: only A0-A7 are ADC inputs on the connector.
- Expecting 1024: the 10-bit ADC ranges from 0 to 1023, hence dividing by 1023 and not 1024.
- Unconnected input: a floating ADC pin gives drifting values — always provide a path to GND.
- Printing speed: printing on every
loopfloods the monitor; throttle withmillisas shown above. - I²C on this board is NOT on A4/A5 by default (
Wirebus on dedicated SDA/SCL pins): if you are reusing a classic Nano schematic, check this point carefully before adding an I²C sensor.
Other tutorials for this board
- Faire clignoter la LED builtin (broche 13)
- Lire un bouton avec résistance de pull-up interne
- Variateur lumineux en PWM (broches 3, 5, 6, 9, 10)
- Piloter un micro-servo avec la bibliothèque Servo
- Deux ports série : Serial (USB) et Serial1 (broches 0/1)
- Apprivoiser les registres modernes de l'ATmega4809 : le timer TCB
Code examples for this board
multi-serie.inoArduinoadc-filtre.inoArduinointerrupt-bouton.inoArduinonvm-consignes.inoArduinosommeil-every.inoArduinowatchdog-secours.inoArduino