5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Arduino CLIでRaspberry Pi Pico WをLチカとWi-Fiスキャン

Last updated at Posted at 2023-03-30

はじめに

本日Raspberry Pi Pico Wが届いたのでさっそくArduino CLIで書き込みを試してみたメモ記事です。
同じ手順できっとLチカとWi-Fiスキャンができるはず…!

環境

  • OS: Windows10
  • Arduino CLI: v0.31.0
  • Device: Raspberry Pi Pico W

Arduino CLIのインストールからLチカまで

Arduino CLIのインストール

macOS / Linux
$ brew update
$ brew install arduino-cli

shスクリプトからのインストールであればWindows環境でもGit Bash等のCLIから実行できます。

shスクリプトからインストール
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh

私はWindows環境なのですがScoopからインストールしました。

Scoop(Windows)
$ scoop update
$ scoop search arduino-cli
$ scoop install arduino-cli

もちろん各OS向けの実行ファイルも直接ダウンロードできます。

Arduino CLI設定ファイルの初期化

以下のコマンドでarduino-cli.yamlという設定ファイルが生成されます。

生成パスはデフォルトではmacOS / Linuxだと~/.arduino15/arduino-cli.yaml
Windowsだと%USERPROFILE%\AppData\Local\Arduino15\arduino-cli.yamlです。

arduino-cli config init

Raspberry Pi Pico用ボードマネージャーの追加

まずはどのボードマネージャーを使えばいいか調査します。
以下の記事にてhttps://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.jsonという情報があったのでこれを設定します。

使用する情報 設定値
ボードマネージャURL https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
ボードマネージャ名 Raspberry Pi Pico/RP2040

arduino-cli config add board_manager.additional_urls https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

Raspberry Pi Pico W用ボードパッケージのインストール

続いてRaspberry Pi Pico W用ボードパッケージをインストールします。
まずは以下のコマンドを実行し「rp2040」に該当するRaspberry Pi Pico W用ボードパッケージが見つかることを確認します。
※「RP2040」はRaspberry Pi Pico Wに搭載されるチップの名称です

$ arduino-cli core update-index
$ arduino-cli core search rp2040

ID                  Version Name
arduino:mbed_nano   4.0.2   Arduino Mbed OS Nano Boards
arduino:mbed_rp2040 4.0.2   Arduino Mbed OS RP2040 Boards
rp2040:rp2040       3.1.0   Raspberry Pi Pico/RP2040
arduino:mbed        3.3.0   [DEPRECATED] [DEPRECATED - Please install standalone packages] Arduino Mbed OS Boards

いくつか見つかりましたが、先ほどの記事に記載のあった「Raspberry Pi Pico/RP2040」をインストールします。

インストールは以下のコマンドで行います。
installコマンドの最後のオプションは上記のsearchコマンドで出力されたIDの値になります。

インストール後にlistコマンドを実行して正常にインストールされていることを確認します。

$ arduino-cli core install rp2040:rp2040
$ arduino-cli core list

ID            インストール済 Latest Name
rp2040:rp2040 3.1.0   3.1.0  Raspberry Pi Pico/RP2040

Lチカスケッチの作成

まずスケッチファイルを作成します。
Arduino CLIではsketch newコマンドで作成できます。
以下のコマンドを実行しrpipicow-blinkというスケッチを作成します。

arduino-cli sketch new rpipicow-blink

続いてLチカのソースコードですが、別件で調べていたRP2040のPlatform.IOのリポジトリにいい感じのExampleがあったのでそれを使います。

使うソースコードは以下のものです。

以下のcurlコマンドで先ほど作成したスケッチのinoファイルを上書きします。

curl -fsSLk https://raw.githubusercontent.com/maxgerhardt/platform-raspberrypi/develop/examples/arduino-blink/src/main.cpp > rpipicow-blink/rpipicow-blink.ino

コンパイル

コンパイルにあたりArduino CLIではFQBNでボード情報を指定します。
まず以下のコマンドを実行しRaspberry Pi Pico WのFQBNを調べます。

$ arduino-cli board search rp2040

Board Name                          FQBN                                       Platform ID
0xCB Helios                         rp2040:rp2040:0xcb_helios                  rp2040:rp2040
Adafruit Feather RP2040             rp2040:rp2040:adafruit_feather             rp2040:rp2040
Adafruit Feather RP2040 DVI         rp2040:rp2040:adafruit_feather_dvi         rp2040:rp2040

...

Raspberry Pi Pico                   rp2040:rp2040:rpipico                      rp2040:rp2040
Raspberry Pi Pico W                 rp2040:rp2040:rpipicow                     rp2040:rp2040

...

結果がたくさん出ますがBoard NameRaspberry Pi Pico WのレコードのFQBNであるrp2040:rp2040:rpipicowが正解です。

コンパイルコマンドはFQBNとスケッチ名を指定して実行します。

arduino-cli compile --fqbn rp2040:rp2040:rpipicow rpipicow-blink

アップロード

最後にデバイスへコンパイルしたスケッチをアップロードします。
Arduino IDEやPlatform.IOの場合もそうですが、アップロードには対象デバイスのポート情報が必要となります。
Raspberry Pi Pico Wのポート番号を調べてみましょう。

再び先ほどの記事を参照すると初回書き込み時は「BOOT SEL」ボタンを押しながらPCにUSB接続するとあります。
使用するRaspberry Pi Pico Wが初回書き込みであれば同様にPCへUSBで接続してみましょう。

PC接続後、以下のコマンドを実行しポート情報を調べます。
私のWindows環境では以下のような結果となりました。

$ arduino-cli board list

シリアルポート   Protocol タイプ         Board Name FQBN Core
UF2_Board uf2conv  UF2 Devices Unknown

シリアルポートにあるUF2_Boardがポート情報となります。
このポート情報とFQBN、スケッチ名を指定してアップロードコマンドを実行します。

arduino-cli upload --port <ポート> --fqbn rp2040:rp2040:rpipicow rpipicow-blink

以上でRaspberry Pi Pico WへLチカスケッチが書き込まれます。
Raspberry Pi Pico WのLEDが1秒おきに点滅していたら成功です。

Wi-Fiスキャンスケッチの書き込み

先ほどLチカのソースコードを使用したPlatform.IOリポジトリのExampleにWi-Fiスキャンのソースコードもあります。
Raspberry Pi Pico WはWi-Fi付きのデバイスなのでこれも試してみましょう。

スケッチ作成

先ほどと同様にスケッチを新規作成し、Exampleのソースコードで上書きます。
今回はrpipicow-wifi-scanというスケッチ名で作成します。

$ arduino-cli sketch new rpipicow-wifi-scan
$ curl -fsSLk https://raw.githubusercontent.com/maxgerhardt/platform-raspberrypi/develop/examples/arduino-wifi-scan/src/main.cpp > rpipicow-wifi-scan/rpipicow-wifi-scan.ino

ポート情報の再確認

Raspberry Pi Pico WとPCのUSB接続ですが、Lチカスケッチのアップロード時は初回書き込みのため「BOOT SEL」ボタンを押しながらPCにUSB接続しました。
しかし一度書き込み済みであれば以降は普通にPCにUSB接続すれば大丈夫です。

普通にPCにUSB接続した場合はポート情報が変わるため再度ポート情報を確認します。
私のWindows環境では以下のような結果となり、普通にPCにUSB接続した場合のポート情報はCOM15となりました。

$ arduino-cli board list

シリアルポート Protocol タイプ               Board Name FQBN Core
COM15   serial   Serial Port (USB) Unknown

コンパル & アップロード

続いてスケッチのコンパイルとアップロードを行います。
コンパイルコマンドに--uploadオプションを加えるとコンパイルとアップロードを同時に実行してくれます。
先ほど再確認したポート情報、FQBN、スケッチ名を同様にオプションに指定します。

arduino-cli compile --upload --port <ポート> --fqbn rp2040:rp2040:rpipicow rpipicow-wifi-scan

Wi-Fiスキャン結果の確認

Wi-Fiスキャンの結果はシリアルモニタから確認します。
Arduino CLIではmonitorコマンドでシリアルモニタを実行できます。
このときオプションにポート情報が必要になりますが、ここで指定するポート情報はRaspberry Pi Pico Wを普通にPCにUSB接続したときのポート情報を指定してください。
※「BOOT SEL」ボタンを押しながらPCにUSB接続したときのポート情報ではシリアルモニタを実行できません

arduino-cli monitor --port <ポート>

付近のWi-FiのAP情報が表示されれば成功です。

本記事は以上となります。
お疲れさまでした🎉

参考

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?