🛠 Lire une tension avec l'ADC A0 et un diviseur de tension

ESP8266 (ESP-12E module) · debutant · Schéma : IMG-esp8266-esp-12e-debutant-3-esp8266-adc-a0-diviseur.svg — Module ESP8266-ESP-12E nu, diviseur de tension R1 20 kΩ / R2 10 kΩ ramenant une tension 0–3,3 V dans la plage 0–1,0 V de la broche A0.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials