analogique-12-bits.ino
Teensy ADCs reach 12 bits (0-4095) versus 10 on classic Arduinos. This sketch combines hardware averaging (analogReadAveraging) and a software sliding window for a very stable voltage from an LDR or potentiometer.
Hardware
A Teensy 4.0 (NXP i.MX RT1062, Cortex-M7 @ 600 MHz, 3.3 V logic) powered over USB. 1 10 kΩ potentiometer (or LDR divider) between 3.3 V and GND, wiper to A0.
Wiring
Potentiometer wiper to A0, ends to 3.3 V and GND. Never exceed 3.3 V on a Teensy analog input.
How it works
analogReadResolution(12) raises conversion to 4095 steps; analogReadAveraging(8) already averages in hardware. The 16-value sliding window smooths residual spikes.
Running it
With Teensyduino in the Arduino IDE, select « Teensy 4.0 », Upload then confirm in the Teensy Loader. Pins are 3.3 V; the 4.0 is pin-compatible with the 3.2.
Variants
Switch to 13/14 bits per board (slower), or continuously sample with the DMA ADC for audio.
analogique-12-bits.ino
// ADC 12 bits (0..4095) avec moyenne glissante sur A0.
const int CANAL = A0;
const int NB = 16; // taille de la fenetre glissante
uint16_t echantillons[NB];
int index = 0;
bool plein = false;
unsigned long derniereMesure = 0;
void setup() {
analogReadResolution(12); // 12 bits sur les Teensy (defaut 10)
analogReadAveraging(8); // moyenne materielle sur 8 echantillons
Serial.begin(9600);
}
void loop() {
if (millis() - derniereMesure < 100) return;
derniereMesure = millis();
echantillons[index] = analogRead(CANAL);
index = (index + 1) % NB;
if (index == 0) plein = true;
int n = plein ? NB : index;
if (n == 0) return;
long somme = 0;
for (int i = 0; i < n; i++) somme += echantillons[i];
long moyenne = somme / n;
float tension = moyenne * 3.3 / 4095.0; // entree max 3,3 V
Serial.print(F("brut=")); Serial.print(moyenne);
Serial.print(F(" -> ")); Serial.print(tension, 3);
Serial.println(F(" V"));
}