Skip to content

Commit e21fc6f

Browse files
committed
Add documentation about the sampling rate, serial and gain
1 parent a2c06d7 commit e21fc6f

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

libraries/PDM/examples/PDMSerialPlotter/PDMSerialPlotter.ino

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,65 @@
44
Arduino IDE can be used to plot the audio data (Tools -> Serial Plotter)
55
66
Circuit:
7-
- Arduino Nano 33 BLE board
7+
- Arduino Nano 33 BLE board or
8+
- Arduino Portenta H7 board plus Portenta Vision Shield
89
910
This example code is in the public domain.
1011
*/
1112

1213
#include <PDM.h>
1314

14-
// buffer to read samples into, each sample is 16-bits
15+
// Buffer to read samples into, each sample is 16-bits
1516
short sampleBuffer[256];
1617

17-
// number of samples read
18+
// Number of audio samples read
1819
volatile int samplesRead;
1920

2021
void setup() {
2122
Serial.begin(9600);
2223
while (!Serial);
2324

24-
// configure the data receive callback
25+
// Configure the data receive callback
2526
PDM.onReceive(onPDMdata);
2627

27-
// optionally set the gain, defaults to 20
28+
// Optionally set the gain
29+
// Defaults to 20 on the BLE Sense and -10 on the Portenta Vision Shield
2830
// PDM.setGain(30);
2931

30-
// initialize PDM with:
32+
// Initialize PDM with:
3133
// - one channel (mono mode)
32-
// - a 16 kHz sample rate
34+
// - a 16 kHz sample rate for the Arduino Nano 33 BLE Sense
35+
// - a 32 kHz or 64 kHz sample rate for the Arduino Portenta Vision Shield
3336
if (!PDM.begin(1, 16000)) {
3437
Serial.println("Failed to start PDM!");
3538
while (1);
3639
}
3740
}
3841

3942
void loop() {
40-
// wait for samples to be read
43+
// Wait for samples to be read
4144
if (samplesRead) {
4245

43-
// print samples to the serial monitor or plotter
46+
// Print samples to the serial monitor or plotter
4447
for (int i = 0; i < samplesRead; i++) {
4548
Serial.println(sampleBuffer[i]);
4649
}
4750

48-
// clear the read count
51+
// Clear the read count
4952
samplesRead = 0;
5053
}
5154
}
5255

56+
/**
57+
* Callback function to process the data from the PDM microphone.
58+
* NOTE: This callback is executed as part of an ISR.
59+
* Therefore using `Serial` to print messages inside this function isn't supported.
60+
* */
5361
void onPDMdata() {
54-
// query the number of bytes available
62+
// Query the number of available bytes
5563
int bytesAvailable = PDM.available();
5664

57-
// read into the sample buffer
65+
// Read into the sample buffer
5866
PDM.read(sampleBuffer, bytesAvailable);
5967

6068
// 16-bit, 2 bytes per sample

0 commit comments

Comments
 (0)