Skip to content

Introductory tutorial and examples for ESP32 using ESP-IDF framework

License

Notifications You must be signed in to change notification settings

tomas-fryza/esp-idf

Repository files navigation

ESP32 projects in C/C++

Espressif IDF and PlatformIO extensions are popular choices for developing ESP32 projects in C/C++ using Visual Studio Code. Espressif IDF (IoT Development Framework) is the official development framework from Espressif Systems, the manufacturer of the ESP32 microcontroller. It is a very powerful and flexible framework, but it can also be quite complex to use, especially for beginners.

PlatformIO is a more user-friendly alternative to the IDF. It is a build system that can manage the compilation and flashing of ESP32 projects (but also Arduino, STM32, AVR, ...), as well as providing a variety of other features, such as integrated debugging support.

Feature Espressif IDF PlatformIO
Official framework Yes No
Power and flexibility High Medium
User friendliness Low High
Ease of use Low High
Documentation Good Good
Community support Medium Large

Installation of Espressif IDF with VS Code

logo-espressif-idf

  1. Follow these instructions: Installation on Linux and Windows and install Espressif IDF extension.

  2. It is recommended to start by an example project. See how to create, build, and run your first project.

NOTE: Please note that all available commands are accessible in VS Code in menu View > Command Palette... or by Ctrl+Shift+P shortcut, followed by a command ESP-IDF:

Command Description
F1 or Ctrl+Shift+P Command Palette where all commands are found
ESP-IDF: Show Examples Projects List of available projects
ESP-IDF: New project Create a new project via Command Palette
ESP-IDF: Set Espressif device target Select your Espressif target (esp32, esp32s2, etc.)
ESP-IDF: Build your project Build the project
ESP-IDF: Build, Flash, and start a monitor on your device Combine build, flash and serial monitor in a single command
ESP-IDF: Open ESP-IDF Terminal Open ESP-IDF Terminal where you can use idf.py command-line tool
idf.py create-project NAME Create a new project titled NAME in ESP-IDF Terminal
idf.py menuconfig Open project configuration
idf.py build Build the project
idf.py flash monitor Flashing esp-idf project and open serial monitor
Ctrl+] or Ctrl+) Exit the monitor

Other common shortcuts in VS Code.

Installation and usage of PlatformIO

logo-platformio

  1. Download and install Visual Studio Code source code editor.

  2. Run Visual Studio Code application:

    1. Open up the extensions viewer in menu View > Extensions Ctrl+Shift+X.

    2. Type platformio into the search bar.

    3. Press Install button on the result PlatformIO IDE. It may take a few minutes depending on your connection speed. Do not close any window and do not open other folders until the installing process is completed.

      Install PlatformIO IDE

    4. After that, restart the Visual Studio Code application.

  3. Create a new project:

    1. Click on the PlatformIO icon in the left-hand margin.

    2. In menu PIO Home select Open.

    3. Click to New Project button.

      Create a new project

    4. In project wizard, type gpio as project name.

    5. Select ESP32 board you have.

    6. Change framework to Espressif IoT Development Framework and select location to your working folder.

    7. Click on the Finish button. The creation of the first project may takes a few minutes and the internet connection to download the needed libraries and frameworks is mandatory.

      Create a new project

  4. Code the application:

    1. Copy/paste the basic template to your GPIO > src > main.c file.

    2. Add the following settings to platformio.ini project configuration file.

      monitor_speed = 115200
      
      # DTR & RTS settings of the serial monitor must be OFF
      monitor_rts = 0
      monitor_dtr = 0
      
      # Enable ANSI color codes in serial monitor
      monitor_raw = yes
    3. Connect your ESP32 board to USB port. (Mac users may need to install USB drivers, such as CH340.)

    4. Compile and upload the code. The handy shortcuts for building and uploading the code is available on the blue footer taskbar. Hover over the icon to get its functionality. First, compile (build) the project and then upload it to ESP32 board. Open the Serial monitor. (At some platforms you have to press on-board reset button.)

      PlatformIO navigation

Examples

  1. For Espressif IDF

    1. Hello world
    2. Remote control of LED through HTTP server by tbravenec
    3. Introduction to FreeRTOS using LED blinking task by tbravenec
  2. For PlatformIO

    1. Basic template with GPIO
    2. Logging methods
    3. Tasks
    4. I2C scan
    5. I2C DHT12 sensor
    6. Wi-Fi APs scan
    7. Wi-Fi GET requests to HTTP
    8. Wi-Fi & DHT12 & ThingSpeak
    9. Remote control of LED through HTTP server by tbravenec
    10. Introduction to FreeRTOS using LED blinking task by tbravenec

Tested on

Version Result (yyyy-mm-dd) Note
Linux Mint 21.2 (Victoria) OK (2024-01-06) Laptop
Windows 10 OK (2022-11-18) HomeOffice
Linux Mint 20.3 (Una) OK (2022-11-01) Laptop
# FYI: How to check OS version in Linux
cat /etc/os-release

# Or by Neofetch
neofetch

Useful information

References

  1. Espressif Systems. ESP-IDF Programming Guide

  2. Esp32 Tutorials. Install ESP32 ESP-IDF on Windows and Integrate with VS code

  3. ESP32 for IoT: A Complete Guide

  4. Pedro Minatel. ESP-IDF Development Tools Guide — Part I

  5. Espressif Systems. Build Your First Project

  6. Random Nerd Tutorials. ESP32-CAM AI-Thinker Pinout Guide: GPIOs Usage Explained

  7. Renzo Mischianti. ESP32-CAM: pinout, specs and Arduino IDE configuration – 1

  8. How To Choose The Best RTOS for IoT Devices