PDM working example

viznu3391 gravatar image

asked 2017-05-19 14:37:40 +0200

hi, Is there any working PDM example for nRF52832? or can anyone give a sample initialization code for PDM with easyDMA using the inbuilt nrf_pdm_drv functions?

Regardss, Vishnu

edit retag flag offensive close delete report spam

2 answers

Sort by » oldest newest most voted
sigurdon gravatar image

answered 2017-05-22 15:54:47 +0200

updated 2017-05-29 13:35:41 +0200


We don't have any full examples yet. But here is some code to get you started:

#include "nrf_drv_pdm.h"


// <o> PDM Decimation Filter Gain <0x00-0x50>
// <i> For details on the PDM decimation filter, see the 'Decimation filter' section in the nRF52 Product Specification document.
#define CONFIG_PDM_GAIN 0x28

// <o> PDM CLK Pin
#define CONFIG_IO_PDM_CLK 0x06

// <o> PDM DATA Pin
#define CONFIG_IO_PDM_DATA 0x05

// <o> PDM Microphone Power Control Pin

/**@brief Audio buffer handler. */
typedef void (*drv_audio_buffer_handler_t)(int16_t *p_buffer, uint16_t samples);

static int16_t                      m_pdm_buff[2][CONFIG_PDM_BUFFER_SIZE_SAMPLES];

static drv_audio_buffer_handler_t   m_buffer_handler;

static void m_audio_buffer_handler(int16_t *p_buffer, uint16_t samples)
    // Audio processing

static void drv_audio_pdm_event_handler(uint32_t *p_buffer, uint16_t length)
    m_buffer_handler((int16_t *)p_buffer, length);

uint32_t drv_audio_init(drv_audio_buffer_handler_t buffer_handler)
    nrf_drv_pdm_config_t pdm_cfg = NRF_DRV_PDM_DEFAULT_CONFIG(CONFIG_IO_PDM_CLK,

    if (buffer_handler == NULL)

    m_buffer_handler    = buffer_handler;
    pdm_cfg.gain_l      = CONFIG_PDM_GAIN;
    pdm_cfg.gain_r      = CONFIG_PDM_GAIN;

    return nrf_drv_pdm_init(&pdm_cfg, drv_audio_pdm_event_handler);

 * @brief Function for application main entry.
int main(void)
    uint32_t err_code;

    err_code = drv_audio_init(m_audio_buffer_handler);

    err_code =  nrf_drv_pdm_start();
    while (true)
        // Do nothing.
/** @} */

I also recommend taking a look at the PDM driver. It's located in SDK_folder\components\drivers_nrf\pdm

edit flag offensive delete publish link more


Thanks Sigurd. That helped me a lot. The program executes into the "drv_audio_pdm_event_handler" but I am unable to capture any data from the "p_buffer". It returns either zero sometimes or 0xFFF8FFF8. I am using SPK0838HT4H PDM microphone in my application. The configuration is mono. I have made the(L/R)select pin as ground(which is R). The configuration is set as same as above. I have changed the frequency with all the three options. But there is no convincing result. What are the things I should look into?

Vishnu Pradeep ( 2017-05-29 13:05:52 +0200 )editconvert to answer

Are you samling data on the correct edge of PDM_CLK?

Try to set pdm_cfg.edge = NRF_PDM_EDGE_LEFTRISING; in drv_audio_init()

Sigurd ( 2017-05-29 13:48:57 +0200 )editconvert to answer

edge settings is currently default NRF_PDM_EDGE_LEFTFALLING, which is what the application note says. I tried with NRF_PDM_EDGE_LEFTRISING as well but similar result. It is going into the drv_audio_pdm_event_handler. What is the significance of CONFIG_PDM_BUFFER_SIZE_SAMPLES ?

Vishnu Pradeep ( 2017-05-29 14:05:25 +0200 )editconvert to answer

I am not able to move any further from this. Is there anything I can check with?

Vishnu Pradeep ( 2017-05-30 06:23:56 +0200 )editconvert to answer

Do you see any difference if you change the Right/Left output gain adjustment(CONFIG_PDM_GAIN) ?

Sigurd ( 2017-05-30 10:40:30 +0200 )editconvert to answer

I've written code to send the PDM buffer through BLE using ble_app_uart program. I am getting some data for sometime and then it is giving a constant value. If I change the "CONFIG_PDM_BUFFER_SIZE_SAMPLES" to higher value,say 2000, it becomes constant value early, like with 25 samples. If the "CONFIG_PDM_BUFFER_SIZE_SAMPLES" is set as 20 or 2 I am getting around 35 samples and after that it is giving out constant values! Is it related to EasyDMA?

Vishnu Pradeep ( 2017-05-31 12:55:02 +0200 )editconvert to answer

We have now released Nordic Thingy:52, with a Thingy SDK. The Thingy uses a digital PDM microphone, so a part of this SDK is PDM audio drivers with audio codecs. I recommend taking a look at this part of the Thingy SDK.

Sigurd ( 2017-06-02 10:59:25 +0200 )editconvert to answer
bstromquist gravatar image

answered 2017-06-02 19:28:06 +0200

I am experiencing the same issue. As far as I can tell, my code configures the PDM driver the same way your thingy API does. Like Vishnu, I have tried many combinations of configuration parameters. Are there SDK versions where this did not work or was unsupported?

edit flag offensive delete publish link more


Could be an issue with how you handle the buffers. Take a look at how this is done in the Thingy SDK. I.e. take a look at the m_audio_buffer_handler() and m_audio_process() functions in drv_mic.c.

Sigurd ( 2017-06-06 15:05:38 +0200 )editconvert to answer

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

Question Tools

1 follower


Asked: 2017-05-19 14:37:40 +0200

Seen: 276 times

Last updated: Jun 02