Skip to content

Introduction and examples for ESP32 using Arduino framework

License

Notifications You must be signed in to change notification settings

tomas-fryza/esp-arduino

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arduino framework for ESP32/ESP8266 microcontrollers

The repository contains instructions and examples for ESP32/ESP8266 microcontrollers using C/C++ Arduino framework in PlatformIO and Visual Studio Code IDE.

Installation

PlatformIO is a cross-platform build system that can be used to develop software platforms for Arduino, ESP32, PIC32, and AVR. PlatformIO can be used with VS Code to provide a powerful set of tools to assist you in development [1], [2].

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

  2. Run Visual Studio Code, open up the extensions viewer in menu View > Extensions Ctrl+shift+X, and then typing PlatformIO into the search bar. Press Install button on the result PlatformIO IDE.

    Install 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. After that, restart the Visual Studio Code.

Usage

  1. Create a new project: Click on the PlatformIO icon in the left-hand margin, then in menu PIO Home > Open > New Project. Type blink as project name, select your ESP board, such as Espressif ESP32 Dev Module, keep Arduino framework, and select location to your working folder. Click on the Finish button.

  2. Code the application: Copy/paste the following code to your BLINK > src > main.cpp source file.

    /*
     * Blink
     *
     * Turns on and off an LED and send a message to VS Code repeatedly.
     */
    
    #include <Arduino.h>
    
    // Set pin number of your LED
    #define PIN_LED 2
    
    // Put your setup code here, to run once
    void setup()
    {
        // Initialize LED digital pin as an output
        pinMode(PIN_LED, OUTPUT);
        // Tur off the LED
        digitalWrite(PIN_LED, LOW);
    
        // Set the communication symbol rate
        Serial.begin(115200);
    }
    
    // Put your main code here, to run repeatedly
    void loop()
    {
        Serial.println("LED is on");  // Transmit string
        digitalWrite(PIN_LED, HIGH);  // Turn on the LED
        delay(250);                   // Wait for 250 ms
        Serial.println("LED is off");
        digitalWrite(PIN_LED, LOW);   // Turn off the LED
        delay(750);                   // Wait for 750 ms
    }

    The code combine both output LED control and UART communication from ESP microcontroller and VS Code. To ensure the correct receiption, add the following line to the BLINK > platformio.ini configuration file:

    monitor_speed = 115200
  3. Compile and upload the code: The handy shortcuts for building and uploading the code is available on the footer taskbar. Hover over the icon to get its functionality. First, compile (build) the project, then upload it to ESP board, and open the Serial monitor to receive messages from the board. You can clean up generated files as well.

    Control icons

    Command Palette: All available commands in VS Code are also accessible by Ctrl+Shift+P shortcut and followed by a command. Most useful commands in this course are:

    • PlatformIO: Build
    • PlatformIO: Upload
    • PlatformIO: Clean
    • PlatformIO: Upload and Monitor

VS Code shortcuts

Other common shortcuts in VS Code are:

Shortcut Usage
Ctrl+Shift+P Command Palette. Access to all of the VS Code functionality
Ctrl+K+O Open folder
Ctrl+K F Close folder
Ctrl+Tab Switch between open files
Ctrl+O Open a file
Ctrl+Shift+S Save all open files
Ctrl+Shift+V Open Markdown preview
Ctrl+K V Open Markdown preview to the side
Ctrl+B Toggle Sidebar visibility

Examples

  1. Basic C template, blink a LED
  2. Button
  3. Timers
  4. I2C scanner
  5. I2C sensor DHT12
  6. I2C sensor MPU-6050
  7. I2C SH1106 OLED display
  8. Lilygo T-Display
  9. Wi-Fi scanner
  10. Wi-Fi Station mode
  11. ThingSpeak online platform
  12. OpenWeatherMap

Tested on

Version Result (yyyy-mm-dd) Note
Linux Mint 21.2 (Victoria) OK (2024-01-18) Laptop
Linux Mint 20.3 (Una) OK (2023-09-25) Laptop
Windows 10 OK (2023-09-21) Lab SC 6.61
# FYI: How to check OS version in Linux
cat /etc/os-release

# Or by Neofetch
neofetch

About

Introduction and examples for ESP32 using Arduino framework

Topics

Resources

License

Stars

Watchers

Forks