今回は、前回構築したプロジェクトファイルに手を加えて、定番の Hello_world を行います。
ESP-IDFにはサンプルプログラムが提供されています。
まずはそれらを触ってみるのが理解への近道だと思います。
この記事は JTAG でデバッグすることを前提にして書いています。
環境構築については こちら をご覧になってください。
投稿時の開発環境を記しておきます。
PC:
Windows10 OS
開発ボード :
ESP32-DevKitCーVE
デバッガー(H/W):
FT2232D
デバッガー (S/W) :
Visual Studio Code + PlatformIO + ESP-IDF Framework
コーディングしてみる
VSCodeにPlatformIOをインストールし、ESP-IDFのプロジェクトをつくると以下のフォルダにサンプルプログラムがコピーされます。
(xxxxx は皆さまのユーザー名)
C:\Users\xxxxx\.platformio\packages\framework-espidf\examples
まず前回つくったプロジェクトの src\main.c に書かれている1行を削除します。

次に examples\get-started\hello_world\main にある hello_world_main.c の内容を src\main.c に貼りつけます。
貼りつけたコードを載せておきます。
/* Hello World Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
void app_main(void)
{
printf("Hello world!\n");
/* Print chip information */
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
printf("This is %s chip with %d CPU core(s), WiFi%s%s, ",
CONFIG_IDF_TARGET,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
printf("silicon revision %d, ", chip_info.revision);
printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
for (int i = 10; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();
}
main.cにカーソルのある状態で Ctrl + s キーを押してファイルを保存します。
ビルドする
アクティビティバーの一番下のアイコンをクリックするたびに、PLATFORMIO が見え隠れします。
PLATFORMIOを表示させた状態でツリーの Build を選択します。

エラーがなければ画面下の TERMINAL に ===== [SUCCESS] Took xxxxx ======= のように表示されます。
(当然何かエラーが出たら、それなりに対処する必要があります)
iniファイルを編集する
アクティビティバーの一番上のアイコンをクリックするたびに、EXPLORER が見え隠れします。
EXPLORERを表示させた状態でツリーの下の方にある platformio.ini を選択します。
以下のように下の3行を追加します。
ここで、COM[4]の 4 には前回メモした、ESP32 COM に続く番号を記述します。
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf
debug_tool = minimodule
upload_port = COM[4]
monitor_speed = 115200
COMポートを自動検出するので、複数あると誤る可能性があるために明記しておきます。
ctrl + s キーを押して platformio.ini を保存しておきます。
続いて Build の下にある Upload を選択し、プログラムをESP32に書き込みます。
エラーがなければ画面下の TERMINAL に ===== [SUCCESS] Took xxxxx ======= のように表示されます。
(当然何かエラーが出たら、それなりに対処する必要があります)
printf()出力をモニターする
Upload の下にある Monitor を選択します。
画面下の TERMINAL に以下のメニューが出ます。
Silicon Labs CP210x USB … のポートを選択します。
この場合 1 を選択します。
一度 TERMINAL をクリックすると
— Enter port index or full name: の後にカーソルが出るので、そこに 1 を入力して enter します。

TERMINAL に Hello world! とそれに続く情報が繰り返し表示されれば成功です。
Hello world!
This is esp32 chip with 2 CPU core(s), WiFi/BT/BLE, silicon revision 3, 8MB external flash
Minimum free heap size: 291480 bytes
Restarting in 10 seconds...
Restarting in 9 seconds...
Restarting in 8 seconds...
Restarting in 7 seconds...
Restarting in 6 seconds...
Restarting in 5 seconds...
Restarting in 4 seconds...
Restarting in 3 seconds...
Restarting in 2 seconds...
Restarting in 1 seconds...
Restarting in 0 seconds...
Restarting now.