„Akarj! Remélj! Szeress! És tiéd lesz újra a Föld!”
Herman Hesse

Using arduino from command line in Ubuntu with cmake


After a while I decided I want to use my favorite code editor, and skipp the Arduino-IDE.
I found, with cmake I can easily compile and upload my projects from the command line to an arduino Uno board. Using ubuntu 12.04.
More iformation about arduino-cmake: https://github.com/queezythegreat/arduino-cmake

First U need a successfully working Arduino IDE install.
Step 0: install linux requirements
I think this packages is automatically installed with arduino ide...

gcc-avr - AVR GNU GCC compiler
binutils-avr - AVR binary tools
avr-libc - AVR C library
avrdude - Firmware uploader
Step 1: install cmake and arduino tools
$ apt-get install cmake

Step 2: install arduino-cmake

After that download arduino-cmake from here as a zip file.

Download and uncompress it to a folder in your home directory (in this example using ~/work/arduino/arduino-cmake-master).
Step 3: Create the source codes

Create the source directory for your project:
Create a blink.cpp file here.
$ mkdir ~/work/arduino/blink
Copy the code below to this new file.
#include "Arduino.h"
// LED connected to digital pin 13
const int PIN13 = 13;
void setup() { // sets the digital pin as output pinMode(PIN13, OUTPUT); }
void loop() { // Set LED on, wait a second, turn LED off // then wait one more second digitalWrite(PIN13, HIGH); delay(1000); digitalWrite(PIN13, LOW); delay(1000); }
Step 4: Create CMakeLists.txt and your Build Directory

$ mkdir ~/work/arduino/blink/build
create a file CMakeLists.txt, and copy the content below to this file:
set(PROJECT_NAME blink)
set(CMAKE_ARDUINO_PATH ~/work/arduino/arduino-cmake-master) set(CMAKE_TOOLCHAIN_FILE ${CMAKE_ARDUINO_PATH}/cmake/ArduinoToolchain.cmake) # Arduino Toolchain set(CMAKE_EXTERNAL_LIBS_PATH ~/sketchbook/libraries) # arduino external libs
# Link external libs directories (as Arduino-IDE uses it) link_directories(${CMAKE_EXTERNAL_LIBS_PATH})
cmake_minimum_required(VERSION 2.8)
project(${PROJECT_NAME} C CXX)
# find_package(Arduino 1.0 REQUIRED)
# Define your Arduino board (below is for Arduino uno) set(${PROJECT_NAME}_BOARD uno)
# Define the source code set(${PROJECT_NAME}_SRCS ${PROJECT_NAME}.cpp)
# Define the port for uploading code to the Arduino set(${PROJECT_NAME}_PORT /dev/ttyACM0)
# Command to generate code arduino firmware (.hex file) generate_arduino_firmware(${PROJECT_NAME})
Step 5: Compile and upload your project

$ cd ~/work/arduino/blink/build
$ cmake .. 

Then cmake creates the necessary files later enough to use just the make command.
$ make
$ make blink-upload
Compiles and uploads your code, to your arduino dev board.