ES: ESP32 Getting Started

Setting up an ESP32 development environment using PlatformIO and Visual Studio Code. This guide covers installation, project creation, writing a blink program, uploading firmware, and using the serial monitor for debugging.

Getting started with ESP32 development is straightforward once the right tools are in place. This guide uses PlatformIO with Visual Studio Code — a free, cross-platform development ecosystem that provides dependency management, multi-board support, and integrated debugging in a single environment.


Core Features of Every ESP32 Board

Before setting up the environment, it helps to know what every ESP32 board brings to the table:

FeatureDescription
Wireless CommunicationBuilt-in Wi-Fi and Bluetooth
Dual-Core ProcessorFor multitasking and compute-intensive workloads
PeripheralsGPIO, ADC, DAC, UART, SPI, I2C, PWM
MemoryVarying RAM and flash configurations; PSRAM in WROVER models
Low Power ModesMultiple sleep modes — essential for battery projects

Development Environment Options

Three development environments support ESP32:

EnvironmentProsBest For
Arduino IDESimple, large library ecosystemBeginners
PlatformIO (VS Code)Professional, cross-platform, freeIntermediate to advanced
ESP-IDFMost powerful, full ESP32 feature accessAdvanced use

This guide covers PlatformIO with Visual Studio Code — the best balance of power and accessibility.


What is PlatformIO?

PlatformIO is a development ecosystem that simplifies working with microcontrollers, especially in VS Code. It provides:

  • Easy management of libraries and dependencies
  • Support for multiple platforms including ESP32, STM32, AVR, and more
  • Integrated tools for building, uploading, and debugging code
  • A unified platformio.ini configuration file for project settings

Prerequisites

Before installing PlatformIO, ensure these are installed:

  1. Visual Studio Codecode.visualstudio.com
  2. Python — PlatformIO requires Python to run — python.org

Step 1: Install PlatformIO

  1. Open VS Code and navigate to the Extensions panel (Ctrl+Shift+X or the square icon on the sidebar).
  2. Search for PlatformIO IDE.
  3. Click Install.

After installation, PlatformIO adds icons to the VS Code sidebar and new options to the status bar at the bottom.


Step 2: Create Your First Project

  1. Click the PlatformIO icon in the VS Code sidebar.
  2. Click New Project and fill in:
  • Name: ESP32-Blink
  • Board: ESP32 Dev Module
  • Framework: Arduino
  1. Click Finish.

PlatformIO creates a project with this structure:

md
ESP32-Blink/
├── src/            <- Your source code (.cpp files)
├── include/        <- Header files
├── lib/            <- Local libraries
├── platformio.ini  <- Project configuration
└── .pio/           <- Build artifacts (auto-generated)


Open src/main.cpp and replace its contents with:

cpp
#include <Arduino.h>

void setup() {
    pinMode(LED_BUILTIN, OUTPUT); // Set built-in LED as output
}

void loop() {
    digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on
    delay(1000);                     // Wait for 1 second
    digitalWrite(LED_BUILTIN, LOW);  // Turn the LED off
    delay(1000);                     // Wait for 1 second
}

This program blinks the onboard LED every second — the classic first embedded program.


Step 4: Build and Upload

  1. Connect the ESP32 to your computer via USB.
  2. Select the Serial Port in the PlatformIO toolbar at the bottom of VS Code:
  • Windows: usually COM3 or COM4
  • Linux/macOS: usually /dev/ttyUSB0 or /dev/cu.usbserial-*
  1. Build the project — click the checkmark () in the PlatformIO toolbar.
  2. Upload the firmware — click the right arrow () to flash to the ESP32.

The onboard LED should start blinking once upload completes.


Step 5: Use the Serial Monitor for Debugging

The serial monitor is essential for reading debug output from the ESP32. Add serial communication to the blink program:

cpp
#include <Arduino.h>

void setup() {
    Serial.begin(115200);          // Initialize serial at 115200 baud
    pinMode(LED_BUILTIN, OUTPUT);   // Set built-in LED as output
}

void loop() {
    Serial.println("LED ON");       // Print to Serial Monitor
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);

    Serial.println("LED OFF");      // Print to Serial Monitor
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
}

Open the Serial Monitor by clicking the plug icon in the PlatformIO toolbar or selecting PlatformIO: Monitor. You should see LED ON and LED OFF alternating every second.


Configuring platformio.ini

The platformio.ini file controls all project settings. A typical ESP32 configuration:

ini
[env:esp32dev]
platform      = espressif32
board         = esp32dev
framework     = arduino
upload_speed  = 921600
monitor_speed = 115200

SettingDescription
platformEspressif's platform package
boardTarget board identifier
frameworkBuild framework (Arduino or ESP-IDF)
upload_speedFaster uploads with 921600 baud
monitor_speedSerial monitor baud rate must match Serial.begin()

Adding Libraries

To add a library, either:

  1. Use the PlatformIO Library Manager in the sidebar to search and install
  2. Add to platformio.ini directly:

ini
[env:esp32dev]
platform    = espressif32
board       = esp32dev
framework   = arduino
lib_deps    =
    bblanchon/ArduinoJson@^6.21.0
    knolleary/PubSubClient@^2.8

PlatformIO automatically downloads and compiles the dependencies.


Common Troubleshooting

ProblemSolution
Upload failsHold the BOOT button on the ESP32 during upload
Port not foundInstall CP2102 or CH340 USB driver for your OS
Serial Monitor shows garbageCheck that monitor_speed matches Serial.begin() baud rate
Board not recognizedTry a different USB cable (data cable, not charge-only)

Next Steps

With the development environment working, you are ready to explore:

md
GPIO control         →  Digital input/output, button reading
Serial communication →  UART, debugging, GPS modules
Wi-Fi connectivity   →  HTTP requests, MQTT, OTA updates
BLE                  →  Device provisioning, sensor data
ADC                  →  Analog sensor reading
I2C / SPI            →  Display and sensor interfacing


Final Thoughts

PlatformIO with VS Code provides a professional-grade embedded development experience for free. The combination of dependency management, multi-board support, and integrated serial monitor removes most of the setup friction so you can focus on writing firmware.

A working blink program is not just a test — it is proof that your entire toolchain is functional from editor to hardware.