Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e9aa6e0

Browse files
Merge pull request #19 from arduino/sync/karlsoderby/built-in-libs
[MKC-177,178,293,294] Core library migration
2 parents b7ad0f4 + ea7c92a commit e9aa6e0

File tree

5 files changed

+1482
-0
lines changed

5 files changed

+1482
-0
lines changed
Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
---
2+
title: PDM Library
3+
description: The PDM library allows you to use Pulse-density modulation microphones, found onboard the Nano RP2040 Connect & Nano 33 BLE Sense boards.
4+
author: 'Arduino'
5+
tags: [Microphone, PDM]
6+
---
7+
8+
## Overview
9+
10+
The PDM library allows you to use PDM ([Pulse-density modulation](https://en.wikipedia.org/wiki/Pulse-density_modulation)) microphones, such as the onboard MP34DT05 on the Arduino Nano 33 BLE Sense.
11+
12+
To use this library:
13+
14+
```
15+
#include <PDM.h>
16+
```
17+
18+
The library takes care of the audio that will be accessible also through the [ArduinoSound](https://www.arduino.cc/en/Reference/ArduinoSound) library.
19+
20+
21+
## Functions
22+
23+
### `begin()`
24+
25+
#### Description
26+
27+
Initialize the PDM interface.
28+
29+
#### Syntax
30+
31+
```
32+
PDM.begin(channels, sampleRate)
33+
34+
```
35+
36+
#### Parameters
37+
- channels: the number of channels, 1 for mono, 2 for stereo
38+
- sampleRate: the sample rate to use in Hz
39+
40+
#### Returns
41+
1 on success, 0 on failure
42+
43+
#### Example
44+
45+
```
46+
if (!PDM.begin(1, 16000)) {
47+
Serial.println("Failed to start PDM!");
48+
while (1);
49+
}
50+
51+
52+
```
53+
54+
### `end()`
55+
56+
#### Description
57+
58+
De-initialize the PDM interface.
59+
60+
#### Syntax
61+
62+
```
63+
PDM.end()
64+
65+
```
66+
67+
#### Parameters
68+
None
69+
70+
#### Returns
71+
Nothing
72+
73+
#### Example
74+
75+
```
76+
if (!PDM.begin(1, 16000)) {
77+
Serial.println("Failed to start PDM!");
78+
while (1);
79+
}
80+
81+
82+
//
83+
84+
PDM.end();
85+
86+
87+
```
88+
89+
### `available()`
90+
91+
#### Description
92+
93+
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.
94+
95+
#### Syntax
96+
97+
```
98+
PDM.available()
99+
100+
```
101+
102+
#### Parameters
103+
None
104+
105+
#### Returns
106+
The number of bytes available to read
107+
108+
#### Example
109+
110+
```
111+
// buffer to read samples into, each sample is 16-bits
112+
short sampleBuffer[256];
113+
114+
// number of samples read
115+
volatile int samplesRead;
116+
117+
//
118+
119+
// query the number of bytes available
120+
int bytesAvailable = PDM.available();
121+
122+
// read into the sample buffer
123+
PDM.read(sampleBuffer, bytesAvailable);
124+
125+
126+
```
127+
128+
### `read()`
129+
130+
#### Description
131+
132+
Read data from the PDM into the specified buffer.
133+
134+
#### Syntax
135+
136+
```
137+
PDM.read(buffer, size)
138+
139+
```
140+
141+
#### Parameters
142+
- buffer: array to store the PDM data into
143+
- size: number of bytes to read
144+
145+
#### Returns
146+
The number of bytes read
147+
148+
#### Example
149+
150+
```
151+
// buffer to read samples into, each sample is 16-bits
152+
short sampleBuffer[256];
153+
154+
// number of samples read
155+
volatile int samplesRead;
156+
157+
//
158+
159+
// query the number of bytes available
160+
int bytesAvailable = PDM.available();
161+
162+
// read into the sample buffer
163+
Int bytesRead = PDM.read(sampleBuffer, bytesAvailable);
164+
165+
// 16-bit, 2 bytes per sample
166+
samplesRead = bytesRead / 2;
167+
168+
169+
```
170+
171+
### `onReceive()`
172+
173+
#### Description
174+
175+
Set the callback function that is called when new PDM data is ready to be read.
176+
177+
#### Syntax
178+
179+
```
180+
181+
PDM.onReceive(callback)
182+
183+
```
184+
185+
#### Parameters
186+
callback: function that is called when new PDM data is ready to be read
187+
188+
#### Returns
189+
Nothing
190+
191+
#### Example
192+
193+
```
194+
// buffer to read samples into, each sample is 16-bits
195+
short sampleBuffer[256];
196+
197+
// number of samples read
198+
volatile int samplesRead;
199+
200+
//
201+
202+
// configure the data receive callback
203+
PDM.onReceive(onPDMdata);
204+
205+
// initialize PDM with:
206+
// - one channel (mono mode)
207+
// - a 16 kHz sample rate
208+
if (!PDM.begin(1, 16000)) {
209+
Serial.println("Failed to start PDM!");
210+
while (1);
211+
}
212+
213+
214+
//
215+
216+
void onPDMdata() {
217+
// query the number of bytes available
218+
int bytesAvailable = PDM.available();
219+
220+
// read into the sample buffer
221+
Int bytesRead = PDM.read(sampleBuffer, bytesAvailable);
222+
223+
// 16-bit, 2 bytes per sample
224+
samplesRead = bytesRead / 2;
225+
}
226+
227+
228+
229+
```
230+
231+
### `setGain()`
232+
233+
#### Description
234+
235+
Set the gain value used by the PDM interface.
236+
237+
#### Syntax
238+
239+
```
240+
PDM.setGain(gain)
241+
242+
```
243+
244+
#### Parameters
245+
gain: gain value to use, 0 - 255, defaults to 20 if not specified.
246+
247+
#### Returns
248+
Nothing
249+
250+
#### Example
251+
252+
```
253+
// optionally set the gain, defaults to 20
254+
PDM.setGain(30);
255+
256+
// initialize PDM with:
257+
// - one channel (mono mode)
258+
// - a 16 kHz sample rate
259+
if (!PDM.begin(1, 16000)) {
260+
Serial.println("Failed to start PDM!");
261+
while (1);
262+
}
263+
264+
265+
266+
```
267+
268+
### `setBufferSize()`
269+
270+
#### Description
271+
272+
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.
273+
274+
#### Syntax
275+
276+
```
277+
PDM.setBufferSize(size)
278+
279+
```
280+
281+
#### Parameters
282+
size: buffer size to use in bytes
283+
284+
#### Returns
285+
Nothing
286+
287+
#### Example
288+
289+
```
290+
PDM.setBufferSize(1024);
291+
292+
// initialize PDM with:
293+
// - one channel (mono mode)
294+
// - a 16 kHz sample rate
295+
if (!PDM.begin(1, 16000)) {
296+
Serial.println("Failed to start PDM!");
297+
while (1);
298+
}
299+
300+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /