adc-filtre.ino

Arduino Nano Every · File : adc-filtre.ino · Language : Arduino · Use case : ADC 10 bits filtré par moyenne glissante

The Nano Every analog converter stays 10-bit but gains internal 1.024/2.048 V references, precious for small signals. This exponential filter smooths the potentiometer without a buffer and only prints when the value truly moves.

Hardware

An Arduino Nano Every (ATmega4809 at 20 MHz, same footprint as the Nano, micro-B USB, separate UART chip, 5 V pins, drop-in compatibility with the classic Nano). Nano Every, 0-5 V analog source on A0 (potentiometer to test).

Wiring

Signal on A0, common GND; never exceed VCC (5 V) on a 4809 pin.

How it works

The exponential average (alpha=0.1) weights each reading: time constant ~200 ms at 50 Hz. The 2-count threshold keeps the monitor from filling with background noise.

Running it

Install the Arduino megaAVR Boards package in the IDE, select Arduino Nano Every, upload. The USB Serial is separate from hardware Serial1 (RX/TX pins): classic serial code works as is.

Variants

Switch analogReference(INTERNAL) for a 0-1 V sensor without an amplifier, or raise ALPHA when responsiveness beats noise.

adc-filtre.ino

// ADC du 4809 : 10 bits comme le 328P, mais reference configurable.
// Filtre exponentiel + seuil de changement pour le moniteur serie.
const int CANAL = A0;
const float ALPHA = 0.1; // poids de la nouvelle mesure

float valeur = 0;
bool init = false;
unsigned long dernier = 0;

void setup() {
  Serial.begin(115200);
  analogReadResolution(10); // 0..1023 (par defaut sur Every)
  analogReference(DEFAULT); // 5 V ; INTERNAL = 1.024 V ou 2.048 V
}

void loop() {
  if (millis() - dernier < 20) return; // 50 Hz
  dernier = millis();
  float brut = analogRead(CANAL);
  if (!init) { valeur = brut; init = true; }
  // moyenne exponentielle : reactivite sans buffer de 16 mesures
  valeur = valeur * (1 - ALPHA) + brut * ALPHA;

  static float derniereSortie = -10;
  if (abs(valeur - derniereSortie) >= 2) { // mort d'affichage
    derniereSortie = valeur;
    Serial.print(F("brut="));
    Serial.print(valeur, 1);
    Serial.print(F(" V="));
    Serial.println(valeur * (5.0 / 1023.0), 3);
  }
}

← Back to the board