はじめる

Author:ArduinoV1.5.1

Arduino CLIは、Arduino IDEにあるすべての機能を提供します。いくつかの例を見ていきましょう。

始める前に

arduino-cliはコマンドの集合体で、各コマンドにはそれぞれヘルプテキストがあります。ヘルプテキストは、以下のように、helpコマンドで表示することができます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$ arduino-cli help core
Arduino core operations.

Usage:
  arduino-cli core [command]

Examples:
  arduino-cli core update-index

Available Commands:
  download     Downloads one or more cores and corresponding tool dependencies.
  install      Installs one or more cores and corresponding tool dependencies.
  list         Shows the list of installed platforms.
  search       Search for a core in Boards Manager.
  uninstall    Uninstalls one or more cores and corresponding tool dependencies if no longer used.
  update-index Updates the index of cores.
  upgrade      Upgrades one or all installed platforms to the latest version.

Flags:
  -h, --help   help for core

Global Flags:
      --additional-urls strings   Comma-separated list of additional URLs for the Boards Manager.
      --config-file string        The custom config file (if not specified the default will be used).
      --json                      Print the output in JSON format.
      --log                       Print the logs on the standard output.
      --log-file string           Path to the file where logs will be written.
      --log-format string         The output format for the logs, can be: text, json
      --log-level string          Messages with this level and above will be logged. Valid levels are: trace, debug, info, warn, error, fatal, panic
      --no-color                  Disable colored output.

Use "arduino-cli core [command] --help" for more information about a command.

設定ファイルを作成する

コマンドラインインターフェイスですべての利用可能な機能を提供しているので、Arduino CLIが動作するのに、厳密には設定ファイルは不要です。しかし、設定ファイルを作成すれば、コマンドを実行する際のタイピング量を大幅に節約できるので、作成してみましょう。

1
2
$ arduino-cli config init
Config file written: /home/luca/.arduino15/arduino-cli.yaml

arduino-cli.yamlの内容を見れば、利用可能なオプションとそれぞれのデフォルト値がわかります。詳細は、設定を参照してください。

新しいスケッチを作成する

カレントディレクトリにMyFirstSketchという新しいスケッチを作成するには、以下のコマンドを実行します。

1
2
$ arduino-cli sketch new MyFirstSketch
Sketch created in: /home/luca/MyFirstSketch

スケッチは、ソースファイルやライブラリなどのアセットを含むフォルダです。newコマンドは、定型コードが書かれたMyFirstSketch.inoという.inoファイルを作成します。

1
2
3
4
5
6
$ cat $HOME/MyFirstSketch/MyFirstSketch.ino
void setup() {
}

void loop() {
}

この時点で、好みのエディタやIDEを使い$HOME/MyFirstSketch/MyFirstSketch.inoというファイルを開き、このようにコードを変更することができます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
}

ボードをPCに接続する

新規インストール時に最初に実行するのは、プラットフォームとライブラリのローカルキャッシュの更新です。以下を実行します。

1
2
$ arduino-cli core update-index
Updating index: package_index.json downloaded

ボードとPCをUSBケーブルで接続したら、ボードが認識されたかを以下の実行して確認できます。

1
2
3
$ arduino-cli board list
Port         Type              Board Name              FQBN                 Core
/dev/ttyACM1 Serial Port (USB) Arduino/Genuino MKR1000 arduino:samd:mkr1000 arduino:samd

この例では、MKR1000ボードが認識され、コマンドの出力から、ボードを動作させるには、arduino:samdというプラットフォームコアがインストールされている必要があることがわかります。

Unknownというボードが表示されていても、プラットフォームコアを識別し、正しいFQBNを利用すれば、アップロードは実行可能です。どのような理由にせよ、ボードが検出できなければ、以下を実行することで、サポートされているボードとFQBNを表示することができます。

1
2
3
4
5
6
7
8
$ arduino-cli board listall mkr
Board Name              FQBN
Arduino MKR FOX 1200    arduino:samd:mkrfox1200
Arduino MKR GSM 1400    arduino:samd:mkrgsm1400
Arduino MKR WAN 1300    arduino:samd:mkrwan1300
Arduino MKR WiFi 1010   arduino:samd:mkrwifi1010
Arduino MKRZERO         arduino:samd:mkrzero
Arduino/Genuino MKR1000 arduino:samd:mkr1000

ボードに対応したコアをインストールする

arduino:samdプラットフォームコアをインストールするには、以下を実行します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ arduino-cli core install arduino:samd
Downloading tools...
arduino:arm-none-eabi-gcc@4.8.3-2014q1 downloaded
arduino:bossac@1.7.0 downloaded
arduino:openocd@0.9.0-arduino6-static downloaded
arduino:CMSIS@4.5.0 downloaded
arduino:CMSIS-Atmel@1.1.0 downloaded
arduino:arduinoOTA@1.2.0 downloaded
Downloading cores...
arduino:samd@1.6.19 downloaded
Installing tools...
Installing platforms...
Results:
arduino:samd@1.6.19 - Installed
arduino:arm-none-eabi-gcc@4.8.3-2014q1 - Installed
arduino:bossac@1.7.0 - Installed
arduino:openocd@0.9.0-arduino6-static - Installed
arduino:CMSIS@4.5.0 - Installed
arduino:CMSIS-Atmel@1.1.0 - Installed
arduino:arduinoOTA@1.2.0 - Installed

以下を実行して、コアが正しくインストールされたかを確認します。

1
2
3
$ arduino-cli core list
ID              Installed       Latest  Name
arduino:samd    1.6.19          1.6.19  Arduino SAMD Boards (32-bits ARM Cortex-M0+)

これで、スケッチをコンパイルしアップロードする準備が整いました。

サードパーティのコアを追加する

ボードを利用するのに、サードパーティのコアパッケージが必要であれば、Arduino CLIの設定ファイルに、追加のパッケージのインデックスのURLを追加します。

例えば、ESP8266コアを追加するには、設定ファイルを編集し、board_managerの設定を以下のように変更します。

1
2
3
board_manager:
  additional_urls:
    - https://arduino.esp8266.com/stable/package_esp8266com_index.json

パッケージインデックスをローカルにインストールしている場合は、Arduino CLIの設定ファイルにファイルパスを設定します。

例えば、NRF52832コアを追加するには、設定ファイルを編集し、board_managerの設定を以下のように変更します。

1
2
3
4
board_manager:
  additional_urls:
    - https://arduino.esp8266.com/stable/package_esp8266com_index.json
    - file:///absolute/path/to/your/package_nrf52832_index.json

これで、カスタムコアに対応しているコマンドは、設定ファイルから追加のURLを自動的に利用します。

1
2
3
4
5
6
7
8
9
$ arduino-cli core update-index
Updating index: package_index.json downloaded
Updating index: package_esp8266com_index.json downloaded
Updating index: package_nrf52832_index.json
Updating index: package_index.json downloaded

$ arduino-cli core search esp8266
ID              Version Name
esp8266:esp8266 2.5.2   esp8266

あるいは、追加のパッケージインデックスファイルへのリンクを--additional-urlsオプションを使い、渡すことができます。これは、サードパーティプラットフォームコアを必要とするすべてのコマンドを実行するたびに、毎回指定する必要があります。

1
2
3
4
5
6
$ arduino-cli  core update-index --additional-urls https://arduino.esp8266.com/stable/package_esp8266com_index.json
Updating index: package_esp8266com_index.json downloaded

$ arduino-cli core search esp8266 --additional-urls https://arduino.esp8266.com/stable/package_esp8266com_index.json
ID              Version Name
esp8266:esp8266 2.5.2   esp8266

同じことは、ファイルパスにより指定された追加のパッケージインデックスにも適用されます。

1
2
3
4
5
6
$ arduino-cli  core update-index --additional-urls file:///absolute/path/to/your/package_esp8266com_index.json
Updating index: package_esp8266com_index.json downloaded

$ arduino-cli core search esp8266 --additional-urls file:///absolute/path/to/your/package_esp8266com_index.json
ID              Version Name
esp8266:esp8266 2.5.2   esp8266

スケッチをコンパイルしアップロードする

スケッチをコンパイルするには、compileコマンドを実行します。この際、適切なFQBN文字列を渡します。

1
2
$ arduino-cli compile --fqbn arduino:samd:mkr1000 MyFirstSketch
Sketch uses 9600 bytes (3%) of program storage space. Maximum is 262144 bytes.

スケッチをボードにアップロードするには、以下のコマンドを実行します。この際、ボードを接続しているシリアルポートを指定します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:samd:mkr1000 MyFirstSketch
No new serial port detected.
Atmel SMART device 0x10010005 found
Device       : ATSAMD21G18A
Chip ID      : 10010005
Version      : v2.0 [Arduino:XYZ] Dec 20 2016 15:36:43
Address      : 8192
Pages        : 3968
Page Size    : 64 bytes
Total Size   : 248KB
Planes       : 1
Lock Regions : 16
Locked       : none
Security     : false
Boot Flash   : true
BOD          : true
BOR          : true
Arduino      : FAST_CHIP_ERASE
Arduino      : FAST_MULTI_PAGE_WRITE
Arduino      : CAN_CHECKSUM_MEMORY_BUFFER
Erase flash
done in 0.784 seconds

Write 9856 bytes to flash (154 pages)
[==============================] 100% (154/154 pages)
done in 0.069 seconds

Verify 9856 bytes of flash with checksum.
Verify successful
done in 0.009 seconds
CPU reset.

ライブラリを追加する

スケッチで追加の機能が必要な場合、Arduinoエコシステムで利用可能なライブラリが、必要な機能を提供している可能性があります。例えば、ボタン入力をよりよく扱うために、チャタリング防止機能(debouncing strategy)が必要な場合、debouncerというキーワードで検索することができます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ arduino-cli lib search debouncer
Name: "Debouncer"
    Author: hideakitai
    Maintainer: hideakitai
    Sentence: Debounce library for Arduino
    Paragraph: Debounce library for Arduino
    Website: https://github.com/hideakitai
    Category: Timing
    Architecture: *
    Types: Contributed
    Versions: [0.1.0]
Name: "FTDebouncer"
    Author: Ubi de Feo
    Maintainer: Ubi de Feo, Sebastian Hunkeler
    Sentence: An efficient, low footprint, fast pin debouncing library for Arduino
    Paragraph: This pin state supervisor manages debouncing of buttons and handles transitions between LOW and HIGH state, calling a function and notifying your code of which pin has been activated or deactivated.
    Website: https://github.com/ubidefeo/FTDebouncer
    Category: Uncategorized
    Architecture: *
    Types: Contributed
    Versions: [1.3.0]
Name: "SoftTimer"
    Author: Balazs Kelemen <prampec+arduino@gmail.com>
    Maintainer: Balazs Kelemen <prampec+arduino@gmail.com>
    Sentence: SoftTimer is a lightweight pseudo multitasking solution for Arduino.
    Paragraph: SoftTimer enables higher level Arduino programing, yet easy to use, and lightweight. You are often faced with the problem that you need to do multiple tasks at the same time. In SoftTimer, the programmer creates Tasks that runs periodically. This library comes with a collection of handy tools like blinker, pwm, debouncer.
    Website: https://github.com/prampec/arduino-softtimer
    Category: Timing
    Architecture: *
    Types: Contributed
    Versions: [3.0.0, 3.1.0, 3.1.1, 3.1.2, 3.1.3, 3.1.5, 3.2.0]

我々はFTDebouncerがいいので、インストールするのに、以下のコマンドを実行しましょう。

1
2
3
4
5
6
$ arduino-cli lib install FTDebouncer
FTDebouncer depends on FTDebouncer@1.3.0
Downloading FTDebouncer@1.3.0...
FTDebouncer@1.3.0 downloaded
Installing FTDebouncer@1.3.0...
Installed FTDebouncer@1.3.0

daemonモードとgRPCインターフェイスを使う

Arduino CLIは、daemonコマンドを使い、gRPCサーバとして起動することができます。

client_exampleフォルダには、gRPCサーバとやり取りする方法を示す、クライアントコードの例があります。利用可能なサービスとメッセージの詳細は、gRPCリファレンスを参照してください。

ログのほかに、gRPCサーバの動作状況を観測するために、daemonモードでは、デフォルトで、Prometheusのエンドポイント (http://localhost:9090/metrics)を有効にし公開します。以下のように、メトリクスデータを取得することができます。

1
2
3
4
5
# TYPE daemon_compile counter
daemon_compile{buildProperties="",exportFile="",fqbn="arduino:samd:mkr1000",installationID="ed6f1f22-1fbe-4b1f-84be-84d035b6369c",jobs="0",libraries="",preprocess="false",quiet="false",showProperties="false",sketchPath="5ff767c6fa5a91230f5cb4e267c889aa61489ab2c4f70f35f921f934c1462cb6",success="true",verbose="true",vidPid="",warnings=""} 1 1580385724726

# TYPE daemon_board_list counter
daemon_board_list{installationID="ed6f1f22-1fbe-4b1f-84be-84d035b6369c",success="true"} 1 1580385724833

メトリクス設定は、CLI設定のmetricsセクションにあります。

1
2
3
metrics:
  enabled: true
  addr: :9090

i
このページでは、最新版のArduino CLIを確認できます。過去のバージョンはこちらにあります。

オリジナルのページ

https://docs.arduino.cc/arduino-cli/getting-started/

最終更新日

August 16, 2026

inserted by FC2 system