PDM digital microphone support for P2/Photon 2 and Gen 3

The Microphone_PDM library is now available! It supports PDM digital microphones on both RTL872x (P2, Photon 2) and nRF52 (Boron, B Series SoM, Tracker, E404X, Argon).

The repository including instructions, sample code, and API documentation and is available here, and the library is in the community libraries.

5 Likes

Hi @rickkas7 -

I am trying to find some example code on how to use a PDM sensor. I found your library but have not seen any example code?

Regards, Friedl.

1 Like

Look in the examples and more-examples directories in the Github repo for it. There's also a link to the full API documentation there.

The API is very simple and is well-suited for local processing of data, but there aren't any examples of that yet.

2 Likes

Hi. Thank, will have another look!

The most common reason in the Web IDE is that you didn't have the Photon 2 selected as the device. It needs to have a gold star to the left of the device name in the Devices tab (target icon).

Hi Rick- I deleted my own post before noticing your message below it. Turns out that my copy-pasting the example into a new program and adding the library wasn't the right way to go (been a year since I last used these devices). When I chose the option to "use this example in a project", it compiled OK (but I haven't yet tested the code)
Another Photon2 question- When I add Serial.begin(115200) and Serial.print("123") in the loop(), nothing prints out in an Arduino Serial Terminal (when I select the USB COM port that pops up in Device Manager when I plug the Photon2 in). At first, I didn't think of loading Particle Workbench/Terminal just to test the serial port, but when I later did, the USB serial output stream does show up in its Terminal. And Realterm serial terminal picks it up- regardless of baudrate set. It seems like Photon2 is not enumerating as a CDC COM port and the Workbench Terminal is finding it by VID/PID . So, it seems only Arduino Serial terminal has problems with it
.I wrote a few Circuit Cellar magazine articles on Particle back in the Electron/Photon days and am doing another series now on your new generation, so I'm just trying to keep things straight.
Thanks

The baud rate is irrelevant on Particle devices in USB CDC mode, both in what you connect with and what you pass to Serial.begin(). In fact, you can just leave the number out of the begin statement for USB serial.

I haven't checked with the Arduino serial terminal, but it wouldn't surprise me if it filters based on VID/PID, in which case it wouldn't see Particle devices since they don't have the Arduino VID.

The other thing to watch out for is if you use the default values for SYSTEM_MODE and SYSTEM_THREAD, setup() and loop() don't run unless cloud connected (breathing cyan) so this can cause confusion when you're expecting serial output since you won't get any immediately at boot.

Thanks Rick. Yeh, with USB COM ports the baudrate is an artificial concept anyway, but in other cases, it doesn't work unless they match- The Arduino Serial terminal is one example of this- but it at least prints out garbage so you know something is happening.
I use the Arduino serial terminal for Teensys, ESP32s, AVRs- many different boards, and so far this is the first time it didn't work for me. Also, Visual Studio/Visual Micro that I use for all chips that have Arduino support- doesn't work either. But, it's not surprising as VS/VM is just a fancy wrapper around Arduino tool-chain.
Yes, I'm quite aware that "my code" doesn't get executed until a Particle MCU has connected to your cloud and the LED is breathing blue/cyan.
While I have you, a working Argon project of mine which scanned for BLE beacons won't compile for the Photon2. It uses the <BeaconScanner.h> library and fails to find
BLE_GAP_SEC_STATUS_SUCCESS and BLE_GAP_SEC_STATUS_CONFIRM_VALUE

This program was compiled to OS 4.0.1 on my Argon 6 months ago and works fine.
Thanks

The BLE issue is that the P2/Photon 2 (RTL872x) don't support BLE secure modes (LESC pairing).

I don't doubt your answer is correct, but seems to me that beacons are the most primitive/least secure BLE protocols one could use.
.

Oh, I think you're running into this problem. It can be fixed with a #if as long as you are not using the Laird BT510 beacons:

Thanks Rick. No, I'm using iBeacons or Eddystone- not Laird. I'll give this a try when I get a chance.

Rick: Pardon my ignorance on this, but I cannot make any progress trying to compile the Microphone_PDM using SDFat example
This is part of the Microphone_PDM-main.zip file that you give a link for in your post .
Specifically, I am looking in the more-examples folder at 1-sdfat.
What I hoped this would do is save the PDM microphone data to an SDCARD I have connected up.
Using the Web-IDE, I cut/paste the sdfat.cpp code into a new program.
Then I add the 3 libraries called for at the head of this program, using the Library "Include in Program" button.
This procedure duplicates the 3 existing #include ... statments, so I remove them. When I compile it, I get tons of errors like
/firmware/user/../wiring/inc/spark_wiring_logging.h:730:0: multiple definition of "WavHeaderBase::~WavHeaderBase()"
lib/Microphone_PDM/WavWriter.cpp:90:0: multiple definition of "WavHeaderBase::getDataOffset() const"
etc.
Given this problem, I remembered that it might be better to just look at the
examples provided for the Microphone_PDM library, and invoke the
"Use this example as a program" option. However, none of the "extra-examples" are included, so that doesn't work.
On another matter, at the end of this thread, you gave instructions on how I could get the beacon function working with the P2 by bypassing the laird beacon code. This worked OK for me- thanks.

@rickkas7 I am trying the Example 1 Audio over TCP on a Particle Argon and when compiling code, I get an error that hal_pin_map() is not defined. Thoughts?

....
lib/Microphone_PDM/src/Microphone_PDM_nRF52.cpp: In member function 'virtual int Microphone_PDM_nRF52::init()':
lib/Microphone_PDM/src/Microphone_PDM_nRF52.cpp:18:25: error: 'hal_pin_map' was not declared in this scope
18 | Hal_Pin_Info *pinMap = hal_pin_map();
| ^~~~~~~~~~~
.....

I released version 0.0.4 of the library which corrects that issue with Device OS 4.x and earlier on nRF52 devices (Argon, Boron, etc.).

Thanks! I see the change and am using the 0.0.4 but still hit that issue. Any thoughts on where I could look deeper?

Most likely the right version is not getting picked up. If you are using Workbench and cloud compile, make sure the version number in project.properties is correct as that is used instead of the version in lib.

Otherwise, if you target 5.x it should build correctly, as the problem is specifically in 4.x and earlier versions. However I tested 4.1.0, 3.3.0, and 2.1.0 on the Argon and example 1 did build for me.

Thank you Rick, that was the problem. I am using Web IDE and now figured out how to select the correct version of the library. No more compile error. Thank you!!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.