Skip to content
This repository was archived by the owner on Apr 16, 2021. It is now read-only.

Commit c076947

Browse files
sandeepmistryper1234
authored andcommitted
PDM: use SAMD I2S style double buffer, without nrfx, add plotter example (#1)
* PDM: use SAMD I2S style double buffer, without nrfx, add plotter example Co-Authored-By: per1234 <accounts@perglass.com>
1 parent 492db05 commit c076947

File tree

10 files changed

+366
-741
lines changed

10 files changed

+366
-741
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
This example reads audio data from the on-board PDM microphones, and prints
3+
out the samples to the Serial console. The Serial Plotter built into the
4+
Arduino IDE can be used to plot the audio data (Tools -> Serial Plotter)
5+
6+
Circuit:
7+
- Arduino Nano 33 BLE board
8+
9+
This example code is in the public domain.
10+
*/
11+
12+
#include <PDM.h>
13+
14+
// buffer to read samples into, each sample is 16-bits
15+
short sampleBuffer[256];
16+
17+
// number of samples read
18+
volatile int samplesRead;
19+
20+
void setup() {
21+
Serial.begin(9600);
22+
while (!Serial);
23+
24+
// configure the data receive callback
25+
PDM.onReceive(onPDMdata);
26+
27+
// optionally set the gain, defaults to 20
28+
// PDM.setGain(30);
29+
30+
// initialize PDM with:
31+
// - one channel (mono mode)
32+
// - a 16 kHz sample rate
33+
if (!PDM.begin(1, 16000)) {
34+
Serial.println("Failed to start PDM!");
35+
while (1);
36+
}
37+
}
38+
39+
void loop() {
40+
// wait for samples to be read
41+
if (samplesRead) {
42+
43+
// print samples to the serial monitor or plotter
44+
for (int i = 0; i < samplesRead; i++) {
45+
Serial.println(sampleBuffer[i]);
46+
}
47+
48+
// clear the read count
49+
samplesRead = 0;
50+
}
51+
}
52+
53+
void onPDMdata() {
54+
// query the number of bytes available
55+
int bytesAvailable = PDM.available();
56+
57+
// read into the sample buffer
58+
PDM.read(sampleBuffer, bytesAvailable);
59+
60+
// 16-bit, 2 bytes per sample
61+
samplesRead = bytesAvailable / 2;
62+
}

libraries/PDM/examples/RecordAndDumpOnSerial/RecordAndDumpOnSerial.ino

-54
This file was deleted.

libraries/PDM/keywords.txt

+4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ PDM KEYWORD1
1414
begin KEYWORD2
1515
end KEYWORD2
1616

17+
available KEYWORD2
18+
read KEYWORD2
19+
1720
onReceive KEYWORD2
1821

22+
setGain KEYWORD2
1923
setBufferSize KEYWORD2
2024

2125
#######################################

0 commit comments

Comments
 (0)