Skip to content

PlatformIO

Here you will find the necessary steps to program our Arduino based PLC using PlatformIO, from setting up your PC to completing the first program upload.

Requirements

  • Arduino or ESP32 based Industrial Shields PLC.
  • USB B or Micro USB cable (depending on the PLC).
  • Computer running GNU/Linux, windows or macOS.

Installation

There are 2 ways of working with PlatformIO:

To install any of them, follow the corresponding steps.

About VSCodium

In this documentation we use VSCodium, a telemetry-free, open-source version of VS Code. It offers the same powerful features without concerns about data privacy or licensing. However, this tutorial can also be followed with VSCode.

  1. Make sure you have VSCodium properly installed. If you do not, you can download them from their website: https://vscodium.com/.
  2. Download the C/C++ Microsoft extension for your operative system (use the Download Extension button on the right column of the page).
  3. Install it in VSCodium by going to, on the left bar: Extensions > ... > Install from VSIX... and choosing the downloaded file.
  4. Following the same procedure, download and install the PlatformIO IDE extension.
  5. Only on GNU/Linux, install udev rules for PlatformIO supported boards/devices: 99-platformio-udev.rules

Capture: PlatformIO IDE

  1. Install the PlatformIO Core according to the official documentation: Installer Script.
  2. In order to access PlatformIO Core outside PlatformIO IDE (e. g. using a terminal emulator), install Shell commands.
  3. Only on GNU/Linux, install udev rules for PlatformIO supported boards/devices: 99-platformio-udev.rules

PlatformIO

For more information about PlatformIO, check out the official documentation.

Starting a Project

This tutorial introduces you to the basics of PlatformIO, showing how to create a simple project for an Industrial Shields Arduino (or ESP32) PLC.

  1. Open VSCodium and press the PlatformIO: Home button on the bottom bar. Then click New Project, choose a name and select any board, as we will be manually setting the board later.
  2. Modify the platformio.ini file so it has the following contents, according to your PLC family:

    platformio.ini
    [env]
    platform_packages = framework-industrialshields-avr@https://apps.industrialshields.com/main/arduino/boards/industrialshields-boards-avr-1.2.0.tar.bz2
    
    [env:board]
    platform = https://github.com/Industrial-Shields/platform-industrialshields-avr.git
    board = mduinoplc_21+
    framework=arduino
    
    Select your PLC model by changing the board option. The available models can be seen here.

    platformio.ini
    [env]
    platform_packages = framework-industrialshields-esp32@https://apps.industrialshields.com/main/arduino/boards/industrialshields-boards-esp32-2.5.1.tar.bz2
    
    [env:board]
    platform = https://github.com/Industrial-Shields/platform-industrialshields-esp32.git
    framework = arduino
    board = esp32plc_21
    ; You don't need these lines for 10 IOs, 14 IOs or the WiFi module
    custom_version = 3 
    custom_click1 = None
    custom_click2 = None 
    
    Change the following parameters according to your PLC model:

    Parameter Device Family Optional (Default) Options
    board ESP32, ESP32 14 No Availabe models can be seen here.
    custom_version ESP32 Yes (3) 1, 3
    custom_click1 ESP32 Yes (None) None, GPRS, NB, LTE, CAN, LORA_EUROPE, LORA_AMERICA, LORA_ASIA
    custom_click2 ESP32 Yes (None) None, GPRS, NB, LTE, CAN, LORA_EUROPE, LORA_AMERICA, LORA_ASIA
    custom_click ESP32 14 Yes (None) None, NB, LTE, DALI, LORA_EUROPE, LORA_AMERICA, LORA_ASIA

    Package version

    The platform package version can also be changed, by modifying the platform_packages parameter. Look at the available versions here.

  3. Modify the src/main.cpp file in the project folder, where the main program is located, to start your program.

    Program example

    Try this simple sketch to alternate the value on a digital output:

    main.cpp
    #include <Arduino.h>
    
    #if defined(PIN_R1)
    #define OUTPUT_PIN R1
    #elif defined(PIN_R0_1)
    #define OUTPUT_PIN R0_1
    #elif defined(PIN_Q0_1)
    #define OUTPUT_PIN Q0_1
    #endif
    
    void setup() {
        pinMode(OUTPUT_PIN, OUTPUT);
        Serial.begin(115200);
    
        Serial.println("Hello World");
    }
    
    void loop() {
        digitalWrite(OUTPUT_PIN, HIGH);
        delay(1000);
        digitalWrite(OUTPUT_PIN, LOW);
        delay(1000);
    }
    
  4. Finally, compile and upload the program, using the buttons on the bottom PlatformIO Toolbar:

    • Build: build the project (or press Ctrl+Alt+B).
    • Upload: autodetect the connected board and program it (or press Ctrl+Alt+U).

After uploading, the PLC will start running the program. The operation execution and result will be visible on the terminal window.

Setting the port

You can manually set the port using the Set upload port.

Cature: PlatformIO IDE program upload

  1. Go to the directory where you want to locate your project, for example: /home/user/Documents/PlatformIO/Projects/Example and run the following command to create the project:
    pio project init
    
  2. Now that you have a PlatformIO project, let's configure it for the PLC model being used. Open the platformio.ini file and modify it accordingly:

    platformio.ini
    [env]
    platform_packages = framework-industrialshields-avr@https://apps.industrialshields.com/main/arduino/boards/industrialshields-boards-avr-1.2.0.tar.bz2
    
    [env:board]
    platform = https://github.com/Industrial-Shields/platform-industrialshields-avr.git
    board = mduinoplc_21+
    framework=arduino
    
    Select your PLC model by changing the board option. The available models can be seen here.

    platformio.ini
    [env]
    platform_packages = framework-industrialshields-esp32@https://apps.industrialshields.com/main/arduino/boards/industrialshields-boards-esp32-2.5.1.tar.bz2
    
    [env:board]
    platform = https://github.com/Industrial-Shields/platform-industrialshields-esp32.git
    framework = arduino
    board = esp32plc_21
    ; You don't need these lines for 10 IOs, 14 IOs or the WiFi module
    custom_version = 3 
    custom_click1 = None
    custom_click2 = None 
    
    Change the following parameters according to your PLC model:

    Parameter Device Family Optional (Default) Options
    board ESP32, ESP32 14 No Availabe models can be seen here.
    custom_version ESP32 Yes (3) 1, 3
    custom_click1 ESP32 Yes (None) None, GPRS, NB, LTE, CAN, LORA_EUROPE, LORA_AMERICA, LORA_ASIA
    custom_click2 ESP32 Yes (None) None, GPRS, NB, LTE, CAN, LORA_EUROPE, LORA_AMERICA, LORA_ASIA
    custom_click ESP32 14 Yes (None) None, NB, LTE, DALI, LORA_EUROPE, LORA_AMERICA, LORA_ASIA

    Package version

    The platform package version can also be changed, by modifying the platform_packages parameter. Look at the available versions here.

  3. Modify the src/main.cpp file in the project folder, where the main program is located, to start your program.

    Program example

    Try this simple sketch to alternate the value on a digital output:

    main.cpp
    #include <Arduino.h>
    
    #if defined(PIN_R1)
    #define OUTPUT_PIN R1
    #elif defined(PIN_R0_1)
    #define OUTPUT_PIN R0_1
    #elif defined(PIN_Q0_1)
    #define OUTPUT_PIN Q0_1
    #endif
    
    void setup() {
        pinMode(OUTPUT_PIN, OUTPUT);
        Serial.begin(115200);
    
        Serial.println("Hello World");
    }
    
    void loop() {
        digitalWrite(OUTPUT_PIN, HIGH);
        delay(1000);
        digitalWrite(OUTPUT_PIN, LOW);
        delay(1000);
    }
    
  4. Build the project and upload it to the PLC with the following command, note that you can manually specify the port with the --upload-port option:

    pio run --target upload --upload-port /dev/ttyACM0