Enginursday: Efficient Arduino Programming with Arduino CLI and VS Code

How to eschew the Arduino IDE for a combination of and Arduino command-line tool (Arduino CLI) and a professional code editor (Visual Studio Code).

Favorited Favorite 4

I’m always looking for the most productive way to get my work done. When I’m in software-engineer mode, my familiarity with the features of my development environment play a huge role in that efficiency. My favorite integrated development environment (IDE) varies by programming language, but, increasingly – whether I’m programming in C/C++, Javascript or Python – I try to find any excuse I can to use Visual Studio Code.

VS Code (not to be confused with Visual Studio) is a free, open source code editor supported by Microsoft. It has a long list of features including powerful key-bind-ability, file navigation, extensions to support just about any language and a pleasantly modern UI.

VS Code editing an Arduino lib/example

I’ve used VS Code as my IDE for platforms ranging from the nRF52840 and the C-based nRF5 SDK to a Raspberry Pi running Python scripts. However, a big chunk of the software development we do here at SparkFun revolves around Arduino, which usually means utilization of the Arduino IDE. Compared to VS Code, the Arduino IDE’s feature-set is limited – there’s basic syntax highlighting, auto formatting, and line numbering, but not much more. It’s missing modern IDE features like:

  • Quick code navigation – Whether it’s find-by-reference (instantly navigating to the definition of the function you’re using), search-by-symbol (quick navigation to function or symbol definitions within a file), or a quick link to a compilation error, code navigation is critical to managing large code bases.
  • Auto-complete – This feature can, of course, help complete long constant names, but it can also provide insight into the parameters that a function may be expecting.
  • Version control integration – Whether you’re using git or SVN, many modern IDE’s provide source-control integration that can show, line-by-line, the changes you’ve made since your last commit.
  • Refactoring – Need to overhaul a function’s naming scheme? Or convert a common block of code into a function that can be more widely used throughout your application? Sounds like a refactoring job! A modern IDE can help with that.
  • Integrated Terminal – Whether you use bash or the Windows CMD, an integrated terminal can save you loads of time. This tool allows you to run “make,” “grep,” or any of your favorite terminal commands without ever swapping windows.

Until recently, beyond exploring Arduino’s “Use External Editor” preference, there wasn’t much to be done to add more functionality to the Arduino development workflow. That all changed with the release of Arduino CLI.

Arduino CLI is a command-line software tool that features board and file management functionality plus compilation and programming tools. Whether you want to download a new Arduino library or upload a compiled Arduino sketch to a RedBoard, the Arduino CLI is there for all of your scripting and command-lining needs. Taking it a step further – combined with an IDE or editor (like VS Code) – Arduino CLI can become an integral part of a powerful, DIY Arduino sketch, library, and core development environment.

To document this pairing, I wrote up a quick tutorial:

New!

Efficient Arduino Programming with Arduino CLI and Visual Studio Code

December 6, 2018

How to eschew the Arduino IDE for a combination of and Arduino command-line tool (Arduino CLI) and a professional code editor (Visual Studio Code).

The tutorial explains how to pair VS Code with Arduino CLI to get the best of both development worlds: a modern IDE and the simplicity of Arduino’s API and board support.


Comments 4 comments

  • For all those looking at doing Arduino in Visual Studio Code, check out the Arduino Extension: https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino @JIMBLOM

  • I read through your tutorial and it looks like VS code could makes things easier, if the task file is unique to each project (sketch). Using the Arduino IDE is a real pain moving from project to project, with different boards, com ports, etc.

  • I’ve been using VS Code and PlatformIO for about a year. Is this better?

  • Thanks for the post! I’ll have to check out the stuff you’ve suggested!

    I’ve been “doing” software since shortly after Neil Armstrong took his “one small step”! (At least we no longer have to punch paper cards for our programs…)

    I’ve always thought that the Arduino API is a real “blast from the past” – it has the feel and capabilities from APIs from the late 1970s, albeit using C++ (which is more recent). For something intended for “newbies” to use, it’s missing what I consider to be the most instructive feature – the ability to single-step through the program. (True, the ATmega328 lacks hardware support for single-stepping, but it is possible to support single-stepping purely in software, though it’s not easy to develop.) Single-stepping, especially when combined with “variable watch”, can be VERY instructive as to what’s going on in the program for the beginner. (It can also be very useful for the more experienced programmer, especially in tracking down those “dumb mistakes” that we all make!). The micro:bit’s simulator can be helpful in this regard, but simulators can have problems with hardware interfaces.

    The other problem I have with the Arduino IDE is that it doesn’t have an easy and obvious way to add “-D” options to the gcc command line. This can be very useful for things like ESP8266 programs where one might have, say, three or four processors for sensors that vary only in name (and/or IP address if, like me, you use fixed IPs for your “edge nodes”). At first blush, it looks like the CLI may work in this situation.

Related Posts

Recent Posts

Tags


All Tags