Uploading a sketch from the command-line

Here's how to upload a sketch from the Raspberry Pi to the Arduino using the command-line tool arduino-mk.

  1. Install arduino-mk `sudo apt-get install arduino-mk`
  2. Create a sketches directory in your home directory. I created a directory called my mysketches.
  3. Add a sketch file into the sketches directory, and rename the sketch extension to .ino I created a blink.ino sketch as follows:
    void setup() {
      pinMode(13, OUTPUT);     
    void loop() {
      digitalWrite(13, HIGH);
      digitalWrite(13, LOW);
  4. Now creat e a makefile making sure the first letter in the name is uppercase (Makefile). Your makefile may be the same as the makefile below:
    ARDUINO_DIR = /usr/share/arduino
    BOARD_TAG    = mega
    ARDUINO_PORT = /dev/ttyUSB0
    include /usr/share/arduino/Arduino.mk
    The b oard_tag must match your type of Arduino. Type `ard-parse-boards --boards` to get a listing like that show below:
    Tag          Board Name
    atmega168    Arduino NG or older w/ ATmega168
    atmega328    Arduino Duemilanove w/ ATmega328
    atmega8      Arduino NG or older w/ ATmega8
    bt           Arduino BT w/ ATmega168
    bt328        Arduino BT w/ ATmega328
    diecimila    Arduino Diecimila or Duemilanove w/ ATmega168
    ethernet     Arduino Ethernet
    fio          Arduino Fio
    leonardo     Arduino Leonardo
    lilypad      LilyPad Arduino w/ ATmega168
    lilypad328   LilyPad Arduino w/ ATmega328
    mega         Arduino Mega (ATmega1280)
    mega2560     Arduino Mega 2560 or Mega ADK
    mini         Arduino Mini w/ ATmega168
    mini328      Arduino Mini w/ ATmega328
    nano         Arduino Nano w/ ATmega168
    nano328      Arduino Nano w/ ATmega328
    pro          Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168
    pro328       Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328
    pro5v        Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168
    pro5v328     Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328
    uno          Arduino Uno
    The arduino_port will mo st likely be /dev/ttyUSB0 however use `dmesg` to confirm what port the arduino is attached to.
  5. Compile and upload the program using the command `make upload`. Here's the output I observed:
    for STTYF in 'stty -F' 'stty --file' 'stty -f' 'stty <' ; \
                      do $STTYF /dev/tty >/dev/null 2>&1 && break ; \
                    done ; \
                    $STTYF /dev/ttyUSB0  hupcl ; \
                    (sleep 0.1 2>/dev/null || sleep 1) ; \
                    $STTYF /dev/ttyUSB0 -hupcl
    /usr/bin/avrdude -q -V -p atmega1280 -C /etc/avrdude.conf -c arduino -b 57600 -P /dev/ttyUSB0  \
                            -U flash:w:build-cli/mysketch.hex:i
    avrdude: AVR device initialized and ready to accept instructions
    avrdude: Device signature = 0x1e9703
    avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
             To disable this feature, specify the -D option.
    avrdude: erasing chip
    avrdude: reading input file "build-cli/mysketch.hex"
    avrdude: writing flash (1556 bytes):
    avrdude: 1556 bytes of flash written
    avrdude: safemode: Fuses OK
    avrdude done.  Thank you.


20-09-2012 22:53