🛠 ADC et bruit Wi-Fi : le moyennage qui sauve

ESP32-C3 · avance · Schéma : IMG-esp32-c3-avance-3-adc-bruit-wifi-moyennage.svg — Lecture ADC sur ESP32-C3 avec et sans trafic Wi-Fi, bruit atténué par une moyenne sur 64 échantillons

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials