Image of eBits Academy fundamentals illustration

Setting up ESP32 in Arduino IDE and PlatformIO

  • October 15, 2021
  • |
  • Jesper Nielsen

ARDUINO IDE VS. PLATFORMIO

Arduino is an open-source platform that makes it straightforward to program microcontrollers. These include Arduino’s own boards such as Arduino Nano, Arduino Uno and Arduino Mega among others.
You can also program third-party boards and chips, including products from Adafruit, ATtiny and Espressif ESP32. Arduino IDE offers a simple interface suited to beginners and to experienced users who do not need a large feature set.

Arduino IDE 2 includes code completion and a modern editor. PlatformIO is an alternative with project-based management of boards, frameworks and libraries in Visual Studio Code. Debugging requires a supported board and suitable debug hardware; it is not automatically available on every ESP32 board.

INSTALLING ARDUINO IDE

The following steps cover Arduino IDE. Further down, there is also a guide to installing Visual Studio Code and PlatformIO IDE.

STEP 1

To use Arduino IDE, download it from Arduino.cc. Versions are available for Windows, Linux and macOS.
Use this link: Software | Arduino

STEP 2

Install Arduino IDE on your computer. The video below can help illustrate the process. Open the program after installation.

STEP 3

If Windows does not recognise the ESP32 board, first check the USB data cable, port and the board’s actual USB interface. CP210x, CH34x and native USB do not necessarily use the same driver. The manufacturer’s CP210x driver is here: https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers. Choose the driver matching the USB chip, operating system and computer architecture. A driver for another chip will not solve the problem.

STEP 4

Open settings in Arduino IDE 2 and add Espressif’s official ESP32 package index under Additional Boards Manager URLs. Then install esp32 by Espressif Systems through Boards Manager. The ESP8266 package is only relevant to ESP8266 boards.

https://espressif.github.io/arduino-esp32/package_esp32_index.json

Confirm the settings with OK.

STEP 5

Select the exact board model and correct port. Do not automatically use ESP32 Wrover Module for every ESP32 board: ESP32, ESP32-S3 and ESP32-C3 are different targets. Follow the board manufacturer’s flash, PSRAM and USB recommendations.

Arduino IDE is now set up to program the ESP using Arduino code.

STEP 6

Build the code with the checkmark at the top left and upload it with the arrow beside it.

Try this using the example below:

Blink code:

We use GPIO2 on the ESP32 board as an example. Check the LED pin and polarity of your exact board; not every ESP32 board has an onboard LED on GPIO2. An external LED needs a suitable series resistor.

void setup(){
    // put your setup code here, to run once:
    pinMode(2,OUTPUT);
}
void loop(){
    // put your main code here, to run repeatedly:
    digitalWrite(2,HIGH);
    delay(100);
    digitalWrite(2,LOW);
    delay(100);

}

 

 

EXTERNAL LIBRARIES IN ARDUINO IDE

Arduino IDE offers many existing libraries that simplify projects. Find them under Tools -> Manage Libraries...

In the window shown below, search for the library you want. Here the search is for libraries for the DHT11 temperature and humidity sensor available from eBits.dk.

INSTALLING VSCODE AND PLATFORMIO

PlatformIO IDE uses Visual Studio Code to program Arduino and similar devices. VS Code supports languages ranging from HTML, CSS and JavaScript for web development to Python, C and C++ for embedded hardware.

STEP 1

First download Visual Studio Code (VS Code) from its website. It is available for Windows, Linux and macOS. Use this link: Visual Studio Code - Code Editing. Redefined

STEP 2

Install VS Code on your computer and open it when installation is complete. The video below illustrates setup of VS Code and PlatformIO, or you can follow the written steps.

STEP 3

Install PlatformIO as a VS Code extension. Open Extensions in the left toolbar, search for PlatformIO, select PlatformIO IDE and choose Install.

STEP 4

If VS Code asks to restart after PlatformIO IDE is installed, choose OK.

STEP 5

To create an Arduino project with PlatformIO IDE, select the alien icon in the left toolbar below Extensions. Choose Open to open PIO Home, then choose New Project.

STEP 6

The Project Wizard asks for three things: the project name, then the board you want to program. Search for your exact ESP board, then select the framework. For example, the ESP32 board can use Arduino or Espressif’s framework. For this example, choose Arduino

Then choose Finish and proceed to the blink example below.

STEP 7

Build the code with the small checkmark and upload it using the arrow beside it.

Blink code:

We use GPIO2 on the ESP32 board as an example. Check the LED pin, polarity and whether the board has an onboard LED at all. Adapt the code to the specific board.

#include <Arduino.h>
void setup(){
    // put your setup code here, to run once:
    pinMode(2,OUTPUT);
}
void loop(){
    // put your main code here, to run repeatedly:
    digitalWrite(2,HIGH);
    delay(100);
    digitalWrite(2,LOW);
    delay(100);

}

EXTERNAL LIBRARIES IN PLATFORMIO

PlatformIO manages libraries through your project configuration. Open platformio.ini and add lib_deps with the libraries required by the project. When you build, PlatformIO downloads the specified dependencies and their required libraries.

The image below shows the DHT11 temperature and humidity sensor available from eBits. For a more detailed walkthrough, see the Andreas Spiess video linked earlier.

Leave a comment

Please note, comments need to be approved before they are published.