Getting started: Arduino IDE, VS Code and PlatformIO

You write ESP32 code on your computer. A software tool compiles the code and sends it to the board through the USB cable. This page tells you how to set up two tools for a classic ESP32-WROOM-32 DevKit. The Arduino IDE 2.x is the makerspace default. VS Code with PlatformIO is better for large projects. The code on this site uses the Arduino-ESP32 core 3.x API.

Which tool to choose

Arduino IDE 2.xVS Code + PlatformIO
Best forBeginners, short sketches, makerspace workshopsLarge projects with many files, team projects
SetupEasy. One installer and one board package.More steps. The first build downloads many tools.
ESP32 core versionThe newest 3.x core from EspressifSet by the platform line in platformio.ini (see the note below)
LibrariesInstalled one time for all sketchesListed in each project, so each project keeps its own versions
Version pinningManualYes. You write the exact versions in platformio.ini.
Git and code editorBasic editorFull editor with autocomplete and Git
Recommendation: Start with the Arduino IDE. Move to PlatformIO when your project has many files or when you use Git.

Arduino IDE 2.x

Install the IDE and the ESP32 boards

  1. Install the Arduino IDE 2.x.Download it from arduino.cc/en/software. Run the installer.
  2. Open the preferences.On Windows, select File → Preferences. On macOS, select Arduino IDE → Settings.
  3. Add the Espressif boards URL.Paste this URL into Additional boards manager URLs:
    https://espressif.github.io/arduino-esp32/package_esp32_index.json
    Click OK.
  4. Open the Boards Manager.Select Tools → Board → Boards Manager. Type esp32 in the search box.
  5. Install the ESP32 core.Find esp32 by Espressif Systems. Select a 3.x version and click Install. The download is large, so wait some minutes.
  6. Install the USB driver if necessary.Connect the board. If a new port shows in Tools → Port, you do not need a driver. If no port shows, read the name of the small chip next to the USB connector (CP2102, CH340 or CH9102). Then download and install the driver for that chip. Read Install the ESP32 USB driver for the download links and the steps for Windows and macOS. Restart the Arduino IDE after the installation.
Install “esp32 by Espressif Systems”. The package “Arduino ESP32 Boards” by Arduino is for Arduino-brand boards only.

Select the board and the port

  1. Connect the DevKit.Use a USB cable that carries data.
  2. Select the board.Select Tools → Board → esp32 → ESP32 Dev Module.
  3. Select the port.Select Tools → Port. On Windows, the port is COM3 or a similar name. On macOS, the port is /dev/cu.usbserial-… or /dev/cu.wchusbserial….
  4. If no port shows, examine the driver and the cable.Install the USB driver (refer to step 6 above), or use a different USB cable. Read Install the ESP32 USB driver.
  5. Open the Serial Monitor.Select Tools → Serial Monitor. Set the baud rate list to 115200 baud. This value must agree with Serial.begin(115200) in your code.

Keep the other settings in the Tools menu at their default values. The defaults are correct for a 4 MB ESP32-WROOM-32 DevKit.

Upload Blink

Many DevKits have a blue LED on GPIO 2. If your board has no LED on GPIO 2, connect an LED and a 220 Ω resistor from GPIO 2 to GND.

blink.ino


              const int LED_PIN = 2;   // on-board LED on many DevKits

              void setup() {
                Serial.begin(115200);
                pinMode(LED_PIN, OUTPUT);
              }

              void loop() {
                digitalWrite(LED_PIN, HIGH);
                Serial.println("LED on");
                delay(500);
                digitalWrite(LED_PIN, LOW);
                Serial.println("LED off");
                delay(500);
              }
            
  1. Paste the code.Select File → New Sketch. Replace all the text with the code above.
  2. Upload the code.Click the Upload button (the right arrow). The first compile takes some minutes.
  3. Examine the result.The LED flashes. The Serial Monitor shows “LED on” and “LED off”.

Install libraries

  1. Open the Library Manager.Select Sketch → Include Library → Manage Libraries. You can also click the books icon in the left sidebar.
  2. Find the library.Type the name, for example Adafruit NeoPixel.
  3. Install the library.Click Install. If the IDE asks about dependencies, click Install all.
  4. Use the library.Add the #include line at the top of your sketch. Each page on this site tells you which library to install.
Note: Some old libraries use the ESP32 core 2.x API. If a library does not compile with core 3.x, look for a newer version of the library.

Common upload errors

Message or symptomCauseFix
Failed to connect to ESP32: Wrong boot mode detected or the upload stops at Connecting…The board did not go into download mode automatically.Push and hold the BOOT button. Click Upload. Release BOOT when the percent values show.
No port in Tools → PortThe cable carries only power, or the USB driver is missing.Use a different cable that carries data. Then install the driver.
Port is busy or Access is deniedAnother program uses the port.Close other Serial Monitor windows and other serial programs. Then try again.
Upload is correct, but nothing happensThe board stays in download mode, or the pin is wrong.Push the EN (reset) button. Make sure that the pin number in the code agrees with your wiring.
Random characters in the Serial MonitorThe baud rate is wrong.Set the Serial Monitor to the same value as Serial.begin().
'ledcSetup' was not declared or a similar errorThe code uses the old 2.x API.Use ledcAttach() and ledcWrite(pin, duty). Read the PWM page.

VS Code + PlatformIO

PlatformIO is an extension for Visual Studio Code. It keeps the board settings, the ESP32 platform version and the library list in one file: platformio.ini. You can put this file in Git, so other people can build the same project.

Install and create a project

  1. Install VS Code.Download it from code.visualstudio.com.
  2. Install the PlatformIO extension.Open the Extensions view (Ctrl+Shift+X). Search for PlatformIO IDE. Click Install.
  3. Wait for the installation to complete.PlatformIO downloads its core tools. Restart VS Code when it asks.
  4. Create a project.Click the PlatformIO icon (the ant head) in the left bar. Select PIO Home → Projects → Create New Project.
  5. Set the project options.Type a name. For Board, select Espressif ESP32 Dev Module (esp32dev). For Framework, select Arduino. Click Finish.
  6. Edit platformio.ini.Use the example on this page. Add the monitor_speed line.

platformio.ini


              [env:esp32dev]
              platform = espressif32
              board = esp32dev
              framework = arduino
              monitor_speed = 115200
              lib_deps =
                  adafruit/Adafruit NeoPixel@^1.12.0
            
  • monitor_speed sets the Serial Monitor baud rate. Make it agree with Serial.begin().
  • lib_deps lists the libraries. PlatformIO downloads them at the next build. Add one library on each line.
  • Find the exact library names in the PIO Home → Libraries search, or at registry.platformio.org.
Check the ESP32 core version. The official platform = espressif32 gives the older Arduino-ESP32 core 2.0.x. It does not give the 3.x core. Some code on this site uses 3.x functions, for example ledcAttach(), so that code does not compile with the official platform. To get the 3.x core, many people use the community pioarduino platform. Replace the platform line with this line:
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
pioarduino is a community project. PlatformIO and Espressif do not maintain it. The pioarduino project also has its own VS Code extension, pioarduino IDE. Do not install the PlatformIO IDE extension and the pioarduino IDE extension in the same VS Code profile, because they can conflict.

Where the code goes

Put your code in src/main.cpp. A .cpp file needs #include <Arduino.h> at the top. The Arduino IDE adds this line automatically, but PlatformIO does not.

src/main.cpp


              #include <Arduino.h>

              const int LED_PIN = 2;

              void setup() {
                Serial.begin(115200);
                pinMode(LED_PIN, OUTPUT);
              }

              void loop() {
                digitalWrite(LED_PIN, !digitalRead(LED_PIN));
                Serial.println("toggle");
                delay(500);
              }
            

In a .cpp file, declare each function before you use it. The Arduino IDE does this step for .ino files.

Build, upload and monitor

Use the buttons in the blue status bar at the bottom of VS Code:

ButtonAction
✓ (check mark)Build. Compiles the code.
→ (right arrow)Upload. Compiles the code and sends it to the board.
Plug iconSerial Monitor. Shows the output at monitor_speed.
Trash can iconClean. Deletes the build files. Use it after you change the platform.
  • PlatformIO usually finds the port automatically. To set it, add upload_port = COM5 (or /dev/cu.usbserial-…).
  • Close the Serial Monitor before you upload. If you do not, the upload can fail because the port is busy.
  • The BOOT button fix and the driver fix are the same as for the Arduino IDE.
Other VS Code extensions: Microsoft no longer maintains its “Arduino” extension for VS Code. Use PlatformIO, or use the Arduino IDE 2.x.

Resources