Nano 33 BLE Senseをデータ入力に使う<BLE>⑤音センサ<BLEペリフェラル>

 ここまで、複数搭載されているセンサのうち、

を扱いました。ここではマイクMP34DT05を使います。

 本マイコン・ボードをインストールしたとき、PDMライブラリとサンプル・スケッチPDMSerialPlotterが入っています。コンパイルしてシリアルプロッタを立ち上げると、正しく動作します。

MP34DT05のおもなスペック

  • 動作電圧  1.6~3.6V
  • 感度 -26dBFS
  • S/N比 64dB
  • クロック・レート 1.2〜3.25MHz

接続

 USBケーブルで、PCと接続します。

サンプルPDMSerialPlotter

 実行します。

 変化の度合いが激しいので、BLE用に30回の移動平均をとるように変更しました。交流データなので、正しくデータ処理をしていません。めやすです。


#include <PDM.h>

static const char channels = 1;  // default number of output channels
static const int frequency = 16000;  // default PCM output frequency
short data[512];
short sampleBuffer[512];  // Buffer to read samples into, each sample is 16-bits
volatile int samplesRead;  // Number of audio samples read

void setup() {
  Serial.begin(9600);
  while (!Serial);
  
  PDM.onReceive(onPDMdata);  // Configure the data receive callback
  // Initialize PDM with:
  // one channel (mono mode),a 16 kHz sample rate
  if (!PDM.begin(channels, frequency)) {
    Serial.println("Failed to start PDM!");
    while (1);
  }
}

void loop() {
  // Wait for samples to be read
  if (samplesRead) {
    for (int i = 0; i < samplesRead; i++) {
      data[i] =(sampleBuffer[i-29] + sampleBuffer[i-28] + sampleBuffer[i-27] + sampleBuffer[i-26] + sampleBuffer[i-25] + sampleBuffer[i-24] + sampleBuffer[i-23] + sampleBuffer[i-22] + sampleBuffer[i-21] + sampleBuffer[i-20] 
              + sampleBuffer[i-19] + sampleBuffer[i-18] + sampleBuffer[i-17] + sampleBuffer[i-16] + sampleBuffer[i-15] + sampleBuffer[i-14] + sampleBuffer[i-13] + sampleBuffer[i-12] + sampleBuffer[i-11] + sampleBuffer[i-10] 
              + sampleBuffer[i-9] + sampleBuffer[i-8] + sampleBuffer[i-7] + sampleBuffer[i-6] + sampleBuffer[i-5] + sampleBuffer[i-4] + sampleBuffer[i-3] + sampleBuffer[i-2] + sampleBuffer[i-1] + sampleBuffer[i])
                /30; 
      Serial.print(data[i]);Serial.print(",");
      Serial.println(sampleBuffer[i]);
    }
    // Clear the read count
    samplesRead = 0;
  }
}

void onPDMdata() {  //  Callback function to process the data from the PDM microphone.
  int bytesAvailable = PDM.available();  // Query the number of available bytes
  PDM.read(sampleBuffer, bytesAvailable);  // Read into the sample buffer
  samplesRead = bytesAvailable / 2;  // 16-bit, 2 bytes per sample
}

 青色が平均の出力です。

スケッチ

 BLEペリフェラルのスケッチを追加します。onPDMdata() 関数で音声データは読み取り作業なしでどんどんsampleBufferに取り込まれています。データは交流なので、絶対値をとって音圧データとしてBLEで送ります。データ自体は、瞬間の値です。


#include <PDM.h>
#include <ArduinoBLE.h>

// MP34DT05 Mic
static const char channels = 1;  // default number of output channels
static const int frequency = 16000;  // default PCM output frequency
short sampleBuffer[256];  // Buffer to read samples into, each sample is 16-bits

// BLE Service
#define Nano33BLESense_Service3_UUID   "F000AA50-0451-4000-B000-000000000000"
BLEService Nano33BLESense_Service3(Nano33BLESense_Service3_UUID);

// BLE  Characteristic
#define Mic_Characteristic_UUID       "F000AA51-0451-4000-B000-000000000000"
BLEIntCharacteristic    Mic(Mic_Characteristic_UUID, BLERead | BLENotify);

// BLE  Descriptor
#define Mic_Descriptor_UUID       "2901"
BLEDescriptor   Mic_Descriptor(Mic_Descriptor_UUID, "Mic level");

#define localNAME  "MP34DT05"
#define DeviceNAME "MP34DT05_Mic_BLE"

void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Mic test");
  
  PDM.onReceive(onPDMdata);  // Configure the data receive callback
  // Initialize PDM with:
  // one channel (mono mode),a 16 kHz sample rate
  if (!PDM.begin(channels, frequency)) {
    Serial.println("Failed to start PDM!");
    while (1);
  }
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }

  BLE.setLocalName(localNAME);
  BLE.setDeviceName(DeviceNAME);

  //// set the service3
  BLE.setAdvertisedService(Nano33BLESense_Service3);

  // add characteristic
  Nano33BLESense_Service3.addCharacteristic(Mic);

  // add descriptor
  Mic.addDescriptor(Mic_Descriptor);

  // add service
  BLE.addService(Nano33BLESense_Service3);

  // start advertising
  BLE.advertise();
  Serial.println("\nBluetooth device active, waiting for connections...");
}

void loop() {
  // wait for a BLE central
  BLEDevice central = BLE.central();
  // if a central is connected to the peripheral:
  if (central) {
    delay(100);
    Serial.print("\nConnected to central: ");
    // print the central's BT address:
    Serial.println(central.address());
    while (central.connected()) {
        updateValue();
        delay(200);
    }
    // when the central disconnects
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
    goto brout;
  }
  brout: ;
  delay(1000);
}

void updateValue() {
  Serial.print("  ");Serial.println(sampleBuffer[0]);
  int valueof_Mic = abs(sampleBuffer[1]);
  // update  characteristic
  Mic.writeValue(valueof_Mic);
}

void onPDMdata() {  //  Callback function to process the data from the PDM microphone.
  int bytesAvailable = PDM.available();  // Query the number of available bytes
  PDM.read(sampleBuffer, bytesAvailable);  // Read into the sample buffer
}

 シリアルモニタの様子です。

 RLS10のBLEセントラルの画面です。

前へ

Nano 33 BLE Senseをデータ入力に使う<BLE>④近接センサ<BLEセントラル>

次へ

Nano 33 BLE Senseをデータ入力に使う<BLE>⑥音センサ<BLEセントラル>