🛠 Lire un capteur analogique (potentiomètre)
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
- 1 × Arduino Pro Mini (note your model: the ADC full scale is VCC, 5 V or 3.3 V)
- 1 × 10 kΩ potentiometer (or analog sensor: LDR + resistor, flex sensor…)
- 3 × wires; 3.3 V or 5 V power supply from the board depending on the model (never more: the ADC accepts 0-VCC maximum)
- FTDI/CP2102 adapter for the serial monitor
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
- Forgetting that the full scale is VCC: on the 3.3 V model, converting with 5000 mV overestimates every measurement; the ADC does not have a fixed internal reference like other boards.
- Measuring more than VCC on the input: the ADC is limited to VCC; beyond that you risk damaging the pin (use a divider for 12 V).
- Forgetting to connect the sensor ground to the Pro Mini ground: the reading drifts and oscillates for no reason.
- Expecting 0-5 V from analogRead: the function returns 0-1023, not volts; manual conversion is necessary.
- Confusing A4/A5 with free inputs: these are also SDA/SCL for I²C — perfect for later, but do not wire them as analog inputs if I²C is running.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-poussoir.inoArduinopwm-fade.inoArduinocapteur-lumiere-ldr.inoArduinoservo-balayage.inoArduinoruban-neopixel.inoArduino