Solutions for multiple sensors serial stream, Arduino Nano Sense

I am doing a project that needs to stream both IMU and Microphone from Arduino.
There is a closed post that has a similar question: Simultaneous Data recording on Nano 33 BLE Sense, however, there is no concrete solution.
I use the official sample code of Arduino Nano BLE Sense and use Serial.write to transmit.
My microphone serial codes (IMU code is very similar):

#include <PDM.h>

// default number of output channels
static const char channels = 1;

// default PCM output frequency
static const int frequency = 16000;

// Buffer to read samples into, each sample is 16-bits
short sampleBuffer[512]; 
uint8_t sampleBuffer_8bit[1536]; 
int sb_index = 0;

// Number of audio samples read
volatile int samplesRead;

void setup() {
  SerialUSB.begin(115200);
  while(!SerialUSB);

  // Configure the data receive callback
  PDM.onReceive(onPDMdata);

  // Optionally set the gain
  // Defaults to 20 on the BLE Sense and 24 on the Portenta Vision Shield
   PDM.setGain(5);


  if (!PDM.begin(channels, frequency)) {
    SerialUSB.println("Failed to start PDM!");
    while (1);
  }
}

void loop() {
  // Wait for samples to be read
  while(!SerialUSB);
  
  if (samplesRead) {

    // Conversion 16bit sample to 8 bit sample, adding \n 
    for (int i = 0; i < samplesRead; i=i+1) {

      sampleBuffer_8bit[sb_index] = '\n';
      sb_index ++;
      
      sampleBuffer_8bit[sb_index] = (sampleBuffer[i] >> 8) & 0xFF;
      sb_index ++;
 
      sampleBuffer_8bit[sb_index] = (sampleBuffer[i] & 0xFF);
      sb_index ++;

      //Sending data via SerialUSB
      if (sb_index >= 1536){
 
        SerialUSB.write(sampleBuffer_8bit,sb_index);
 
        sb_index=0;
        }
      

  }

    
        
    // Clear the read count
    samplesRead = 0;
  }
}

/**
 * Callback function to process the data from the PDM microphone.
 * NOTE: This callback is executed as part of an ISR.
 * Therefore using `Serial` to print messages inside this function isn't supported.
 * */
 
void onPDMdata() {
  // Query the number of available bytes
  int bytesAvailable = PDM.available();

  // Read into the sample buffer
  PDM.read(sampleBuffer, bytesAvailable);

  // 16-bit, 2 bytes per sample
  samplesRead = bytesAvailable / 2;

}

I have the following questions:

  1. since the IMU and Microphone have very different sample rate, how to read the data properly?
    My idea: As in Simultaneous Data recording on Nano 33 BLE Sense, we can attach one data from IMU to the Microphone after certain samples (if the sample rate of Mic is 20 times than that of IMU, the interval samples number = 20). However, using such a fixed interval omits the fact that the sample rate is not so stable. Is there a better solution?
  2. Arduino Nano seems only to have one Serial port, how to differentiate the two streams?
    My idea: keep two buffers, one for each sensor. use a header on each buffer to denote the type of sensor. However, I am not sure if it is an efficient solution.

I also think about using thread but seems still abstract to me.
I will post my update on this project.

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