The PDM library allows you to use PDM (Pulse-density modulation) microphones, such as the onboard MP34DT05 on the Arduino Nano 33 BLE Sense.
To use this library:
1#include <PDM.h>
The library takes care of the audio that will be accessible also through the ArduinoSound library.
begin()
Initialize the PDM interface.
1PDM.begin(channels, sampleRate)
1 on success, 0 on failure
1if (!PDM.begin(1, 16000)) {2 Serial.println("Failed to start PDM!");3 while (1);4 }
end()
De-initialize the PDM interface.
1PDM.end()
None
Nothing
1if (!PDM.begin(1, 16000)) {2 Serial.println("Failed to start PDM!");3 while (1);4 }5
6
7 // 8
9 PDM.end();
available()
Get the number of bytes available for reading from the PDM interface. This is data that has already arrived and was stored in the PDM receive buffer.
1PDM.available()
None
The number of bytes available to read
1// buffer to read samples into, each sample is 16-bits2short sampleBuffer[256];3
4// number of samples read5volatile int samplesRead;6
7// 8
9 // query the number of bytes available10 int bytesAvailable = PDM.available();11
12 // read into the sample buffer13 PDM.read(sampleBuffer, bytesAvailable);
read()
Read data from the PDM into the specified buffer.
1PDM.read(buffer, size)
The number of bytes read
1// buffer to read samples into, each sample is 16-bits2short sampleBuffer[256];3
4// number of samples read5volatile int samplesRead;6
7// 8
9 // query the number of bytes available10 int bytesAvailable = PDM.available();11
12 // read into the sample buffer13 Int bytesRead = PDM.read(sampleBuffer, bytesAvailable);14
15 // 16-bit, 2 bytes per sample16 samplesRead = bytesRead / 2;
onReceive()
Set the callback function that is called when new PDM data is ready to be read.
1PDM.onReceive(callback)
callback: function that is called when new PDM data is ready to be read
Nothing
1// buffer to read samples into, each sample is 16-bits2short sampleBuffer[256];3
4// number of samples read5volatile int samplesRead;6
7// 8
9 // configure the data receive callback10 PDM.onReceive(onPDMdata);11
12 // initialize PDM with:13 // - one channel (mono mode)14 // - a 16 kHz sample rate15 if (!PDM.begin(1, 16000)) {16 Serial.println("Failed to start PDM!");17 while (1);18 }19
20
21 // 22
23void onPDMdata() {24 // query the number of bytes available25 int bytesAvailable = PDM.available();26
27 // read into the sample buffer28 Int bytesRead = PDM.read(sampleBuffer, bytesAvailable);29
30 // 16-bit, 2 bytes per sample31 samplesRead = bytesRead / 2;32}
setGain()
Set the gain value used by the PDM interface.
1PDM.setGain(gain)
gain: gain value to use, 0 - 255, defaults to 20 if not specified.
Nothing
1// optionally set the gain, defaults to 202 PDM.setGain(30);3
4 // initialize PDM with:5 // - one channel (mono mode)6 // - a 16 kHz sample rate7 if (!PDM.begin(1, 16000)) {8 Serial.println("Failed to start PDM!");9 while (1);10 }
setBufferSize()
Set the buffer size (in bytes) used by the PDM interface. Must be called before PDM.begin(...), a default buffer size of 512 is used if not called. This is enough to hold 256 16-bit samples.
1PDM.setBufferSize(size)
size: buffer size to use in bytes
Nothing
1PDM.setBufferSize(1024);2
3 // initialize PDM with:4 // - one channel (mono mode)5 // - a 16 kHz sample rate6 if (!PDM.begin(1, 16000)) {7 Serial.println("Failed to start PDM!");8 while (1);9 }