Pages

2018/09/06

How to develop Arduino ESP32 firmware using VS Code and PlatformIO

This post is about how to do firmware development for ESP32 using VS Code, PlatformIO, and Arduino-ESP32 Core.

In case the environment for VS Code and PlatformIO hasn't been set up, please refer to the post below.

How to develop Arduino ESP8266 firmware using VS Code and PlatformIO - Part 1

Create a new project for ESP32

1. Launch VS Code and click on the "PlatformIO: Home" icon.


2. Click on "+ New Project" under "Quick Access".


3. Enter project name, select board type, then select framework.

Note, be sure to leave no space in the project name.


 Click "Finish" when done. PlatformIO will start preparing the environment for project development.


When default location is used, on my computer, the newly created project is placed under "C:\Users\WeiHsiungHuang\Documents\PlatformIO\Projects".


Click on platformio.ini to check the configuration settings.


To change the baud rate of the serial output (in this case - 115200bps), add the below line to platformio.ini.

monitor_baud = 115200

Note, be sure to change "monitor_baud" to "monitor_speed" because "monitor_baud" is deprecated and will be removed in future release.

monitor_speed = 115200

Save and close the file when done.


Build (compile) and upload a new project

1. Click on "src" then on "main.cpp".


2. Copy and paste the blink sample code to "main.cpp".


The Code

#include <Arduino.h>

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

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

3. Click the "PlatformIO: Build" icon to start building (compiling) the code.


IMPORTANT!!!
Be sure to close all other projects and leave only the project that you are working on in the workspace. Otherwise, the build and upload process may not be on the right project. 


4. If all goes well, click the "PlatformIO: Upload" icon to upload the firmware to ESP32 for execution.


Below is the message generated during the upload process.

> Executing task in folder my_1st_esp32_arduino_project: C:\Users\WeiHsiungHuang\.platformio\penv\Scripts\platformio.exe run --target upload <

Processing nodemcu-32s (platform: espressif32; board: nodemcu-32s; framework: arduino)
-----------------------------------------------------------------------------------------------------------------------------------------------
PackageManager: Installing tool-mkspiffs @ ~2.230.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Verbose mode can be enabled via `-v, --verbose` option
PLATFORM: Espressif 32 > NodeMCU-32S
SYSTEM: ESP32 240MHz 320KB RAM (4MB Flash)
DEBUG: CURRENT(esp-prog) EXTERNAL(esp-prog, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny)
Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF MODES: FINDER(chain) COMPATIBILITY(soft)
Collected 24 compatible libraries
Scanning dependencies...
No dependencies
Retrieving maximum program size .pioenvs\nodemcu-32s\firmware.elf
Checking size .pioenvs\nodemcu-32s\firmware.elf
Memory Usage -> http://bit.ly/pio-memory-usage
DATA:    [          ]   4.2% (used 13884 bytes from 327680 bytes)
PROGRAM: [=         ]  13.6% (used 178028 bytes from 1310720 bytes)
Configuring upload protocol...
AVAILABLE: esp-prog, esptool, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny
CURRENT: upload_protocol = esptool
Looking for upload port...
Auto-detected: COM7
Uploading .pioenvs\nodemcu-32s\firmware.bin
Serial port COM7
Connecting........__
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core
MAC: 24:0a:c4:12:1d:18
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 15088 bytes to 9755...
Wrote 15088 bytes (9755 compressed) at 0x00001000 in 0.1 seconds (effective 815.6 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 144...
Wrote 3072 bytes (144 compressed) at 0x00008000 in 0.0 seconds (effective 3510.9 kbit/s)...
Hash of data verified.
Compressed 8192 bytes to 47...
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.0 seconds (effective 13106.9 kbit/s)...
Hash of data verified.
Compressed 178160 bytes to 89690...
Wrote 178160 bytes (89690 compressed) at 0x00010000 in 1.9 seconds (effective 763.8 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
======================================================== [SUCCESS] Took 26.42 seconds ========================================================

Terminal will be reused by tasks, press any key to close it.

Note,
Depending on the ESP32 module used, you may need to press and hold the BOOT / IO0 button for a while (until the connection with the ESP32 module is established) when seeing the below message; otherwise the upload will fail.

Connecting........_____.

5. The LED on the NodeMCU-32S is blinking now.

No comments:

Post a Comment