entree-analogique.ino

Arduino Portenta H7 · File : entree-analogique.ino · Language : Arduino · Use case : Lire une entrée analogique 12 bits avec moyenne

The H7 12-bit ADC offers sixteen times the resolution of classic Unos. The 16-sample sliding window yields stable voltages for a 0-3.3 V industrial sensor or battery divider.

Hardware

An Arduino Portenta H7 (ST STM32H747 dual-core module Cortex-M7 @ 480 MHz + M4 @ 240 MHz, 8 MB SDRAM, high-density connector), powered by USB-C or 5 V. 1 10 kΩ potentiometer to test (or 0-3.3 V sensor).

Wiring

Wiper to A0, ends to 3V3 and GND. Input strictly ≤ 3.3 V (ADC pins are not 5 V tolerant).

How it works

analogReadResolution(12) exploits the full conversion; software averaging divides noise by 4. Conversion reference is 3.3 V.

Running it

Arduino IDE with the « Arduino Mbed OS Portenta Boards » core: pick « Portenta H7 » (M7 core), the USB-C port, then Upload. The DFU mode M7/M4 selector also loads the M4 core.

Variants

Sample via DMA (Mbed ADC API) for a fast stream, or fit an external reference for accuracy.

entree-analogique.ino

// ADC 12 bits (0..4095) sur A0 avec moyenne glissante.
const int CANAL = A0;
const int NB = 16;

uint16_t fenetre[NB];
int idx = 0;
bool plein = false;
unsigned long t0 = 0;

void setup() {
  analogReadResolution(12);
  Serial.begin(9600);
}

void loop() {
  if (millis() - t0 < 100) return;
  t0 = millis();

  fenetre[idx] = analogRead(CANAL);
  idx = (idx + 1) % NB;
  if (idx == 0) plein = true;
  int n = plein ? NB : idx;
  if (!n) return;

  uint32_t somme = 0;
  for (int i = 0; i < n; i++) somme += fenetre[i];
  uint32_t moyenne = somme / n;

  float tension = moyenne * 3.3f / 4095.0f;
  Serial.printf("brut=%lu  U=%.3f V\n", moyenne, tension);
}

← Back to the board