🛠 ADC2, le piège quand le Wi-Fi est actif
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
- 1 ESP32 DevKit v1 board
- 1 potentiometer on GPIO4 (ADC2_CH0), 1 on GPIO34 (ADC1_CH6)
- Access to a 2.4 GHz Wi-Fi network
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
- Concluding that a "pin is dead": ADC2 is not broken; the driver simply refuses access while Wi-Fi is active.
- Designing battery measurement for a Wi-Fi product on GPIO2 or GPIO4: wire the voltage divider and measurement on GPIO32-39 (ADC1, input-only pins).
- Trusting the raw 12-bit value: the ADC is non-linear at the ends of the range and varies from board to board; calibrate using two points or use esp_adc_cal_characterize (eFuse).
- Forgetting that GPIO2 is also a strapping pin: a permanent voltage divider on it can prevent booting.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht22.inoArduinooled-i2c.inoArduinoserveur-web.inoArduino