PDM Library
The PDM library allows you to use Pulse-density modulation microphones, found onboard the Nano RP2040 Connect & Nano 33 BLE Sense boards.
Overview
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.
Functions
begin()
begin()
Description
Initialize the PDM interface.
Syntax
1PDM.begin(channels, sampleRate)
Parameters
- channels: the number of channels, 1 for mono, 2 for stereo
- sampleRate: the sample rate to use in Hz
Returns
1 on success, 0 on failure
Example
1if (!PDM.begin(1, 16000)) {2 Serial.println("Failed to start PDM!");3 while (1);4 }
end()
end()
Description
De-initialize the PDM interface.
Syntax
1PDM.end()
Parameters
None
Returns
Nothing
Example
1if (!PDM.begin(1, 16000)) {2 Serial.println("Failed to start PDM!");3 while (1);4 }5
6
7 // 8
9 PDM.end();
available()
available()
Description
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.
Syntax
1PDM.available()
Parameters
None
Returns
The number of bytes available to read
Example
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()
Description
Read data from the PDM into the specified buffer.
Syntax
1PDM.read(buffer, size)
Parameters
- buffer: array to store the PDM data into
- size: number of bytes to read
Returns
The number of bytes read
Example
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()
onReceive()
Description
Set the callback function that is called when new PDM data is ready to be read.
Syntax
1PDM.onReceive(callback)
Parameters
callback: function that is called when new PDM data is ready to be read
Returns
Nothing
Example
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()
setGain()
Description
Set the gain value used by the PDM interface.
Syntax
1PDM.setGain(gain)
Parameters
gain: gain value to use, 0 - 255, defaults to 20 if not specified.
Returns
Nothing
Example
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()
setBufferSize()
Description
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.
Syntax
1PDM.setBufferSize(size)
Parameters
size: buffer size to use in bytes
Returns
Nothing
Example
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 }
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.