PDM microphone not working on nRF52840 Sense

I’ve trying to get some data from PDM mic. but no success. Read all the forum and wiki, trying adafruit and seeed code, all libs are installed.

Here is the sample code:

/*
  This example reads audio data from the on-board PDM microphones, and prints
  out the samples to the Serial console. The Serial Plotter built into the
  Arduino IDE can be used to plot the audio data (Tools -> Serial Plotter)

  Please try with the Circuit Playground Bluefruit

  This example code is in the public domain.
*/

#include <PDM.h>

// buffer to read samples into, each sample is 16-bits
short sampleBuffer[256];

// number of samples read
volatile int samplesRead;

void setup() {
  Serial.begin(9600);
  while (!Serial) yield();

  // configure the data receive callback
  PDM.onReceive(onPDMdata);

  // optionally set the gain, defaults to 20
  // PDM.setGain(30);

  // initialize PDM with:
  // - one channel (mono mode)
  // - a 16 kHz sample rate
  if (!PDM.begin(1, 16000)) {
    Serial.println("Failed to start PDM!");
    while (1) yield();
  }
}

void loop() {
  // wait for samples to be read
  if (samplesRead) {
    // print samples to the serial monitor or plotter
    for (int i = 0; i < samplesRead; i++) {
      Serial.println(sampleBuffer[i]);
    }
    // clear the read count
    samplesRead = 0;
  }
}

void onPDMdata() {
  // query the number of bytes available
  int bytesAvailable = PDM.available();

  // read into the sample buffer
  PDM.read(sampleBuffer, bytesAvailable);

  // 16-bit, 2 bytes per sample
  samplesRead = bytesAvailable / 2;
}

The seeed sample code and adafruit is very similar and behaves the same on my xiao nrf52840 Sense. The serial output is counting down from some big number and ends with 7 and still there. Looks like data, but not changing while the mic tapped or knocked.

Arduino ide 2.3.2
Seeed nRF52480 mbed 2.9.2

Hi Eduard_Sik,
I tried the code you posted. When I whistle I see a sine curve on the serial plotter. It is working.
Have you selected “Seeed XIAO BLE Sense - nRF52840” ?
Is your XIAO “Seeed XIAO BLE Sense” ?

1 Like