Skip to content

Instantly share code, notes, and snippets.

@ymt117
Last active September 11, 2022 13:04
Show Gist options
  • Save ymt117/343a2ff278fcb3a653687c32e260c55c to your computer and use it in GitHub Desktop.
Save ymt117/343a2ff278fcb3a653687c32e260c55c to your computer and use it in GitHub Desktop.

開発環境

  • Arduino IDE 1.8.19
  • Manjaro Linux

Arduino IDEにボードを追加する

Arduino IDEの上部メニューから「ファイル > 環境設定」を開き、「追加のボードマネージャURL」に下記URLを追加する。

https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json

Arduino IDEの上部メニューから「ツール > ボード > ボードマネージャ」を開き、検索窓に「seeed nrf52」を入力する。

検索結果から「Seeed nRF52 Boards」と「Seeed nRF52 mbed-enabled Boards」をインストールする。

PDMを使う場合は「Seeed nRF52 mbed-enabled Boards」を使うらしい。

It is recommanded to use the Seeed nRF52 mbed-enabled Boards library if you want to use it in embedded Machine Learning Applications or apply "IMU & PDM advanced function".

https://wiki.seeedstudio.com/XIAO_BLE/#two-arduino-libraries

PDMのサンプルスケッチを試す

Arduino IDEの上部メニューから「ファイル > スケッチ例 > (Seeed XIAO BLE Sence - nRF52840用のスケッチ例の)PDM > PDMSerialPlotter」を開く。

サンプルスケッチが開けたら、上部メニューから「ツール > ボード > Seeed nRF52 mbed-enabled Boards > Seeed XIAO BLE Sence - nRF52840」を選択する。

マイコンをPCをに接続して、上部メニューから「ツール > シリアルポート > /dev/ttyACM0(Seeed XIAO BLE - nRF52840)」を選択する。

「/dev/ttyACM0(Seeed XIAO BLE - nRF52840)」の部分は環境によって異なるかも。

上部メニューから「スケッチ > マイコンボードに書き込む」でプログラムを書き込む。

書き込みが完了できたらArduino IDE下部のターミナルに下記のように出力されるはず。

最大811008バイトのフラッシュメモリのうち、スケッチが84728バイト(10%)を使っています。
最大237568バイトのRAMのうち、グローバル変数が45040バイト(18%)を使っていて、ローカル変数で192528バイト使うことができます。
Upgrading target on /dev/ttyACM0 with DFU package /tmp/arduino_build_747325/PDMSerialPlotter.ino.zip. Flow control is disabled, Single bank, Touch disabled
########################################
########################################
########################################
########################################
######
Activating new firmware
Device programmed.

Arduino IDEの上部メニューから「ツール > シリアルプロッタ」を開く。 シリアルプロッタの画面が開くので、左下のボーレートの設定を「9600 bps」にする。

シリアルプロッタの画面に波形が出力されるはず。

マイコンに口を近づけて「あー」とか「いー」って言えば、それぞれそれっぽい波形が出ると思います。

BLEを試す

Arduino IDEの上部メニューから「スケッチ > ライブラリをインクルード > ライブラリを管理」でライブラリマネージャを開く。

検索窓に「ArduinoBLE」を入力する。 検索から「ArduinoBLE」をインストールする。

Arduino IDEの上部メニューから「ファイル > 新規ファイル」で新規ファイルを作成する。

下記スケッチを書き込む。 (BLE接続すると1秒毎にLED点滅&0からインクリメントしてその値を送信するスケッチ)

#include <ArduinoBLE.h>
 
BLEService bleService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
 
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
 
const int ledPin = LED_BUILTIN; // pin to use for the LED
 
void setup() {
  Serial.begin(9600);
  while (!Serial);
 
  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);
 
  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");
 
    while (1);
  }
 
  // set advertised local name and service UUID:
  BLE.setDeviceName("LED");
  BLE.setAdvertisedService(bleService);
 
  // add the characteristic to the service
  bleService.addCharacteristic(switchCharacteristic);
 
  // add service
  BLE.addService(bleService);
 
  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);
 
  // start advertising
  BLE.advertise();
 
  Serial.println("BLE start");
}
 
void loop() {
  // listen for Bluetooth® Low Energy peripherals to connect:
  BLEDevice central = BLE.central();
 
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());
 
    uint8_t sendValue = 0;
    boolean led_state = false;

    // while the central is still connected to peripheral:
    while (central.connected()) {
      led_state = !led_state;
      digitalWrite(ledPin, led_state);
      switchCharacteristic.writeValue(sendValue);
      sendValue++;
      delay(1000);
    }
 
    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

下記URLの「Control built-in LED using a smartphone」に記載されている「LightBlue」というアプリを使って接続する。

https://wiki.seeedstudio.com/XIAO-BLE-Sense-Bluetooth-Usage/#control-built-in-led-using-a-smartphone

アプリから接続して「READ VALUES」の「Read again」をタップするとマイコンから送信された値が表示される。

switchCharacteristic.writeValue()で数値が送れるので、PDMのサンプルと組み合わせて値送れるかと思います。

補足

シリアルポートに表示されなくなったり、書き込めなくなったりしたとき

リセットボタンを素早く2回押せば書き込みモードに切り替わるので解決するはず。 リセットボタンの位置はTypeCコネクタの隣にある(詳しくはこちらを参照)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment