Recording PDM data of XIAO nRF52840 Sense to SD card

I recorded PDM data to an SD card for practice. I hope this will be helpful for those who are interested. To record PDM data to SD card, several measures were necessary.

  1. Flush after each block recording to prevent file corruption.
  2. Prepare a sampleBuffer of 1000mS to match the 400mS write and flush time.
  3. Use a FIFO for 1000mS to prevent data loss due to SD’s long write time of 1500mS that sometimes occurs.
  4. speed up SD’s SPI clock as much as possible
  5. minimize the gain to prevent data overflow because the microphone is highly sensitive.

The SD contains PCM data with 16-bit 16 kHz sampling; I used Audacity’https://www.audacityteam.org/’ to convert it to WAV format. Unfortunately, the sound quality is far from HiFi.
Note: If the microphone input is even slightly too loud, the data will overflow and noise will be recorded.

Here are the sketches and sample WAV files used in the experiment.
POST_nRF52_XIAO_PDM_SD.zip (384.7 KB)

3 Likes

Hello! I was able to create a similar program to yours, and I had a few questions that you may be able to answer.

Have you recorded the current draw at all for your program?
Why do higher amplitude recordings cause skips/clipping in the audio? How can this be combatted? (An issue in my code)

Thanks!

Current consumption when 3.8V is applied to the battery pad. The average current is 15 mA and the peak is 60 mA.
If the recording clips even with minimal gain, how about reducing the sound input by plugging the microphone hole or moving the microphone away from the sound source?

Thanks for the graph! I have tried moving the device further away from the source, and while this does work, I want to ensure my device can log as much data as possible.

Audio analysis is fairly new to me, and I was wondering what causes the clipping? Additionally, for further clarification, does PDM.setGain(0); set the gain to -20dB? Is that the lowest setting?

Thank you in advance!

does PDM.setGain(0); set the gain to -20dB? Is that the lowest setting?

setGain(0); sets the gain to -20dB.

If you import the waveform into Audacity and check the waveform, you can see how it clips and inverts.

Thank you for your help!

There were some problems with the sketch attached to post 1, which have been corrected. In addition, it is now recorded in WAVE format. The changes are as follows

  1. setGain() is executed after the begin() since the gain is set to the default value in the begin()
  2. Change data writing to little-endian format
  3. Review PDM buffer size and FIFO size
  4. Use SD with the fastest write speed possible
  5. Record in WAVE format.

For example, you can import the waveforms into the application “Audacity” to edit, check, and play back. After editing, the waveform can be exported in wave format.

The timing of reading PDM data and writing to SD was checked with a digital scope.

  1. PDM data is pushed from the PDM buffer to the FIFO every 16mS for 200uS
  2. In most cases, the data is popped every 16mS and written to SD over 6mS
  3. In rare cases, it takes over 500mS to write to the SD

So I needed FIFO which will not overflow, If FIFO overflow, the red LED will blink 5 times, so please increase FIFO size.

POST_nRF52_XIAO_PDM_WAVE_SD.zip (2.4 KB)

HI there,
Could I use the expansion Flash chip if installed on the Grove Expansion board? as FIFO or fast storage for this?
The board Flash speed is fast so what is a big size of a wav file? Then at a more convenient time move to SD card (external flash no fear to overwrite on-board flash and corrupt?)
Curious
GL :slight_smile: PJ
:v:

XIAO’s onboard flash P25Q16H is 2MB, so it can record about 2 hours. I think the flash seems to have a shorter write time than SD, but sometimes it takes a long time to write and I think a FIFO is needed.
I plan to experiment with external 16MB flash in the future.

edit
I was mistaken. At 16000 samples/sec, a 2MB flash only has the capacity for 2 minutes.

2 Likes