analogique-12-bits.ino

Teensy 3.2 · File : analogique-12-bits.ino · Language : Arduino · Use case : Exploiter la résolution 12 bits de l'ADC Teensy

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 3.2 (NXP Kinetis MK20DX256, Cortex-M4 @ 72 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

Install the Teensyduino plugin in the Arduino IDE, select the « Teensy 3.2 » board, click Upload then the Teensy Loader button. Note: 3.3 V pins.

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"));
}

← Back to the board