Software/Arduino Core/Sensors and Audio

Microphone

The on-board PDM microphone, the Microphone library, live audio streaming to Neuro Studio, and access to raw samples.

beginner2 min read

The Neuro N6 has an ST MP34DT05-A PDM microphone connected to the STM32N6 multi-function digital filter (MDF) peripheral. The MDF has six serial interfaces and filter paths with configurable Sinc4 or Sinc5 decimation, a reshape filter, a high-pass filter for DC removal and 16 or 24-bit output, and can run autonomously in Stop mode. The Microphone library captures it as 16-bit mono PCM and streams it to Neuro Studio, where the speaker icon plays it back. No camera is required.

cpp
#include <Microphone.h>

Microphone mic;

void setup() {
  Serial.begin(115200);
  if (mic.begin()) {
    Serial.print("microphone at ");
    Serial.print(mic.sampleRate());
    Serial.println(" Hz");
  }
}

void loop() {
  Serial.println(mic.peak());     // loudest sample since the last call, 0 to 32767
  delay(200);
}

begin() starts capture and streaming. Audio is DMA driven and continuous, so no per-loop call is required. peak() is a level meter that may be called while streaming.

Sample rate

The microphone runs at 32552 Hz. The MDF kernel clock is taken from a free divider off PLL4, which is left at 200 MHz because the PSRAM the application runs from depends on it. The exact rate is sent with the audio so that Neuro Studio plays it at the correct pitch.

The rate was confirmed on hardware by playing a 10 kHz tone and observing it in the spectrum analyser.

Raw samples

read(buffer, count) returns samples to the sketch. The stream and the sketch share one ring buffer, so a sketch that reads samples first disables streaming:

cpp
mic.streamToNS(false);

int16_t chunk[512];
if (mic.available() >= 512) {
  uint32_t got = mic.read(chunk, 512);
}

The spectrum analyser and audio event detection examples consume samples this way.

Hardware

SignalMCU pinFunction
PDM dataPB2MDF1_SDI1
PDM clockPE2MDF1_CCK0

The filter is MDF1 filter 1, SINC4 with decimation 32 and a reshape by 4, fed by GPDMA1 channel 0. MDF1 is marked secure in the resource isolation framework so that the secure DMA channel can write it. The ST MDF HAL driver is vendored in the library and compiles only into sketches that use the microphone.

Transport

Audio travels as a self-describing META_AUDIO_PCM record carrying the sample rate, channel count, bit depth, sample count and PCM data. Unlike sensor readings, which are latest-wins metadata, audio is an ordered lossless stream and is sent as its own metadata-only frames. A ring buffer of about one second absorbs jitter. The path works with or without a camera.

Diagnostics

With Tools > Debug console On, a [MICDBG] line prints once per second: the HAL bring-up results (clkcfg, mdfinit, acq should all be 0), the DMA registers (which should change between prints), a write counter (which should climb) and sample values (which should be non-zero). The first incorrect value identifies the failed link.

Power

The power save library's micSleep() stops the MDF clock and drives the PDM clock pin low, placing the microphone in its sleep state.