Arduinos, with their low barrier to entry and ease of programming, are one of the things that got me started programming a few years ago. My first project was a simple wearable electronics project to get me started with the Lilypad. I made a dress, and added 5 metallic illuminated flowers that alternately faded in an out with LED lights. It was a really fun project, and very satisfying to finish even though I had no idea what I was doing at the time. Since then, I’ve learned a few tips and tricks that make Arduino Programming a bit more fun, and less of a mystery.
One of the biggest hurdles when programming in C for the Arduino is the IDE, or Integrated Development Environment. It’s available for free from the Arduino website, and is a quick way to get setup when starting out with Arduino. the IDE allows you to write code, check your syntax (to a certain degree), compile it, monitor your serial port, and upload it to your hardware. It also comes with a number of example sketches to get you started. However, doing development with it long term can be a little tedious. Syntax highlighting is very limited - all of your variables, functions, and function calls are the same color, where in more robust editors they’d be different colors to be easily identifiable. This can be a little confusing visually when trying to debug a reference to an incorrect variable or misplaced semicolon. In addition, there are no line numbers. This was really frustrating for me, since the compiler references line numbers when it runs into errors in your code. Counting back or forward in your editor without line numbers is really painful. Another thing that irks me is the serial monitor. When debugging or testing, reading serial input is very helpful. Arduino’s serial monitor is a floating window, and can get lost in the shuffle of applications and tabs while I’m working on a project. I also have to frequently restart it, since it’s not the most reliable monitor. Lastly, Arduino IDE only gives you a view of the file you’re currently working on; as someone used to editing several files at a time an opening projects by folder and project, not having tabs or a map of all the files in my project directory can be confusing.
What can we do to fix these little annoyances? Enter the External Editor. With Arduino, we can use pretty much any other editor to write our code, and depending on that editor’s support for compiling and uploading Arduino code, we can use it for anthing else we’d need to use the Arduino IDE for. To enable it, go to Arduino Preferences, and check the box for Use External Editor. Then open up your project files in your preferred editor, and the Arduino IDE will see those changes automatically, and disable your ability to edit the file in Arduino so you don’t have file conflicts. If your editor doesn’t have support for compiling and uploading your code, then you’ll still need to use the Arduino IDE to do that.
I decided I wanted to try to do away with the Arduino IDE entirely. I primarily use Sublime Text as my editor, and there are a wealth of open source packages available for syntax highlighting, theming, and of course, Arduino Programming. The Stino plugin gives me the ability to use the familiar features of sublime text (such as vintage mode) to code my Arduino sketches, and extends Sublime to allow me to compile, upload, serial monitor, and talk to my Arduino board without having to use the Arduino IDE. And I have line numbers for better debugging!
To get this fancy setup, you’ll need to install a few things. Arduino and Sublime Text are cross platform, so there are software packages available for Linux, Mac, and Windows. First off, you’ll need the Arduino Software. Stino is just a wrapper for the Arduino IDE, so it still needs to be present on your computer for Stino to work. Second, if you don’t have Sublime Text, install it. The demo is indefinitely free, but will nag you for registration. Since it’s a great editor, if you can afford the fairly inexpensive license, I suggest purchasing it. Next, install Sublime Text package control - it’s another handy feature that will allow you to install packages and plugins to extend syntax highlighting, shortcuts, and other features Sublime Text has baked in. Once you have these three things installed, open Package Control and install the Stino plugin by searching for Arduino in package control.The developer of Stino has already written some pretty good instructions for installing the plugin with package control, which you can read here.
Once you’ve got everything installed, you’ll need to do a little setup to get Sublime talking to your Arduino board. Installing Stino adds a handy Arduino menu to Sublime Text, and is where you’ll find access to all of Stino’s features. To enable it, go to Sublime Preferences, and select Show Arduino Menu. Since Stino needs the Arduino application to work its magic, we’ll need to tell it where the application is stored on our machine. From the Arduino menu, choose Preferences => Select your Arduino Application. If you’re on a Mac, click on the grey bar under the text box that pops up, and a list of the directories on your machine will show up. Select Applications, scroll down until you see Arduino listed, and select it. The default location for Arduino is in your Applications directory, but if you have it in another directory, you’ll need to navigate to it and select Arduino from there. Note: There appears to be a bug where you have to select your Arduino application each time you relaunch your editor. A little annoying, and might be confusing to some, so I thought I’d point it out.
Ready to do some programming? From here, you can open up a new file and start coding away, and as long as your file is saved with a .ino extension, Sublime should recognize the syntax and highlight, close brackets, and give you syntax and variable suggestions. If you have an Arduino board, you can plug it in, and select your board’s serial port (most use USB) and board type (Uno, Lilypad, etc.). Once you’ve done that and written some code, you should be able to upload it to your Arduino board all within Sublime Text.
This is part 1 of a 3-part series on digging a little deeper into Arduino programming. Stay tuned for parts 2 and 3!