PDM can‘t work in Arduino Nano RP2040 Connect

I can't read data from the PDM microphone of the Arduino Nano RP2040 Connect. I used the example provided in this link: https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-microphone-basics. However, the output sample value remained at -128 and did not change. I am wondering how to confirm whether the hardware of the microphone is faulty or if there are some software issues?

Start from showing yohr test code

#include <WiFiNINA.h>
#include <PDM.h>

bool LED_SWITCH = false;

// default number of output channels
static const char channels = 1;

// default PCM output frequency
static const int frequency = 16000;

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

// Number of audio samples read
volatile int samplesRead;

void setup() {
  Serial.begin(9600);
  pinMode(LEDB, OUTPUT);
  while (!Serial);
  // Configure the data receive callback
  PDM.onReceive(onPDMdata);

  // Optionally set the gain
  // Defaults to 20 on the BLE Sense and -10 on the Portenta Vision Shields
  // PDM.setGain(30);

  // Initialize PDM with:
  // - one channel (mono mode)
  // - a 16 kHz sample rate for the Arduino Nano 33 BLE Sense
  // - a 32 kHz or 64 kHz sample rate for the Arduino Portenta Vision Shields
  if (!PDM.begin(channels, frequency)) {
    Serial.println("Failed to start PDM!");
    while (1);
  }
}

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++) {
      if (channels == 2) {
        Serial.print("L:");
        Serial.print(sampleBuffer[i]);
        Serial.print(" R:");
        i++;
      }
      Serial.println(sampleBuffer[i]);

      if (sampleBuffer[i] > 10000 || sampleBuffer[i] <= -10000) {
        LED_SWITCH = !LED_SWITCH;
        if (LED_SWITCH) {
          Serial.println();
          digitalWrite(LEDB, HIGH);
          Serial.println("ON!");
          Serial.println();
          delay(1000);
        }
        else {
          Serial.println();
          digitalWrite(LEDB, LOW);
          Serial.println("OFF!");
          Serial.println();
          delay(1000);
        }
      }
    }

    // Clear the read count
    samplesRead = 0;
  }
}

/**
   Callback function to process the data from the PDM microphone.
   NOTE: This callback is executed as part of an ISR.
   Therefore using `Serial` to print messages inside this function isn't supported.
 * */
void onPDMdata() {
  // Query the number of available bytes
  int bytesAvailable = PDM.available();

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

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

I Have the same problem, have you found any solution???

Replying so this doesn't auto-close. I have the exact same problem running the exact same code. Has anyone found a solution?

Install the latest Package Core for the board and also install the library indicated in the screenshot below. Then run the example (https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-microphone-basics) again.

Thanks for the suggestion. Unfortunately I'm still seeing the same problem.

To be extra safe I tried uninstalling all libraries and core packages that I have in the Arduino IDE 2.2.1. Then I installed the following three packages:

  • Arduino Mbed OS Nano Boards - version 4.0.8 (through the Boards Manager)
  • Adafruit Zero PDM Library - version 1.2.1 (through the Library Manager)
  • WifiNINA - version 1.8.14 (through the Library Manager)

When I then uploaded the sketch provided in the Reading Microphone Data on Nano RP2040 Connect I got the same behavior described in the original post. Every value in the Serial Monitor is "-128".

It is highly likely that the onboard microphone is faulty. If still under warranty, contact the retailer/distributor or if bought from Arduino directly contact Arduino via Contact Us

Popping back in here to confirm that @dozieagada24 was right. It seems like the hardware was faulty.

I purchased the original faulty Nano RP2040 Connect (with headers) from Amazon. After running into the problem described in this thread, I purchased another Nano RP2040 Connect (without headers) directly from Arduino. I just received it and tested it out and it appears to be working correctly.

This is happening to me also. Bought Rp2040 from Arduino official store a couple years ago but never used. Today it only reads -128. Considering that few people report the issue, I'm not sure it is an hardware problem (unless a whole batch of boards were faulty). I updated the core to 4.0.10 but still not working.

Found this here in the forum: Ardunio nano rp2040 microphone not available - #4 by sterretje

It suggests sampling frequency should be 20 kHz, not 16 kHz.
Cannot test now, but will soon.

1 Like

Nice find @eloquentarduino!. Thanks for sharing that link. I can confirm it solved the problem for me.

There's a note in the Nano RP2040 Connect Cheat Sheet that describes this exact situation and the recommended solution:

  • Please note: The sampling frequency in the PDMSerialPlotter example is set to 16000 Hz. If the microphone appears to not be working (monitor is printing a value of -128), try to change this rate to 20000 Hz. You can change this at the top of the PDMSerialPlotter example sketch, as shown in the example below:

static const int frequency = 20000; //frequency at 20 KHz instead of 16 KHz

1 Like

that's works for mee thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.