Xiao nrf52840 をAndroidスマホで制御 Part1 (Part2は後日)

Craft Saitoの工作室


ゴールはスマホで制御するラジコンカーです。とりあえず、Part1です。
Part2(Androidのプログラミング)は後日アップ予定です。

動画をご覧ください。

www.youtube.com

1. seeed xiao nRF52840について

親指サイズの多機能マイコンボード
私は sense機能 無しバージョンを秋月電子通商で約2,000円で購入しました

nrf52840 BLE control by Pixel4a

Amazonでも購入可能です

半田ごてはこれをお勧めします

詳しくは下のリンクをクリックしてください
wiki.seeedstudio.com



2.1 IDEのダウンロードとインストール

Arduino IDE を下のサイトからダウンロードします
ここからダウンロード、インストール
www.arduino.cc

2.2 サンプルプログラム(ライブラリ)

GitHubのサーチボックスで”Arduino BLE”で検索してダウンロード
(ダウンロードするにはサインインが必要です)
github.com


次に、ダウンロードしたZipファイル(ライブラリ)をArduinoIDEにインクルードします

AddingZipLibraryToArduino


さらに、Arduino BLE by Arduinoをインストールします

InstallingLibrary


サンプルプログラムを開きます
File -> Examples -> ArduinoBLE -> Peripheral -> LED

SampleProgramLED

2.3 プログラムの変更

元のプログラムはLEDのON/OFFが逆なので、その変更と、if文をswitch caseに変更しました。とりあえず、LEDのON/OFF(0x12でON、0x32でOFF)だけのプログラムです。次の通りです。


#include <ArduinoBLE.h>
 
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service 10Feb
 
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central


// Modified ON/OFF logic and changed if... to switch case   Feb2023


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.setLocalName("LED");
  BLE.setAdvertisedService(ledService);
 
  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);
 
  // add service
  BLE.addService(ledService);
 
  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);
 
  // start advertising
  BLE.advertise();
 
  Serial.println("BLE LED Peripheral");
  digitalWrite(ledPin, LOW); // changed from HIGH to LOW       


}
 
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());
 
    // while the central is still connected to peripheral:
  while (central.connected()) {
        if (switchCharacteristic.written()) {


          uint8_t switchvalue = switchCharacteristic.value();
          switch (switchvalue) {


          case 0x12:    //  ==== Go forward ====
            Serial.println("Go forward");
            Serial.println(switchvalue,HEX);
            digitalWrite(ledPin, LOW);
            break;


          case 0x32:    //  ==== Go back ====
            Serial.println("Go back");
            Serial.println(switchvalue,HEX);
            digitalWrite(ledPin, HIGH);
            break;


          case 0x21:    //  ==== Left ====
            Serial.println(F("Turn left"));
            Serial.println(switchvalue,HEX);
            break;


          case 0x22:    //  ==== Stop ====
            Serial.println(F("Stop"));
            Serial.println(switchvalue,HEX);
            break;


          case 0x23:    //  ==== Right ====
            Serial.println(F("Turn right"));
            Serial.println(switchvalue,HEX);
            break;


          }
        }
      }
 
    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

3. 参考記事

参考にさせていただきました。ありがとうございます。
lipoyang.hatenablog.com