🛠 Lire un capteur analogique (potentiomètre)
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
- 1 × Classic Arduino Nano (ATmega328P, 10-bit ADC, 8 analog inputs)
- 1 × 10 kΩ potentiometer (or analog sensor: LDR + resistor, flex sensor…)
- 3 × wires; board 5 V power supply (never more: the ADC accepts 0-5 V maximum)
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
- Measuring more than 5 V at the input: the ADC is limited to Vcc; exceeding this risks damaging the pin (use a divider for 12 V).
- Forgetting to connect the sensor ground to the Nano ground: the reading drifts and oscillates without reason.
- Expecting 0-5 V from analogRead: the function returns 0-1023, not volts; manual conversion is necessary.
- Wiring the wiper to 5 V and the other two pins together: the value stops changing, the divider is short-circuited.
Other tutorials for this board
Code examples for this board
buzzer-melodie.inoArduinoservo-sweep.inoArduinoultrason-hcsr04.inoArduinolcd-i2c.inoArduinoeeprom-contre.inoArduinosommeil-watchdog.inoArduino