🛠 Lire une tension avec l'ADC A0 et un diviseur de tension
Objectives
Understand the main hardware pitfall of the ESP8266EX: its single analog-to-digital converter (pin A0, 10 bits) only measures 0 to 1.0 V, not 0 to 3.3 V like an Arduino. To measure higher voltages, an external voltage divider is required. Be aware of board differences: NodeMCU boards already integrate a divider (ratio approximately 5/1.1) on A0, whereas a bare ESP-12E module has none — the code and wiring are not the same. The actual voltage is calculated from the raw analogRead value and the divider ratio.
Hardware
- 1 bare ESP8266-ESP-12E module (or check if your board is a NodeMCU with an integrated divider)
- 1 resistor R1 = 20 kΩ and 1 resistor R2 = 10 kΩ (×3 divider)
- 1 voltage source to measure between 0 and 3.0 V (e.g., a 10 kΩ potentiometer wired as a divider on the 3.3 V rail)
- Stable 3.3 V power supply, breadboard, wires
Complete code
// Diviseur externe R1=20k (entre Vin et A0) et R2=10k (entre A0 et GND) -> rapport 3
const float RAPPORT = 3.0f; // (R1 + R2) / R2
const float FULL_SCALE = 1.0f; // A0 nu : pleine échelle = 1,0 V (pas 3,3 V !)
void setup {
Serial.begin(115200);
Serial.println("Lecture ADC A0 (0-1,0 V sur module nu)");
}
void loop {
int raw = analogRead(A0); // 10 bits : 0..1023
float vA0 = raw / 1023.0f * FULL_SCALE; // tension sur la broche A0
float vIn = vA0 * RAPPORT; // tension réelle mesurée
Serial.printf("raw=%4d A0=%.3f V Vin=%.3f V\n", raw, vA0, vIn);
delay(500);
yield;
}
Expected verification
Turn the potentiometer: the raw value should sweep approximately 0 to 1023, and Vin should follow the voltage measured with a multimeter (±0.1 V, the ESP8266 ADC is mediocre). With a ×3 divider, Vin ≈ 3× the voltage actually present on A0. On a bare module, inject 0 V directly onto A0: raw should drop to 0 or 1.
Common errors
- Believing that A0 accepts 3.3 V on a bare module: above approximately 1.0 V, the measurement is clipped and the pin may be stressed out of range; the divider is mandatory.
- Adding an external divider on a NodeMCU that already has an internal one: the total ratio is incorrect (×3 × 4.5 ≈ ×13) and all measurements are wrong.
- Forgetting to connect the ground of the measured source to the module's ground: without a common reference, the reading is inconsistent.
- Using A0 while Wi-Fi is transmitting: the ADC shares RF circuits, measurements become noisy; perform the reading just before deep-sleep or filter by averaging.
- Expecting clockwork precision: the ADC is highly non-linear at the beginning and end of the range, accept ±50 mV of error.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht11.inoArduinoluxmetre.inoArduinoserveur-web.inoArduino