🛠 ADC2, le piège quand le Wi-Fi est actif

ESP32 (original, ESP32-WROOM-32 class) · avance · Schéma : IMG-esp32-avance-3-adc2-piege-wifi-calibration.svg — Deux traces d'oscilloscope : l'ADC1 suit le potentiomètre, l'ADC2 reste à plat dès que le Wi-Fi démarre

Objectives

Visually demonstrate the documented pitfall: the ESP32's ADC2 (GPIO2, 4, 12-15, 17-19) becomes unusable as soon as Wi-Fi is running, because the radio driver reserves it. You will compare ADC1 and ADC2 readings before and after connecting to Wi-Fi, then address the calibration of the 12-bit ADC (attenuation, board-to-board variability, eFuse).

Hardware

Complete Code

#include <WiFi.h>

const uint8_t PIN_ADC2 = 4;    // ADC2_CH0
const uint8_t PIN_ADC1 = 34;   // ADC1_CH6, entree seule

void setup {
  Serial.begin(115200);
  analogSetPinAttenuation(PIN_ADC2, ADC_11db);
  analogSetPinAttenuation(PIN_ADC1, ADC_11db);
  Serial.println(F("-- Avant Wi-Fi --"));
  Serial.printf("ADC2 GPIO4 : %d | ADC1 GPIO34 : %d\n",
                analogRead(PIN_ADC2), analogRead(PIN_ADC1));

  WiFi.mode(WIFI_STA);
  WiFi.begin("MON_SSID", "MA_CLE");
  uint32_t t0 = millis;
  while (WiFi.status != WL_CONNECTED && millis - t0 < 10000) delay(200);
  Serial.printf("Connecte : %s\n",
                WiFi.status == WL_CONNECTED ? "oui" : "non");
}

void loop {
  Serial.printf("Wi-Fi actif -> ADC2 : %d | ADC1 : %d\n",
                analogRead(PIN_ADC2), analogRead(PIN_ADC1));
  delay(1000);
}

Expected Verification

Before Wi-Fi, both readings follow the potentiometers. Once connected, the ADC2 reading freezes (0 or a fixed nonsensical value) while ADC1 continues to vary normally: the ADC2/Wi-Fi coexistence issue is well documented and non-negotiable.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials