🛠 ADC et bruit Wi-Fi : le moyennage qui sauve
Objectives
On the ESP32-C3, ADC1 occupies GPIO0-4 and ADC2 occupies GPIO2-6 — following the same rule as the ESP32: ADC2 is unavailable when Wi-Fi is running. Even on ADC1, radio bursts inject noise into measurements (current spikes during transmission). You will highlight this noise and neutralize it by averaging over a window.
Hardware
- 1 ESP32-C3 board
- 1 stable voltage source (divider output or reference) on GPIO3 (ADC1_CH3)
- 2.4 GHz Wi-Fi access
Complete code
#include <WiFi.h>
const uint8_t CAPTEUR = 3; // GPIO3 = ADC1_CH3 (ADC1 sur C3)
const uint8_t N = 64;
uint32_t moyenneFenetre {
uint64_t somme = 0;
for (uint8_t i = 0; i < N; i++) somme += analogRead(CAPTEUR);
return (uint32_t)(somme / N);
}
void setup {
Serial.begin(115200);
analogSetPinAttenuation(CAPTEUR, ADC_11db);
Serial.printf("1 echantillon : %u | moyenne %u : %u\n",
analogRead(CAPTEUR), N, moyenneFenetre);
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("Wi-Fi : %s\n",
WiFi.status == WL_CONNECTED ? "connecte" : "echec");
}
void loop {
uint32_t brut = analogRead(CAPTEUR);
uint32_t lisse = moyenneFenetre;
Serial.printf("brut %u | moyenne %u echantillons %u\n", brut, N, lisse);
delay(500);
}
Expected verification
Without Wi-Fi, the raw reading is already stable. Once connected (and more so during a transfer), the raw value starts dancing by several dozen points in sync with the bursts, while the average over 64 samples remains nearly fixed: the signal-to-noise ratio is improved by a factor of the square root of N, which is approximately 8 here.
Common errors
- Wiring the measurement to GPIO2 or GPIO4 (ADC2) and blaming the noise: with Wi-Fi active, the reading fails first due to ADC2/Wi-Fi coexistence.
- Averaging without filtering outliers: a single radio spike can skew a small average; prefer a median or a sliding filter.
- Forgetting the first sample after wake-up: always discard one or two samples before the useful measurement.
- Expecting 12-bit stability: the lower bits of the ESP32-C3 ADC are inherently noisy; aim for 9-10 usable bits and calibrate to actual voltage.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht22.inoArduinooled-i2c.inoArduinoserveur-web.inoArduino