eriksmartt.com>Selected Archives

Arduino development from the command line

It's not obvious while using the Arduino/Wiring IDE, but you're really just writing C++. When you save your sketch, the IDE patches up your code, then passes it to gcc-avr for compiling and avrdude for flashing. Therefore, if you're so inclined (and would prefer to use a different environment for development) you can skip the IDE, add the extra code manually, and build/flash yourself.

The docs on this tell the story, but they're a little out of date (since they reference release 0007.) You still need to sudo apt-get install gcc-avr avr-libc avrdude, but after that, download the "Arduino 0009 installer for Linux" [the newest at the time of writing] instead, uncompress it, and look in "lib/targets/arduino/" for the Makefile and libraries you need. Read the comments in the Makefile--they explain it all quite well.

Once you stash the libraries somewhere handy, starting a new project goes like this:

  1. Create a new directory to work in
  2. Write your Arduino code as a *.cpp instead of a *.pde file
  3. Copy and modify the Makefile for your project
  4. Run 'make' to compile it
  5. Run 'make upload' to flash your code to the Arduino

It's not as simple as the IDE, but it's familiar, it lets you use any text editor you want, and gets you a little closer to whats going on behind the scenes.

For those curious, I've included an example of how the supplied "Blink" sample looks once modified for command-line building. It's a bit longer... but still manageable:

/*
 * Blink (modified for command-line building)
 *
 * The basic Arduino example.  Turns on an LED on for one second,
 * then off for one second, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

#include <WProgram.h>

void setup();
void loop();
int main();

int ledPin = 13;                // LED connected to digital pin 13

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}

int main() {
  init();
  setup();

  for (;;)
    loop();

  return 0;
}