Skip to content

Using VSCode with platformio cli as Arduino dev environment

Using Platformio CLI to develop, build, and upload firmware for embedded systems

Platformio VSCode extension

Graphical interface that integrates with VS Code for an easier development experience.

Install

pip install platformio

Basic command cheat

Command Description
pio project init --board Create a new project
pio lib install Install libraries
pio run Build firmware
pio run --target upload Upload code to the board
pio device monitor Open Serial Monitor
pio run --target clean Clean compiled files

Init project

pio project init --board <board name>
1
2
3
4
5
6
# search for arduino mega 2560 atmelmegaavr bord
pio boards | grep -i mega | grep 2560

# select megaatmega2560 from the list

pio project init --board megaatmega2560

Project struct

├── .vscode
|        └── c_cpp_properties.json
├── include
|        └── helper.hpp
├── lib                                # External libraries (if manually added)
├── platformio.ini                     # PlatformIO configuration file
├── README.md
├── src
|    ├── helper.cpp
|    └── main.cpp
└── test

create c_cpp_properties.json

pio init --ide vscode

code organize

  • include libraries
  • defines and constants
  • global variables
  • user defined function or function signatures
  • setup
  • loop

Demo

Use blink LED demo to learn how to split project to

  • Multiple file
  • Create Library
  • Arduino OOP

Multiple files

Split utils function to header and code
good reference

Project

1
2
3
4
5
6
├── include
│   └── led_function.hpp
├── platformio.ini
└── src
    ├── led_function.cpp
    └── main.cpp
lef_function.hpp
1
2
3
4
5
6
7
8
9
#ifndef LED_FUNCTUIN_H
#define LED_FUNCTUIN_H

#include <Arduino.h>

void powerOnLed(byte pin);
void powerOffLed(byte pin);

#endif
lef_function.cpp
1
2
3
4
5
6
7
8
9
#include "led_function.hpp"

void powerOnLed(byte pin){
    digitalWrite(pin, HIGH);   // Turn the LED on
  }

  void powerOffLed(byte pin){
    digitalWrite(pin, LOW);   // Turn the LED on
  }
main.cpp
#include <Arduino.h>
#include "led_function.hpp"

# define LED_PIN 13

void setup() {
  Serial.begin(115200);
  // Print log
  Serial.println("setup");
  pinMode(LED_PIN, OUTPUT);
}

void loop() {

  powerOnLed(LED_PIN);
  delay(1000);                   // Wait for a second
  powerOffLed(LED_PIN);
  delay(1000);                   // Wait for a second
}

Create custom Library

Create an Arduino library

TODO: Arduino custom library


Arduino OOP

Arduino OOP [40-Minute Crash Course]

TODO: arduino OOP