MakerGram Logo

    MakerGram

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    • Users
    • Groups

    How to capture audio using PDM mic and send that data to central device using BLE in seeedstudio Axio nrf52840 sense with inbuilt BLE and tinyml functionality

    TinyML
    2
    3
    305
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      amalkrishnam3 last edited by

      I have been working on a project . In which I want to capture data using the PDM mic and trasmit it through the BLE available . I am new to hardware and these sensors so I am unable to make sense of the data I am reciving on the other side(central) that is my mobile device(iphone X) with BlueLight application. I am able to find a service and I am reciving some form of hex code,but I don't know how to convert it to wave files or don't know whether it's possible or not .

      If you guys have any idea or any resources that could help please do share.

      I am attaching my code below :

      #include <ArduinoBLE.h>
      #include <mic.h> //  microphone library 
      
      // Microphone Settings
      #define DEBUG 1
      #define SAMPLES 800
      
      mic_config_t mic_config = {
        .channel_cnt = 1,
        .sampling_rate = 16000,
        .buf_size = 1600,
        .debug_pin = LED_BUILTIN
      };
      
      NRF52840_ADC_Class Mic(&mic_config);
      int16_t recording_buf[SAMPLES];
      volatile static bool record_ready = false;
      
      // Updated UUIDs
      #define SERVICE_UUID "19B10000-E8F2-537E-4F6C-D104768A1214"
      #define CHARACTERISTIC_UUID_AUDIO "19B10001-E8F2-537E-4F6C-D104768A1214"
      
      // BLE Service and Characteristic
      BLEService audioService(SERVICE_UUID);
      // Corrected initialization with explicit value size and fixed length flag
      BLECharacteristic audioDataCharacteristic(CHARACTERISTIC_UUID_AUDIO, BLERead | BLENotify | BLEWrite, sizeof(recording_buf), true);
      
      void setup() {
        Serial.begin(115200);
        while (!Serial) delay(10);
      
        Serial.println("Initializing microphone...");
        Mic.set_callback(audio_rec_callback);
        if (!Mic.begin()) {
          Serial.println("Mic initialization failed");
          while (1);
        }
        Serial.println("Mic initialized.");
      
        Serial.println("Initializing BLE...");
        if (!BLE.begin()) {
          Serial.println("Failed to start BLE!");
          while (1);
        }
      
        BLE.setLocalName("SCT Audio");
        BLE.setAdvertisedService(audioService);
        
        audioService.addCharacteristic(audioDataCharacteristic);
        BLE.addService(audioService);
      
        // Corrected writeValue call with explicit casting
        audioDataCharacteristic.writeValue((uint8_t)0);
        
        BLE.advertise();
        Serial.println("BLE Peripheral is now advertising");
      }
      
      void loop() {
        BLEDevice central = BLE.central();
      
        if (central) {
          Serial.println("Connected to central device");
          
          while (central.connected()) {
            if (record_ready) {
              // Plot the audio data in the Serial Plotter
              for (int i = 0; i < SAMPLES; i++) {
                Serial.println(recording_buf[i]);
              }
      
              // Transmit the audio data
              audioDataCharacteristic.writeValue((uint8_t*)recording_buf, 2 * SAMPLES);
              Serial.println("Audio data transmitted over BLE");
              record_ready = false;
            }
          }
      
          Serial.println("Disconnected from central device");
        }
      }
      
      static void audio_rec_callback(uint16_t *buf, uint32_t buf_len) {
        static uint32_t idx = 0;
        
        for (uint32_t i = 0; i < buf_len; i++) {
          recording_buf[idx++] = buf[i];
          if (idx >= SAMPLES){ 
            idx = 0;
            record_ready = true;
            break;
          } 
        }
      }
      
      
      salmanfaris 1 Reply Last reply Reply Quote 1
      • salmanfaris
        salmanfaris @amalkrishnam3 last edited by

        Hi @amalkrishnam3, Were you able to make any progress?

        A 1 Reply Last reply Reply Quote 0
        • A
          amalkrishnam3 @salmanfaris last edited by

          @salmanfaris No, I haven't made any progess on the matter

          1 Reply Last reply Reply Quote 0
          • First post
            Last post

          Recent Posts

          • @salmanfaris Thank you Salman, This solution worked for me.

            • read more
          • @Abhay Can you try one more method

            Downloaded both boards, “Seeed nRF52 Boards” and “Seeed nRF52 mbed-enabled Boards.”

            Copy the adafruit-nrfutil from

            /Users/<user>/Library/Arduino15/packages/Seeeduino/hardware/mbed/2.9.2/tools/adafruit-nrfutil/macos/adafruit-nrfutil

            to

            /Users/<user>/Library/Arduino15/packages/Seeeduino/hardware/nrf52/1.1.8/tools/adafruit-nrfutil/macos/adafruit-nrfutil

            Then chmod -x the copied file

            Please check and let me know. Src

            • read more
          • @salmanfaris

            I was using "Seeed nRF52 Boards: 1.1.8" and the issue was there. Followed the steps and installed the packages and board you mentioned and the issue is still there. I changed the board from Seeed nRF to Adafruit nRF and issue is also showing for Adafruit nRF

            • read more
          • @Abhay said in Compilation Error for Seeed XIAO nRF52840 Sense in Arduino IDE on macOS:

            Seeed nRF52 mbed-enabled Boards: 2.9.2
            Seeed nRF52 Boards: 1.1.8

            @Abhay, are you getting the error while selecting both boards? You can also try using "Seeed nRF52 Boards: 1.1.8".

            Since it's nRF series can you try to install the - adafruit-nrfutil pip package and see!

            also try to install - Adafruit Feather nRF52 using board manager and check again.

            • read more
          • Compilation Error for Seeed XIAO nRF52840 Sense in Arduino IDE on macOS

            Hi everyone,

            I am encountering an issue when compiling a program for the Seeed XIAO nRF52840 Sense in the Arduino IDE on macOS. Below are the details of the problem:

            System Details: Mac Model: MacBook Air M2 macOS Version: Sequoia 15.2 Arduino IDE Version: 2.3.4 Locale: UTF-8 (already configured) Board manager details: Additional board Manager URL: https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json Board installed: Seeed nRF52 mbed-enabled Boards: 2.9.2 Seeed nRF52 Boards: 1.1.8 Steps to Reproduce: Open Arduino IDE on macOS. Use the default program:// the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } Select Seeed XIAO nRF52840 Sense as the board. Click on "Verify" or "Compile." Observed Behavior:

            The compilation fails with the following error:

            Traceback (most recent call last): File "__main__.py", line 317, in <module> File "click/core.py", line 1134, in __call__ File "click/core.py", line 1040, in main File "click/_unicodefun.py", line 100, in _verify_python_env RuntimeError: Click will abort further execution because Python was configured to use ASCII as encoding for the environment. Consult https://click.palletsprojects.com/unicode-support/ for mitigation steps. This system lists some UTF-8 supporting locales that you can pick from. The following suitable locales were discovered: af_ZA.UTF-8, am_ET.UTF-8, be_BY.UTF-8, bg_BG.UTF-8, ca_ES.UTF-8, cs_CZ.UTF-8, da_DK.UTF-8, de_AT.UTF-8, de_CH.UTF-8, de_DE.UTF-8, el_GR.UTF-8, en_AU.UTF-8, en_CA.UTF-8, en_GB.UTF-8, en_IE.UTF-8, en_NZ.UTF-8, en_US.UTF-8, es_ES.UTF-8, et_EE.UTF-8, eu_ES.UTF-8, fi_FI.UTF-8, fr_BE.UTF-8, fr_CA.UTF-8, fr_CH.UTF-8, fr_FR.UTF-8, he_IL.UTF-8, hr_HR.UTF-8, hu_HU.UTF-8, hy_AM.UTF-8, is_IS.UTF-8, it_CH.UTF-8, it_IT.UTF-8, ja_JP.UTF-8, kk_KZ.UTF-8, ko_KR.UTF-8, lt_LT.UTF-8, nl_BE.UTF-8, nl_NL.UTF-8, no_NO.UTF-8, pl_PL.UTF-8, pt_BR.UTF-8, pt_PT.UTF-8, ro_RO.UTF-8, ru_RU.UTF-8, sk_SK.UTF-8, sl_SI.UTF-8, sr_YU.UTF-8, sv_SE.UTF-8, tr_TR.UTF-8, uk_UA.UTF-8, zh_CN.UTF-8, zh_HK.UTF-8, zh_TW.UTF-8 [15256] Failed to execute script __main__ exit status 1 Compilation error: exit status 1

            I'm experiencing this issue only with Mac, I tried the same with windows and it ran successfully. I have uninstalled and reinstalled Arduino ide as well.

            • read more
          By MakerGram | A XiStart Initiative | Built with ♥ NodeBB
          Copyright © 2023 MakerGram, All rights reserved.
          Privacy Policy | Terms & Conditions | Disclaimer | Code of Conduct