How to program an Arduino using vim

in #arduino8 years ago (edited)

The Arduino IDE leaves a lot to be desired. Granted, it's easy for beginners to build and upload sketches, but as a text-editor it makes you want to smash your keyboard against your monitor. Repeatedly.

Files open in new windows instead of a tab, and there are no advanced features like auto-complete; did I mention it looks like a relic from the Windows 95 era?

This article describes how you can free yourself from the shackles of the Arduino IDE and program your Arduino using vim.

arduino-makefile

arduino-makefile by Sudar Muthu is a great project that allows you compile, upload, and monitor the serial port using a simple makefile.

How simple you ask? 4 lines! Seriously. And one of those is blank!

Let's work through an example together, from scratch. Let's create a makefile for the legendary Blink sketch.

1. Install the Arduino IDE

The Arduino IDE is still required as arduino-makefile uses the software it provides. Download the latest version and extract it:

wget -O arduino.tar.xv http://arduino.cc/download.php\?f\=/arduino-1.6.8-linux64.tar.xz
mkdir ~/.arduino_ide
tar xf arduino.tar.xv -C ~/.arduino_ide --strip-components=1

2. Install arduino-makefile

To install arduino-makefile we'll use git to clone the project:

git clone https://github.com/sudar/Arduino-Makefile.git ~/.arduino_mk

You should now have the Arduino IDE installed in ~/.arduino_ide and arduino-makefile installed in ~/.arduino_mk if you've used the same paths as I have above.

3. Create a makefile

Create a folder for the project and copy the Blink sketch to that folder:

mkdir -p ~/projects/blink
cp .arduino_ide/examples/01.Basics/Blink/Blink.ino ~/projects/blink

Create an empty makefile in the same project folder:

touch ~/projects/blink/Makefile

Open the Makefile in vim and add the following:

ARDUINO_DIR = /home/jon/.arduino_ide
ARDMK_DIR = /home/jon/.arduino_mk
BOARD_TAG = uno

include $(ARDMK_DIR)/Arduino.mk

That's unbelievably simple. All you do is set some variables, and arduino-makefile takes care of the rest.

ARDUINO_DIR Path to the Arduino IDE
ARDMK_DIR Path to arduino-makefile
BOARD_TAG The name of the Arduino board we're programming. Use make show_boards to get a list of available boards.

There are many other variables that can set. One that I use often is ARDUINO_LIBS which specifies the Arduino libraries a project requires. Checkout the arduino-makefile documentation for a complete list.

The last line in the Makefile includes arduino-makefile. You need this!

4. Build, upload, and monitor

There are three commands you need to know, which you can type in a terminal:

make Build the sketch
make upload Upload the sketch
make monitor Monitor the serial port

Try it out. It's freakin' awesome!

Integrate with vim

We're now able to write code using vim and use the command-line to build, upload, and monitor our sketch.

Because we're using a makefile, and vim has built-in support for makefiles, integrating with vim requires no effort. None. Nada.

vim command Task
:make Build the sketch
:make upload Upload the sketch
:make monitor Monitor the serial port

When you run :make and there are errors, vim will go to the line where the first error is. The commands :cnext and :cprevious will cycle through the errors.


Please vote for me for witness to show your support.

This article was originally published by me on March 16th, 2016 at humblecoder.com.

Sort:  

Hi! I am a content-detection robot. I found similar content that readers might be interested in:
http://www.humblecoder.com/how-to-program-the-arduino-using-vim/

Have you upgraded your bot @anyx? I was on a whitelist. In any case, I'm still the original author.

Thnank a lot Спасибо огомное и Danke Shon Copy Shon Take it easy let us fly and sweem and run

Good Luck and Fair Wind for All of Us

James all ok