DEV Community

Udara Dananjaya
Udara Dananjaya

Posted on

How to Set Up Arduino Development with VSCode and Arduino CLI

As the world of DIY electronics and microcontroller programming continues to grow, many makers, engineers, and hobbyists seek more robust and flexible tools for their Arduino projects. While the official Arduino IDE is user-friendly, it lacks advanced features and customizability. A powerful alternative is to use Visual Studio Code (VSCode) paired with the Arduino CLI (Command Line Interface). This combination allows for greater flexibility, faster workflows, and more control over your development environment.

In this guide, we’ll walk you through setting up VSCode with the Arduino CLI to create and manage Arduino projects, compile code, and upload sketches to your Arduino board. Along the way, we’ll also address common issues like file structure errors that might arise.


Why Use VSCode with Arduino CLI?

The traditional Arduino IDE is great for beginners, but as your Arduino skills progress, you might find the need for more advanced features:

  • IntelliSense for code completion and syntax suggestions.
  • Customizability with themes, extensions, and settings.
  • Version control with Git, integrated directly into the editor.
  • Advanced debugging tools when working on larger projects.
  • Command-line tools for compiling and uploading code, which are faster and more flexible.

By combining VSCode with Arduino CLI, you can enjoy these advanced features while keeping the ease and familiarity of the Arduino platform.


Step 1: Install Prerequisites

Before diving into the setup, let's make sure you have all the necessary tools installed.

1. Install Visual Studio Code (VSCode)

First, you'll need to install Visual Studio Code, which is a free, open-source code editor. If you haven’t installed it already, you can download it from here.

VSCode is a versatile editor that supports a wide range of programming languages and tools, including Arduino.

2. Install Arduino CLI

The Arduino CLI allows you to interact with Arduino boards from the command line, bypassing the need for the Arduino IDE. To get started:

  1. Download the Arduino CLI from the official GitHub page.

  2. Follow the installation instructions for your operating system:

    • Windows: You can install it via Chocolatey (if you have it installed):
     choco install arduino-cli
    
  • macOS: You can use Homebrew:

     brew install arduino-cli
    
  • Linux: Download the binaries or use package managers like apt or snap.

  1. After installation, verify the CLI is working by running the following command in your terminal:
   arduino-cli version
Enter fullscreen mode Exit fullscreen mode

3. Install Arduino Core for Your Board

You need to install the core libraries for the specific board you're using. For example, if you're using the Arduino Uno, you'll need to install the arduino:avr core.

  1. First, update the core index:
   arduino-cli core update-index
Enter fullscreen mode Exit fullscreen mode
  1. Install the Arduino Uno core:
   arduino-cli core install arduino:avr
Enter fullscreen mode Exit fullscreen mode

This step ensures that the Arduino CLI knows how to compile code for your specific Arduino board.


Step 2: Set Up VSCode with Arduino Extension

Now that the prerequisites are in place, let's set up VSCode for Arduino development.

1. Install the Arduino Extension in VSCode

In VSCode, you’ll need to install the official Arduino extension to support Arduino development. To do this:

  1. Open VSCode and press Ctrl+Shift+X (or Cmd+Shift+X on macOS) to open the Extensions view.
  2. Search for Arduino and click Install on the Arduino extension by Microsoft. This extension will allow you to:
  • Select your Arduino board.
  • Compile and upload code directly to your board.
  • Use Arduino-specific syntax highlighting and code snippets.

2. Configure Arduino CLI Path in VSCode

If VSCode doesn’t automatically detect your Arduino CLI installation, you may need to manually set the path to the arduino-cli executable. Follow these steps:

  1. Open the Command Palette (Ctrl+Shift+P).
  2. Type "Arduino: Select Arduino Path" and hit Enter.
  3. Set the path to your Arduino CLI. This path is where the arduino-cli executable is installed on your system:
    • Windows: C:\Program Files\Arduino CLI\arduino-cli.exe
    • macOS/Linux: /usr/local/bin/arduino-cli

3. Initialize a New Arduino Project

VSCode might automatically create a new project structure when you first open an Arduino file. If not, create a new folder for your project and add an .ino file.

For example, if you’re creating a blink project, create the following folder structure:

C:\Users\YourUsername\Arduino\
    blink\
        blink.ino
Enter fullscreen mode Exit fullscreen mode

Step 3: Compile and Upload Arduino Code with Arduino CLI

Once your project is set up, you can start writing and uploading code. In this section, we will guide you through the process of compiling and uploading your Arduino code using the CLI.

1. Write Your Arduino Code

Create an blink.ino file in your project folder and add the following basic code to blink the built-in LED on your Arduino Uno:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT); // Set the LED pin as an output
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on
  delay(1000);                     // Wait for 1 second
  digitalWrite(LED_BUILTIN, LOW);  // Turn the LED off
  delay(1000);                     // Wait for 1 second
}
Enter fullscreen mode Exit fullscreen mode

2. Compile the Code

Now, it’s time to compile the sketch. Open a terminal and navigate to your project directory. Run the following command to compile your code for the Arduino Uno:

arduino-cli compile --fqbn arduino:avr:uno blink/blink.ino
Enter fullscreen mode Exit fullscreen mode

The --fqbn flag specifies the fully qualified board name (in this case, arduino:avr:uno for an Arduino Uno). The blink/blink.ino path points to your sketch.

3. Upload the Code

Once the code is compiled, it’s time to upload it to your Arduino board. First, ensure your Arduino is connected to your computer via USB. Then, use the following command to upload your sketch:

arduino-cli upload -p COMx --fqbn arduino:avr:uno blink/blink.ino
Enter fullscreen mode Exit fullscreen mode

Make sure to replace COMx with the correct port for your Arduino board (e.g., COM3, COM4 on Windows, or /dev/ttyUSB0 on Linux/macOS). You can find the correct port by running:

arduino-cli board list
Enter fullscreen mode Exit fullscreen mode

Step 4: Troubleshooting Common Errors

Error: "Can't open sketch: main file missing"

This error occurs when the Arduino CLI can’t find the main .ino file in the specified directory. Here’s how to resolve it:

  • Check the Folder Structure: Make sure your project folder matches the name of the .ino file. For example, if your main file is blink.ino, the folder should be named blink:
  blink/
      blink.ino
Enter fullscreen mode Exit fullscreen mode
  • Correct Path: Ensure that you're passing the correct path to the compile and upload commands:
  arduino-cli compile --fqbn arduino:avr:uno blink/blink.ino
Enter fullscreen mode Exit fullscreen mode

Error: "Board not found"

If you get an error saying the board is not found, ensure:

  • Your Arduino is correctly connected.
  • The correct port is specified using the -p flag (check with arduino-cli board list).
  • The right board core is installed with arduino-cli core install.

Conclusion

By setting up VSCode with Arduino CLI, you can leverage the power of a modern code editor while retaining the simplicity of the Arduino platform. You’ll benefit from features like IntelliSense, Git integration, and faster compiling and uploading of sketches. Plus, the flexibility of the command line makes it easier to automate tasks, create complex projects, and manage your Arduino development environment.

If you follow the steps in this article, you’ll have a streamlined workflow that enhances your productivity and allows you to make the most of both VSCode and Arduino CLI. Happy coding!

Top comments (0)