🛠 Mini sample player on the 12-bit DAC

Arduino UNO R4 Minima · avance · Schéma : IMG-uno-r4-minima-avance-2-mini-lecteur-audio-dac.svg — UNO R4 Minima, broche A0 vers un petit amplificateur (TPA2006) puis haut-parleur, tableau de 256 échantillons 8 bits en flash, sortie audio annotée.

Goal

Build a tiny sample player: store an 8-bit waveform in flash, rescale it to the 12-bit DAC on A0, and stream it at a fixed rate. The budget is generous for a microcontroller - 256 KB flash and 32 KB SRAM - so hundreds of samples of a few seconds fit in code space.

Code skeleton

const uint8_t SND[] PROGMEM = { /* 8-bit PCM, e.g. 8 kHz */ };
const size_t N = sizeof(SND);
const float RATE = 8000.0f;

void setup {
  analogWriteResolution(12);
}

void loop {
  static uint32_t t0 = micros;
  static size_t i = 0;
  uint32_t n = (micros - t0) / (1000000UL / (uint32_t)RATE);
  if (n < N) {
    uint8_t s = pgm_read_byte(&SND[n]);
    analogWrite(A0, (s - 128) * 8 + 2048);  // 8->12 bit, mid-scale
  } else {
    analogWrite(A0, 2048);                  // silence = mid rail
    if (micros - t0 > (N * 1e6f) / RATE + 100000) { t0 = micros; i = 0; }
  }
}

Notes

Autres tutoriels pour cette carte

Exemples de code pour cette carte

← Retour à la fiche 🛠 Tous les tutoriels