Digital Microphones and Arduino



  • I would like to use the record example from Arduino IDE to check the sound on my digital microphones. What changes do I need to make to the sketch to have it work for 2 digital microphones in D01?


  • DeveloperWorld

    Hi @atw72

    Have you seen the https://developer.sony.com/develop/spresense/developer-tools/hardware-documentation#_how_to_connect_digital_microphones ?

    Looking in the audio driver of the I found the following:

    
    err_t setRecorderMode(
          uint8_t input_device, /**< Select input device. AS_SETRECDR_STS_INPUTDEVICE_MIC or
                                     AS_SETRECDR_STS_INPUTDEVICE_I2S. */
          int32_t input_gain,   /**< Input gain : value range
                                     Analog Mic  -7850:-78.50dB, ... , -5:-0.05dB, 0:0dB, 5:+0.5dB, ... , 210:+21.0dB
                                     Digital Mic -7850:-78.50dB, ... , -5:-0.05dB, 0:0dB (Max is 0dB.)
                                     set #AS_MICGAIN_HOLD is keep setting. */
          uint32_t bufsize,     /**< Buffer size of recorder. */
          bool is_digital       /**< Select mic type. true:Digital, false:Analog. */
      );
    

    If you could try to change the call to theAudio->setRecorderMode in your example to specify that you want to use a Digial microphone by setting the is_digital parameter.

    Please have a look and see if it helps you.



  • Hi there!

    I did see the link above. I made those changes to the Spresense board and now have digital microphones connected to the board. Under the Spresense Arduino IDE development, I was trying to use the recorder sketch that is under the Audio Library at this link: https://developer.sony.com/develop/spresense/developer-tools/get-started-using-arduino-ide/developer-guide#_example_audio_sketches. There is a reference to a link that says setting of "MIC channel select map" for SDK configuration information on how to use digital microphones, however, the link just goes back to the top of the page. Do you know what changes I have to make to the SDK to use the recorder example but with digital microphones?

    Thank you!





  • @TE-KarlKomierowski I gave this line a try:

    theAudio->setRecorderMode(AS_SETRECDR_STS_INPUTDEVICE_MIC, AS_MICGAIN_HOLD, 68, true);

    I received this error on the serial monitor.

    initialization Audio Library
    Init Recorder!
    Recording Start!
    Attention: module[4][0] attention id[1]/code[6] (objects/media_recorder/audio_recorder_sink.cpp L84)

    Attention: module[4][0] attention id[1]/code[6] (objects/media_recorder/audio_recorder_sink.cpp L84)


  • DeveloperWorld

    Hi @atw72

    I checked the code in media_recorder/audio_recorder_sink.cpp
    and the warning on line 84 is in this function AudioRecorderSink::write this:
    AS_ATTENTION_SUB_CODE_SIMPLE_FIFO_OVERFLOW

    Attention: module[4][0] attention id[1]/code[6] (objects/media_recorder/audio_recorder_sink.cpp L84)

    Could you share your sketch with us and it will be easier to see all the settings that have been used when this warning occurred.

    And also, do you use an SD card? What type/brand is it?

    Thank you.
    BR
    Karl



  • Hi @TE-KarlKomierowski, below is the recorder sketch I was using. I am using an SD Card. It's a SanDisk 64GB Ultra microSDXC

    #include <SDHCI.h>
    #include <Audio.h>
    
    SDClass theSD;
    AudioClass *theAudio;
    
    File myFile;
    
    bool ErrEnd = false;
    
    /**
     * @brief Audio attention callback
     *
     * When audio internal error occurc, this function will be called back.
     */
    
    static void audio_attention_cb(const ErrorAttentionParam *atprm)
    {
      puts("Attention!");
      
      if (atprm->error_code >= AS_ATTENTION_CODE_WARNING)
        {
          ErrEnd = true;
       }
    }
    
    /**
     * @brief Setup recording of mp3 stream to file
     *
     * Select input device as microphone <br>
     * Initialize filetype to stereo mp3 with 48 Kb/s sampling rate <br>
     * Open "Sound.mp3" file in write mode
     */
    
    static const int32_t recoding_frames = 400;
    static const int32_t recoding_size = recoding_frames*288; /* 96kbps, 1152sample */
    
    void setup()
    {
      theAudio = AudioClass::getInstance();
    
      theAudio->begin(audio_attention_cb);
    
      puts("initialization Audio Library");
    
      /* Select input device as microphone */
      theAudio->setRecorderMode(AS_SETRECDR_STS_INPUTDEVICE_MIC, AS_MICGAIN_HOLD, 68, true);
    
      /*
       * Initialize filetype to stereo mp3 with 48 Kb/s sampling rate
       * Search for MP3 codec in "/mnt/sd0/BIN" directory
       */
      theAudio->initRecorder(AS_CODECTYPE_MP3, "/mnt/sd0/BIN", AS_SAMPLINGRATE_48000, AS_CHANNEL_STEREO);
      puts("Init Recorder!");
    
      /* Open file for data write on SD card */
      myFile = theSD.open("Sound.mp3", FILE_WRITE);
      /* Verify file open */
      if (!myFile)
        {
          printf("File open error\n");
          exit(1);
        }
    
      theAudio->startRecorder();
      puts("Recording Start!");
    }
    
    /**
     * @brief Record given frame number
     */
    void loop() 
    {
      err_t err;
      /* recording end condition */
      if (theAudio->getRecordingSize() > recoding_size)
        {
          theAudio->stopRecorder();
          sleep(1);
          err = theAudio->readFrames(myFile);
    
          goto exitRecording;
        }
    
      /* Read frames to record in file */
      err = theAudio->readFrames(myFile);
    
      if (err != AUDIOLIB_ECODE_OK)
        {
          printf("File End! =%d\n",err);
          theAudio->stopRecorder();
          goto exitRecording;
        }
    
      if (ErrEnd)
        {
          printf("Error End\n");
          theAudio->stopRecorder();
          goto exitRecording;
        }
    
      /* This sleep is adjusted by the time to write the audio stream file.
         Please adjust in according with the processing contents
         being processed at the same time by Application.
      */
    //  usleep(10000);
    
      return;
    
    exitRecording:
    
      theAudio->closeOutputFile(myFile);
      myFile.close();
      
      theAudio->setReadyMode();
      theAudio->end();
      
      puts("End Recording");
      exit(1);
    }
    

  • DeveloperWorld

    Hi @atw72

    Could you try the following and see what the outcome is?

    #include <SDHCI.h>
    #include <Audio.h>
    
    SDClass theSD;
    AudioClass *theAudio;
    
    File myFile;
    
    bool ErrEnd = false;
    
    
    /**
       @brief Audio attention callback
    
       When audio internal error occurc, this function will be called back.
    */
    
    static void audio_attention_cb(const ErrorAttentionParam *atprm)
    {
      puts("Attention!");
    
      switch (atprm->error_code) {
        case AS_ATTENTION_CODE_INFORMATION:
        case AS_ATTENTION_CODE_WARNING    :
          ErrEnd = false;
          break;
        case AS_ATTENTION_CODE_ERROR      :
        case AS_ATTENTION_CODE_FATAL      :
        default:
          ErrEnd = true;
    
      }
    }
    
    /**
       @brief Setup recording of mp3 stream to file
    
       Select input device as microphone <br>
       Initialize filetype to stereo mp3 with 48 Kb/s sampling rate <br>
       Open "Sound.mp3" file in write mode
    */
    
    static const int32_t recoding_frames = 400;
    static const int32_t recoding_size = recoding_frames * 288; /* 96kbps, 1152sample */
    
    void setup()
    {
      theAudio = AudioClass::getInstance();
    
      theAudio->begin(audio_attention_cb);
    
      puts("initialization Audio Library");
    
      /* Select input device as microphone */
      theAudio->setRecorderMode(AS_SETRECDR_STS_INPUTDEVICE_MIC, AS_MICGAIN_HOLD, 68, true);
      
    
      /*
         Initialize filetype to stereo mp3 with 48 Kb/s sampling rate
         Search for MP3 codec in "/mnt/sd0/BIN" directory
      */
      theAudio->initRecorder(AS_CODECTYPE_MP3, "/mnt/sd0/BIN", AS_SAMPLINGRATE_48000, AS_CHANNEL_STEREO);
      puts("Init Recorder!");
    
      /* Open file for data write on SD card */
      myFile = theSD.open("sound.mp3", FILE_WRITE);
    
        /* Verify file open */
      if (!myFile)
      {
        printf("File open error\n");
        exit(1);
      }
    
      theAudio->startRecorder();
      puts("Recording Start!");
    }
    
    /**
       @brief Record given frame number
    */
    void loop()
    {
      err_t err;
      /* recording end condition */
      if (theAudio->getRecordingSize() > recoding_size)
      {
        theAudio->stopRecorder();
        sleep(1);
        err = theAudio->readFrames(myFile);
    
        goto exitRecording;
      }
    
      /* Read frames to record in file */
      err = theAudio->readFrames(myFile);
    
      if (err != AUDIOLIB_ECODE_OK)
      {
        printf("File End! =%d\n", err);
        theAudio->stopRecorder();
        goto exitRecording;
      }
    
      if (ErrEnd)
      {
        printf("Error End\n");
        theAudio->stopRecorder();
        goto exitRecording;
      }
    
      /* This sleep is adjusted by the time to write the audio stream file.
         Please adjust in according with the processing contents
         being processed at the same time by Application.
      */
      //  usleep(10000);
    
      return;
    
    exitRecording:
    
      theAudio->closeOutputFile(myFile);
      myFile.close();
    
      theAudio->setReadyMode();
      theAudio->end();
    
      puts("End Recording");
      exit(1);
    }
    
    


  • I'm having the same challenge. I'm trying to use 2 digital MEMS microphones on D01 of the expansion board.

    What LRCK pin should I use for this example?



  • I did some research, and it looks like the SPR_I2S0_LRCK pin is not pulled off of the extension board. Without this pin, Digital MEMS microphones can't function.

    Located on page 3, extension board, pin 33
    https://github.com/sonydevworld/spresense-hw-design-files/raw/master/CXD5602PWBEXT1/schematics/CXD5602PWBEXT1_schematics.pdf

    Any advice?


  • DeveloperWorld

    Hi @hypnotoed
    Ii'm looking at the datasheet of CXD5247GF page 11. Also how_to_connect_digital_microphones and I can't see that SPR_I2S0_LRCK is mentioned. Where did you find that you need SPR_I2S0_LRCK?

    Could you share your schematic and type of microphone you are using?

    @hypnotoed said in Digital Microphones and Arduino:

    I did some research, and it looks like the SPR_I2S0_LRCK pin is not pulled off of the extension board. Without this pin, Digital MEMS microphones can't function.

    Located on page 3, extension board, pin 33
    https://github.com/sonydevworld/spresense-hw-design-files/raw/master/CXD5602PWBEXT1/schematics/CXD5602PWBEXT1_schematics.pdf

    Any advice?



  • Thanks for looking into it @TE-KarlKomierowski !

    I'm using an Adafruit breakout of the popular Knowles SPH0645LM4H
    https://www.adafruit.com/product/3421
    https://learn.adafruit.com/adafruit-i2s-mems-microphone-breakout/pinouts

    Generally I2S mics require two clocks, a word clock (or LRCK in this instance) and a bit clock. The bit clock is used to tell the mics how often to transmit, and the word clock (or LRCK) is used to tell which of the two mics to transmit.

    The mainboard has an I2S0_LRCK pin (D25), but I'm not sure how to enable it
    https://developer.sony.com/develop/spresense/docs/introduction_en.html#_main_board


  • DeveloperWorld

    hi @hypnotoed

    Ok I looked into the data sheets of the microphone you have.
    And you are right about the word clock.

    According to the Spresense digital microphone guide I can see that the recommended microphones datasheet work in a different way.
    The left/right data is read on rising/falling edge of the clock line.

    Since Spresense have a LRCK line one would easily think that this mode should be supported too. So i looked into the drivers to see if this way could be enabled but I have not found anything directly obvious, yet.

    Reading more on the subject reveals that the two different techniques are called PDM and I2S. This document describes more about how they work:
    MS-2275.pdf

    Checking the data sheet for CXD5247 starting at page 54 I can't read anything about digital microphones with I2S interface so I don't have too much hope that it would be supported.

    I will check with the people who have more specialist knowledge in what Spresense and the driver are supporting at the moment.

    BR
    Karl

    @hypnotoed said in Digital Microphones and Arduino:

    pinouts
    Generally I2S mics require two clocks, a word clock (or LRCK in this instance) and a bit clock. The bit clock is used to tell the mics how often to transmit, and the word clock (or LRCK) is used to tell which of the two mics to transmit.


  • DeveloperWorld

    Hi @hypnotoed

    I got an answer that I2S mode is only available as "through-out" and not for digital microphones in the current release and will not be made available to the public as for now.

    I saw that Adafruit have PDM microphones too: https://www.adafruit.com/product/3492

    BR
    Karl



  • Thanks for following up @TE-KarlKomierowski ! I'll look into the PDM microphones.


Log in to reply