PDM Gain in Arduino examples

The PDM mic gain is being set incorrectly in the Arduino examples. Currently PDM.setGain() is called before PDM.begin(). When it is done in this order, it doesn’t appear to change the mic levels. However, if you call setGain() after begin(), the levels change. This can be easily tested out using the PDMSerialPlotter(). I am not sure if this impacts performance at all though, since the audio gets normalized anyhow.

PDM.setBufferSize(4096);

// initialize PDM with:
// - one channel (mono mode)
// - a 16 kHz sample rate
if (!PDM.begin(1, EI_CLASSIFIER_FREQUENCY)) {
    ei_printf("Failed to start PDM!");
    microphone_inference_end();

    return false;
}
PDM.setGain(80);

HERO!

We’ve been looking at the Nano gain for quite a bit when we did the first port and never realized why this wasn’t working. Result is low volume when recording which can be fixed now, and I think it’ll help a bit with inferencing too.

cc @Arjan

Haha! yea, this one was bugging me for a while because it didn’t seem like the gain had any effect. I happened to just try swapping the functions order on a whim. However, I just saw this weekend that the TF Micro speech example sets the Gain after the Begin:

That was exactly what I have been also looking for. Thank you!

One more question. How do you tweak the mic gain when using the edge-impulse-deamon to record audio data? Are you using a custom code to collect the wav recordings in order to train the neural net?

@anasvaf You’d have to tweak https://github.com/edgeimpulse/firmware-arduino-nano-33-ble-sense and rebuild for now. We’ll fix it in the next few days.

Thank you for the prompt response! I appreciate it :slight_smile:
By the way, in my case i placed the gain at “/src/sensors/ei_microphone.cpp” after the PDM.begin() function in order to make it work

1 Like

I have a couple of changes for the nano33 edge firmware, one of which is the PDM gain fix mentioned previously. Building and flash on MacOS with arduino_build.sh results in an mbed crash.

No changes made to any code, just freshly cloned.

The binary created this way is 3/5ths the size of the bin used in flash_mac.command.

I have a FTDI connected so I can see the crash output. Can that be used to narrow down the crash stack ?

Just realized I am getting this warning.

You’re using an untested version of Arduino CLI, this might cause issues (found: 0.18.0, expected: 0.13.x)

Looking into a downgrade but I’m not sure it’s possible.

Hey I think it’s the Arduino core. There’s an issue with newer cores around serial usage which leads to this.

Can you switch the core version to the one mentioned in the build script? I think 1.3.2 (on mobile can’t check right now!)

But thanks so much, really awesome work! Cc @Arjan

Hi @louis651,

Arduino CLI 0.18.0 works fine as well. The mbed core is the issue. And indeed mbed core 1.3.2 fixes this.

1 Like

That is absolutely the problem. I had mbed core previously installed at 2.1.0.

The current repo will install 1.1.6 if it’s not already installed and that does work.

1.3.2 also worked.

Thanks!

1 Like

I am new to Edge Impulse and nano 33 ble. The problem is that it captures audio from the nano 33’s microphone at a very low level that is almost indistinguishable from noise and is not suitable for training the model. Please tell me how to fix it. Thank you.

@Robotastic post for the code is the fix. I changed ei_microphone.cpp to set the gain after PDM.begin() for my purposes. I don’t think it is fixed yet in the edge impulse repository, @janjongboom can verify or you can take a look at the edge impulse github repository to see if it is updated.

@louis651 @tototek We’ll release a fix on Wednesday for this!

I wait for the fix.Thanks for the help

Hi @tototek this has now been released. You can get new firmware from the Nano 33 BLE Sense page, and the examples are updated when you export a new Arduino library from the Deployment page in the Studio. :rocket:

1 Like

Update all the software and it works fine. Why is the nano mic capture level still low? I attach sample (acquisition 10 cm from mic)

@tototek Yeah I see the same, investigating what went wrong here!

edit: I’ve pushed the firmware to CDN again, can you retry? The bin file in the folder should be ~344K in size.

OK. Now the audio level in the acquisition works correct.Thank you.
But now it happens to me that when acquiring data from the microphone it gives me a timeout error and I have to exit the edgeimpulse daemon and log in again.
On the nano33 ble, the yellow led is flashing, it is waiting to read from the device .
Attachment capture:
Incoming sampling request {
path: ‘/ api / training / data’,
label: ‘pit22’,
length: 10000,
interval: 0.0625,
hmacKey: ‘ca3e5c5363bc82053fb969f449cf82de’,
sensor: ‘Built-in microphone’
}
[SER] Configured upload settings
[SER] Sampling started
[SER] Sampling done
[SER] Device not connected to WiFi directly, reading from buffer (bytes 0 - 323828, expecting to read ~ 431770 bytes …
Failed to sample data Timeout when waiting for> (timeout: 130000) AT + READBUFFER = 0.323828
All my software is update.In my humble opinion, the serial port capture stop order fails.I have tried it with different sample length 8.9,10 sec.